Laser Sensing Display for UI interfaces in the real world

Dependencies:   mbed

Fork of skinGames_forktest by Alvaro Cassinelli

Committer:
mbedalvaro
Date:
Sun Sep 23 10:11:43 2012 +0000
Revision:
31:5f039cbddee8
Parent:
30:d8af03f01cd4
Child:
32:52273c3291fe
this is quite nice, but  I am going to make a deep modification of the bouncing principle: instead of depending on the penetration, it will just be a factor over the speed (perfect elastic bouncing being factorElastic=1, and perfectly absorption=0);

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedalvaro 0:345b3bc7a0ea 1 #include "mbed.h"
mbedalvaro 0:345b3bc7a0ea 2 #include "hardwareIO.h"
mbedalvaro 0:345b3bc7a0ea 3 #include "mbedOSC.h"
mbedalvaro 0:345b3bc7a0ea 4 #include "blobConfig.h"
mbedalvaro 0:345b3bc7a0ea 5 #include "simpleLaserRenderer.h"
mbedalvaro 0:345b3bc7a0ea 6
mbedalvaro 0:345b3bc7a0ea 7 extern "C" void mbed_reset();
mbedalvaro 0:345b3bc7a0ea 8
mbedalvaro 0:345b3bc7a0ea 9 blobConfig blobconf;
mbedalvaro 0:345b3bc7a0ea 10 simpleLaserSensingRenderer lsr;
mbedalvaro 0:345b3bc7a0ea 11
mbedalvaro 0:345b3bc7a0ea 12 // For tests:
mbedalvaro 0:345b3bc7a0ea 13 DigitalOut myled(LED1);
mbedalvaro 0:345b3bc7a0ea 14 DigitalOut myled2(LED2);
mbedalvaro 23:bf666fcc61bc 15 DigitalOut myled3(LED3);
mbedalvaro 0:345b3bc7a0ea 16
mbedalvaro 0:345b3bc7a0ea 17
mbedalvaro 1:a4050fee11f7 18 // To test the time it takes for executing one loop in the main program:
mbedalvaro 10:6f8e48dca1bd 19 //#define LOOPTIMECOMPUTE
mbedalvaro 1:a4050fee11f7 20
mbedalvaro 1:a4050fee11f7 21 // To get serial commands (for debug, or other things using a Terminal - for instance, a laser scan)
mbedalvaro 14:0fc33a3a7b4b 22 #define SERIAL_COMMANDS
mbedalvaro 0:345b3bc7a0ea 23
mbedalvaro 0:345b3bc7a0ea 24 //---------- ETHERNET / OSC related (in the future, put somewhere else...): -------------------------------------------
mbedalvaro 0:345b3bc7a0ea 25 // mbed IP address (server):
mbedalvaro 0:345b3bc7a0ea 26 #ifdef DHCP
mbedalvaro 0:345b3bc7a0ea 27 EthernetNetIf eth;
mbedalvaro 0:345b3bc7a0ea 28 #else
mbedalvaro 0:345b3bc7a0ea 29 EthernetNetIf eth(
mbedalvaro 1:a4050fee11f7 30 IpAddr(10,0,0,2), //IP Address
mbedalvaro 1:a4050fee11f7 31 IpAddr(255,255,255,0), //Network Mask
mbedalvaro 1:a4050fee11f7 32 IpAddr(10,0,0,1), //Gateway
mbedalvaro 1:a4050fee11f7 33 IpAddr(10,0,0,1) //DNS
mbedalvaro 0:345b3bc7a0ea 34 );
mbedalvaro 0:345b3bc7a0ea 35 #endif
mbedalvaro 0:345b3bc7a0ea 36
mbedalvaro 0:345b3bc7a0ea 37 //uint8_t serverMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
mbedalvaro 0:345b3bc7a0ea 38 uint8_t serverIp[] = { 10, 0, 0, 2 }; // not needed perhaps!
mbedalvaro 0:345b3bc7a0ea 39 int serverPort = 10000;
mbedalvaro 1:a4050fee11f7 40
mbedalvaro 21:bc9b9383f4b6 41 uint8_t destIp[] = {10, 0, 0, 3};
mbedalvaro 0:345b3bc7a0ea 42 int destPort = 12000;
mbedalvaro 1:a4050fee11f7 43
mbedalvaro 0:345b3bc7a0ea 44 char *topAddress="/mbed";
mbedalvaro 0:345b3bc7a0ea 45 char *subAddress[3]={ "/test1" , "/test2" , "/test3" };
mbedalvaro 1:a4050fee11f7 46
mbedalvaro 0:345b3bc7a0ea 47 OSCMessage recMes;
mbedalvaro 0:345b3bc7a0ea 48 OSCMessage sendMes;
mbedalvaro 1:a4050fee11f7 49
mbedalvaro 1:a4050fee11f7 50 OSCClass osc;
mbedalvaro 1:a4050fee11f7 51 //OSCClass osc(&recMes); // instantiate OSC communication object, and set the receiver container from the OSC packets
mbedalvaro 1:a4050fee11f7 52
mbedalvaro 1:a4050fee11f7 53 void processOSC(UDPSocketEvent e);
mbedalvaro 0:345b3bc7a0ea 54 // ----------------------------------------------------------------------------------------------------------------------
mbedalvaro 0:345b3bc7a0ea 55
mbedalvaro 2:34157ebbf56b 56 // Tickers:
mbedalvaro 0:345b3bc7a0ea 57 Ticker timerForRendering;
mbedalvaro 2:34157ebbf56b 58 //Ticker timerForSendingData; // better use a timer, so as not to interrupt the exact laser display ticker
mbedalvaro 2:34157ebbf56b 59
mbedalvaro 9:3321170d157c 60 // Timers:
mbedalvaro 0:345b3bc7a0ea 61 Timer measureLoopPeriod;
mbedalvaro 9:3321170d157c 62 //Timer measureUpdatePeriod;
mbedalvaro 18:d72935b13858 63
mbedalvaro 0:345b3bc7a0ea 64
mbedalvaro 1:a4050fee11f7 65 // to get serial commands (not necessary perhaps)
mbedalvaro 0:345b3bc7a0ea 66 void processSerial();
mbedalvaro 1:a4050fee11f7 67
mbedalvaro 0:345b3bc7a0ea 68 int main() {
mbedalvaro 1:a4050fee11f7 69
mbedalvaro 0:345b3bc7a0ea 70 // Initialize the hardware (laser powers, positions...):
mbedalvaro 0:345b3bc7a0ea 71 IO.init();
mbedalvaro 0:345b3bc7a0ea 72
mbedalvaro 1:a4050fee11f7 73 // -------------------------------
mbedalvaro 1:a4050fee11f7 74 // Set the Ethernet port:
mbedalvaro 1:a4050fee11f7 75 printf("Setting up...\r\n");
mbedalvaro 1:a4050fee11f7 76 EthernetErr ethErr = eth.setup();
mbedalvaro 1:a4050fee11f7 77 if (ethErr) {
mbedalvaro 1:a4050fee11f7 78 printf("Error %d in setup.\r\n", ethErr);
mbedalvaro 1:a4050fee11f7 79 return -1;
mbedalvaro 1:a4050fee11f7 80 }
mbedalvaro 1:a4050fee11f7 81 printf("Setup OK\r\n");
mbedalvaro 1:a4050fee11f7 82
mbedalvaro 1:a4050fee11f7 83 //(1) Sending message:
mbedalvaro 1:a4050fee11f7 84 // Set IP and Port:
mbedalvaro 1:a4050fee11f7 85 sendMes.setIp( destIp );
mbedalvaro 1:a4050fee11f7 86 sendMes.setPort( destPort );
mbedalvaro 1:a4050fee11f7 87 // Set data:
mbedalvaro 1:a4050fee11f7 88 // sendMes.setTopAddress(topAddress);
mbedalvaro 1:a4050fee11f7 89
mbedalvaro 1:a4050fee11f7 90 //setting osc functionnality:
mbedalvaro 1:a4050fee11f7 91 //(2) Receiving:
mbedalvaro 1:a4050fee11f7 92 // recMes.setIp( serverIp ); // not needed?
mbedalvaro 1:a4050fee11f7 93 osc.setReceiveMessage(&recMes); // this sets the receiver container for the OSC packets (we can avoid doing this if we use osc.getMessage() to get messages)
mbedalvaro 1:a4050fee11f7 94 osc.begin(serverPort, &processOSC); // binds the upd (osc) messages to an arbitrary listening port ("server" port), and callback function
mbedalvaro 1:a4050fee11f7 95 // -------------------------------
mbedalvaro 1:a4050fee11f7 96
mbedalvaro 0:345b3bc7a0ea 97 /* // sending seems not to work right after setting the osc object??
mbedalvaro 0:345b3bc7a0ea 98 wait(1);
mbedalvaro 0:345b3bc7a0ea 99 sendMes.setTopAddress("starting");
mbedalvaro 1:a4050fee11f7 100 sendMes.setSubAddress("");
mbedalvaro 0:345b3bc7a0ea 101 osc.sendOsc( &sendMes );
mbedalvaro 0:345b3bc7a0ea 102 */
mbedalvaro 1:a4050fee11f7 103
mbedalvaro 0:345b3bc7a0ea 104 // initialize with the desired blob configuration:
mbedalvaro 1:a4050fee11f7 105
mbedalvaro 0:345b3bc7a0ea 106 // Tested modes:
mbedalvaro 1:a4050fee11f7 107 blobconf.clearConfig();
mbedalvaro 10:6f8e48dca1bd 108 // blobconf.addOneElasticLoopContractCentral();
mbedalvaro 11:62f7183a03e7 109 // blobconf.addOneElasticContourFollowing();
mbedalvaro 1:a4050fee11f7 110
mbedalvaro 14:0fc33a3a7b4b 111 // blobconf.addOneRigidLoopBouncing();
mbedalvaro 9:3321170d157c 112 // blobconf.addOneRigidLoopBouncing();
mbedalvaro 16:2ff1cb2ae1b1 113 //blobconf.addOneRigidLoopFollowing();
mbedalvaro 29:2fc8c12822eb 114 // blobconf.addOneRigidLoopFollowing();
mbedalvaro 29:2fc8c12822eb 115
mbedalvaro 29:2fc8c12822eb 116 blobconf.addOneElasticLoopContractCentralFast();
mbedalvaro 2:34157ebbf56b 117
mbedalvaro 7:0df17f3078bc 118
mbedalvaro 5:73cd58b58f95 119 //blobconf.addOneRigidLoopTest();
mbedalvaro 1:a4050fee11f7 120
mbedalvaro 0:345b3bc7a0ea 121 // Important: first, set the initial position for all the blobs, this will be useful because
mbedalvaro 0:345b3bc7a0ea 122 // when changing modes we can use the previous central position...
mbedalvaro 1:a4050fee11f7 123 // blobconf.setInitialPos(CENTER_AD_MIRROR_X, CENTER_AD_MIRROR_Y);
mbedalvaro 1:a4050fee11f7 124
mbedalvaro 24:4e52031a495b 125 // draw the config once before activating the laser buffer:
mbedalvaro 5:73cd58b58f95 126 blobconf.draw();
mbedalvaro 13:9f03eac02700 127 lsr.startFullDisplay();
mbedalvaro 1:a4050fee11f7 128
mbedalvaro 0:345b3bc7a0ea 129 // RENRERER (attn: setConfigToRender must be called when the blobconf is set - i.e., the number of blobs and number of points/blob is fixed)
mbedalvaro 1:a4050fee11f7 130 lsr.setConfigToRender(&blobconf);
mbedalvaro 1:a4050fee11f7 131
mbedalvaro 1:a4050fee11f7 132 // Timer on the rendering function of the oneLoop object:
mbedalvaro 9:3321170d157c 133 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL); // the address of the object, member function, and interval (in seconds)
mbedalvaro 9:3321170d157c 134 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL); // the address of the object, member function, and interval (in seconds)
mbedalvaro 1:a4050fee11f7 135
mbedalvaro 1:a4050fee11f7 136 // Timer for sending OSC data:
mbedalvaro 9:3321170d157c 137 // timerForSendingData.attach(&blobconf, &blobConfig::sendConfData, 0.025); // time in seconds (25ms -> 40Hz)
mbedalvaro 1:a4050fee11f7 138
mbedalvaro 1:a4050fee11f7 139 //========================================== INFINITE LOOP (in USER PROGRAM CONTEXT) ===================================================================
mbedalvaro 1:a4050fee11f7 140 #ifdef LOOPTIMECOMPUTE
mbedalvaro 1:a4050fee11f7 141 int timeCounterNum=1000;
mbedalvaro 1:a4050fee11f7 142 #endif
mbedalvaro 1:a4050fee11f7 143
mbedalvaro 9:3321170d157c 144 //measureUpdatePeriod.start();
mbedalvaro 1:a4050fee11f7 145
mbedalvaro 1:a4050fee11f7 146 while (true) {
mbedalvaro 1:a4050fee11f7 147
mbedalvaro 13:9f03eac02700 148 if (lsr.endedFullDisplay()) {
mbedalvaro 9:3321170d157c 149
mbedalvaro 9:3321170d157c 150 // measureUpdatePeriod.stop();
mbedalvaro 9:3321170d157c 151 // measureUpdatePeriod.reset();
mbedalvaro 9:3321170d157c 152
mbedalvaro 9:3321170d157c 153 // __disable_irq();
mbedalvaro 9:3321170d157c 154
mbedalvaro 1:a4050fee11f7 155 // update config dynamics (this also could be threaded?):
mbedalvaro 1:a4050fee11f7 156 blobconf.update();
mbedalvaro 1:a4050fee11f7 157
mbedalvaro 10:6f8e48dca1bd 158
mbedalvaro 1:a4050fee11f7 159 // draw the config (note: each kind of blob renders differently)
mbedalvaro 1:a4050fee11f7 160 blobconf.draw();
mbedalvaro 18:d72935b13858 161
mbedalvaro 18:d72935b13858 162
mbedalvaro 18:d72935b13858 163 // (b)Sending Data: // PUT THIS IN AN INTERRUPT OR USE A TIMER!!! it may be TOO FAST...
mbedalvaro 18:d72935b13858 164 // NOTE: better use a timer, so the only ISR "ticker" is the laser rendering (otherwise the laser rendering will be interrupted by the sending of data - the other way is ok):
mbedalvaro 18:d72935b13858 165 // NOTE: timer for sending is now not COMMON to all blobs, but depends on the blob (it could depend on the CONFIG only, but this can be simulated by using
mbedalvaro 18:d72935b13858 166 // per blob timer counter.
mbedalvaro 18:d72935b13858 167 blobconf.sendConfData();
mbedalvaro 18:d72935b13858 168
mbedalvaro 16:2ff1cb2ae1b1 169
mbedalvaro 23:bf666fcc61bc 170 lsr.startFullDisplay(); // this start the point-display counter (wherever the actual laser is). Before update and draw, the counter needs to be reset.
mbedalvaro 16:2ff1cb2ae1b1 171
mbedalvaro 9:3321170d157c 172
mbedalvaro 9:3321170d157c 173 // __enable_irq();
mbedalvaro 9:3321170d157c 174
mbedalvaro 9:3321170d157c 175 // measureUpdatePeriod.start();
mbedalvaro 10:6f8e48dca1bd 176
mbedalvaro 1:a4050fee11f7 177 }
mbedalvaro 0:345b3bc7a0ea 178
mbedalvaro 2:34157ebbf56b 179
mbedalvaro 1:a4050fee11f7 180 // COMMUNICATION:
mbedalvaro 1:a4050fee11f7 181 // (a) Reading commands:
mbedalvaro 1:a4050fee11f7 182 // Ethernet:
mbedalvaro 1:a4050fee11f7 183 Net::poll(); // this will take care of calling processOSC(UDPSocketEvent e) when a new packet arrives.
mbedalvaro 1:a4050fee11f7 184
mbedalvaro 1:a4050fee11f7 185 // Serial:
mbedalvaro 1:a4050fee11f7 186 #ifdef SERIAL_COMMANDS
mbedalvaro 1:a4050fee11f7 187 if (pc.readable()>0) processSerial();
mbedalvaro 1:a4050fee11f7 188 #endif
mbedalvaro 9:3321170d157c 189
mbedalvaro 9:3321170d157c 190
mbedalvaro 1:a4050fee11f7 191 // text:
mbedalvaro 1:a4050fee11f7 192 /*
mbedalvaro 1:a4050fee11f7 193 sendMes.setTopAddress("/hello");
mbedalvaro 1:a4050fee11f7 194 sendMes.setSubAddress("/daito"); // ATTENTION: the host computer needs to know in advance how many points are in the loop (I did not implement "bundle" messages yet...)
mbedalvaro 1:a4050fee11f7 195 int x=(long)10;
mbedalvaro 1:a4050fee11f7 196 sendMes.setArgs( "i", &x);
mbedalvaro 1:a4050fee11f7 197 osc.sendOsc( &sendMes );
mbedalvaro 1:a4050fee11f7 198 */
mbedalvaro 1:a4050fee11f7 199
mbedalvaro 0:345b3bc7a0ea 200 #ifdef LOOPTIMECOMPUTE
mbedalvaro 9:3321170d157c 201 if (timeCounterNum>50) myled = 0;
mbedalvaro 9:3321170d157c 202 // if (timeCounterNum%10==0) blobconf.sendConfData();
mbedalvaro 9:3321170d157c 203 if (timeCounterNum>100) {
mbedalvaro 1:a4050fee11f7 204 myled = 1;
mbedalvaro 1:a4050fee11f7 205 measureLoopPeriod.stop();
mbedalvaro 1:a4050fee11f7 206 sendMes.setTopAddress("/timeloop");
mbedalvaro 1:a4050fee11f7 207 sendMes.setSubAddress("/");
mbedalvaro 10:6f8e48dca1bd 208 // long x=(long)(int(measureLoopPeriod.read_us()/100.0));
mbedalvaro 9:3321170d157c 209 // long x=(long)(blobconf.blobArray[0]->displaySensingBuffer.lsdTrajectory.size());
mbedalvaro 9:3321170d157c 210 // long x=(long)(blobconf.blobArray[0]->normRecenteringVector);
mbedalvaro 10:6f8e48dca1bd 211 long x=(long)(1000*blobconf.blobArray[0]->displaySensingBuffer.lsdTrajectory[0].intensity);
mbedalvaro 1:a4050fee11f7 212 sendMes.setArgs( "i", &x);
mbedalvaro 1:a4050fee11f7 213 osc.sendOsc( &sendMes );
mbedalvaro 1:a4050fee11f7 214 timeCounterNum=0;
mbedalvaro 1:a4050fee11f7 215 measureLoopPeriod.reset();
mbedalvaro 1:a4050fee11f7 216 measureLoopPeriod.start();
mbedalvaro 1:a4050fee11f7 217 } else timeCounterNum++;
mbedalvaro 0:345b3bc7a0ea 218 #endif
mbedalvaro 0:345b3bc7a0ea 219
mbedalvaro 0:345b3bc7a0ea 220 }
mbedalvaro 0:345b3bc7a0ea 221 }
mbedalvaro 0:345b3bc7a0ea 222
mbedalvaro 1:a4050fee11f7 223 // ================= INTERPRET COMMAND =========================
mbedalvaro 0:345b3bc7a0ea 224 // NOTE: the following arrays are GLOBAL (used in processOSC and processSerial, as well as in interpretCommand function):
mbedalvaro 1:a4050fee11f7 225 // max of two addresses (top and sub), of a max length of 24 characters:
mbedalvaro 0:345b3bc7a0ea 226 char address[2][24];
mbedalvaro 0:345b3bc7a0ea 227 //long auxdata[2]; // to store a max of two arguments (note: we will only use LONGs)
mbedalvaro 1:a4050fee11f7 228 int data[2]; // this is to have -1 as NO DATA, to detect errors.
mbedalvaro 1:a4050fee11f7 229
mbedalvaro 1:a4050fee11f7 230 //interpretCommand(const char& address[2][], const int& data[2]) {
mbedalvaro 0:345b3bc7a0ea 231 void interpretCommand() {
mbedalvaro 0:345b3bc7a0ea 232 // (I) =========================================== SPECIAL FUNCTIONS (reset, rescan LUT, etc) ====================================================
mbedalvaro 16:2ff1cb2ae1b1 233 if ( !strcmp(address[0], "takeSnapshot" ) ) {
mbedalvaro 24:4e52031a495b 234 int value=data[0];
mbedalvaro 24:4e52031a495b 235 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 24:4e52031a495b 236
mbedalvaro 1:a4050fee11f7 237 // for test:
mbedalvaro 1:a4050fee11f7 238 for (int i=0; i<2 ; i++) {
mbedalvaro 1:a4050fee11f7 239 myled = 1;
mbedalvaro 1:a4050fee11f7 240 wait(0.1);
mbedalvaro 1:a4050fee11f7 241 myled = 0;
mbedalvaro 1:a4050fee11f7 242 wait(0.1);
mbedalvaro 1:a4050fee11f7 243 }
mbedalvaro 1:a4050fee11f7 244
mbedalvaro 1:a4050fee11f7 245 // First, we need to disable the threaded display for the loop:
mbedalvaro 1:a4050fee11f7 246 timerForRendering.detach();
mbedalvaro 1:a4050fee11f7 247
mbedalvaro 24:4e52031a495b 248 // Then, do the scan (sending values on SERIAL port):
mbedalvaro 24:4e52031a495b 249 IO.scan_serial(value);
mbedalvaro 1:a4050fee11f7 250
mbedalvaro 1:a4050fee11f7 251 // Finally, start again threaded display:
mbedalvaro 9:3321170d157c 252 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 9:3321170d157c 253 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 24:4e52031a495b 254
mbedalvaro 24:4e52031a495b 255 }
mbedalvaro 1:a4050fee11f7 256 }
mbedalvaro 1:a4050fee11f7 257
mbedalvaro 1:a4050fee11f7 258 else if ( !strcmp(address[0], "mbedReset" ) ) mbed_reset();
mbedalvaro 22:d87317d7ca91 259
mbedalvaro 22:d87317d7ca91 260 else if (!strcmp(address[0], "showMirrorLimits")) {
mbedalvaro 22:d87317d7ca91 261 int value=data[0];
mbedalvaro 22:d87317d7ca91 262 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 22:d87317d7ca91 263 timerForRendering.detach();
mbedalvaro 24:4e52031a495b 264 IO.showLimitsMirrors(value);
mbedalvaro 22:d87317d7ca91 265 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 24:4e52031a495b 266 }
mbedalvaro 22:d87317d7ca91 267 }
mbedalvaro 1:a4050fee11f7 268
mbedalvaro 1:a4050fee11f7 269 else if ( !strcmp(address[0], "calibrate" ) ) {
mbedalvaro 1:a4050fee11f7 270 // First, we need to disable the threaded display for the loop:
mbedalvaro 1:a4050fee11f7 271 timerForRendering.detach();
mbedalvaro 1:a4050fee11f7 272 // RESCAN (and save LUT table):
mbedalvaro 1:a4050fee11f7 273 IO.scanLUT();
mbedalvaro 1:a4050fee11f7 274 // Finally, start again threaded display:
mbedalvaro 9:3321170d157c 275 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 9:3321170d157c 276 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 1:a4050fee11f7 277 }
mbedalvaro 1:a4050fee11f7 278
mbedalvaro 1:a4050fee11f7 279 // (II) ========================================= GLOBAL CONFIG and HARDWARE COMMANDS ===========================================
mbedalvaro 1:a4050fee11f7 280
mbedalvaro 1:a4050fee11f7 281 else if ( !strcmp(address[0], "setGreenLaser" ) ) {
mbedalvaro 1:a4050fee11f7 282 int value=data[0];
mbedalvaro 1:a4050fee11f7 283 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 25:74cb85b85fd2 284 if (value==0)
mbedalvaro 25:74cb85b85fd2 285 blobconf.allSetColor(0x04);// only red (note that we are in blue for "test" mode... so the blue will appear when touching something anyway
mbedalvaro 25:74cb85b85fd2 286 else
mbedalvaro 25:74cb85b85fd2 287 blobconf.allSetColor(0x06); // RED and GREEN (note: in the future, make a method to set colors properly! one by one...)
mbedalvaro 1:a4050fee11f7 288 }
mbedalvaro 1:a4050fee11f7 289 }
mbedalvaro 23:bf666fcc61bc 290
mbedalvaro 23:bf666fcc61bc 291 else if ( !strcmp(address[0], "testPower" ) ) {
mbedalvaro 23:bf666fcc61bc 292 // First, we need to disable the threaded display for the loop:
mbedalvaro 23:bf666fcc61bc 293 timerForRendering.detach();
mbedalvaro 23:bf666fcc61bc 294
mbedalvaro 23:bf666fcc61bc 295 // Note: arguments is first 3 bits to set the laser powers (3 LSB bits to set each color)
mbedalvaro 23:bf666fcc61bc 296 // and then the second argument is the number of seconds to wait for the measurment
mbedalvaro 23:bf666fcc61bc 297 int value1=data[0], value2=data[1];
mbedalvaro 23:bf666fcc61bc 298 if ((value1!=-1)&&(value2!=-1)) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 23:bf666fcc61bc 299
mbedalvaro 23:bf666fcc61bc 300 // Set position of mirrors:
mbedalvaro 23:bf666fcc61bc 301 IO.writeOutX(CENTER_AD_MIRROR_X);
mbedalvaro 23:bf666fcc61bc 302 IO.writeOutY(CENTER_AD_MIRROR_Y);
mbedalvaro 23:bf666fcc61bc 303
mbedalvaro 23:bf666fcc61bc 304 for (int i=0; i<3 ; i++) {
mbedalvaro 23:bf666fcc61bc 305 myled3 = 1;
mbedalvaro 23:bf666fcc61bc 306 wait(0.2);
mbedalvaro 23:bf666fcc61bc 307 myled3 = 0;
mbedalvaro 23:bf666fcc61bc 308 wait(0.2);
mbedalvaro 23:bf666fcc61bc 309 }
mbedalvaro 23:bf666fcc61bc 310
mbedalvaro 23:bf666fcc61bc 311 // Set laser power:
mbedalvaro 23:bf666fcc61bc 312 IO.setRGBPower((unsigned char)value1);
mbedalvaro 23:bf666fcc61bc 313
mbedalvaro 23:bf666fcc61bc 314 // Wait...
mbedalvaro 23:bf666fcc61bc 315 wait(value2);// in seconds
mbedalvaro 23:bf666fcc61bc 316 //Timer t;
mbedalvaro 23:bf666fcc61bc 317 //t.start();
mbedalvaro 23:bf666fcc61bc 318 //while(t.read_ms()<value2*1000);
mbedalvaro 23:bf666fcc61bc 319 //t.stop();
mbedalvaro 23:bf666fcc61bc 320 }
mbedalvaro 23:bf666fcc61bc 321
mbedalvaro 23:bf666fcc61bc 322 // Finally, start again threaded display:
mbedalvaro 23:bf666fcc61bc 323 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 23:bf666fcc61bc 324
mbedalvaro 23:bf666fcc61bc 325 }
mbedalvaro 23:bf666fcc61bc 326
mbedalvaro 23:bf666fcc61bc 327
mbedalvaro 1:a4050fee11f7 328
mbedalvaro 1:a4050fee11f7 329 // SIMPLE BEHAVIOUR MODES (to be read from an XML file in the future):
mbedalvaro 11:62f7183a03e7 330 else if (!strcmp(address[0], "elastic_following")) { //
mbedalvaro 1:a4050fee11f7 331 timerForRendering.detach();
mbedalvaro 30:d8af03f01cd4 332
mbedalvaro 30:d8af03f01cd4 333 blobconf.initConfig(ONE_ELASTIC_FOLLOWING);
mbedalvaro 30:d8af03f01cd4 334
mbedalvaro 1:a4050fee11f7 335 lsr.setConfigToRender(&blobconf);
mbedalvaro 9:3321170d157c 336 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 9:3321170d157c 337 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 16:2ff1cb2ae1b1 338
mbedalvaro 11:62f7183a03e7 339 } else if (!strcmp(address[0], "elastic_mouth")) { //
mbedalvaro 1:a4050fee11f7 340 timerForRendering.detach();
mbedalvaro 30:d8af03f01cd4 341
mbedalvaro 30:d8af03f01cd4 342 blobconf.initConfig(ONE_ELASTIC_MOUTH);
mbedalvaro 30:d8af03f01cd4 343
mbedalvaro 1:a4050fee11f7 344 lsr.setConfigToRender(&blobconf);
mbedalvaro 9:3321170d157c 345 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 9:3321170d157c 346 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 19:228430f1350e 347
mbedalvaro 19:228430f1350e 348 } else if (!strcmp(address[0], "elastic_mouth_small")) { //
mbedalvaro 19:228430f1350e 349 timerForRendering.detach();
mbedalvaro 30:d8af03f01cd4 350
mbedalvaro 30:d8af03f01cd4 351 blobconf.initConfig(ONE_ELASTIC_MOUTH_SMALL);
mbedalvaro 30:d8af03f01cd4 352
mbedalvaro 19:228430f1350e 353 lsr.setConfigToRender(&blobconf);
mbedalvaro 19:228430f1350e 354 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 19:228430f1350e 355 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 1:a4050fee11f7 356 }
mbedalvaro 11:62f7183a03e7 357 else if (!strcmp(address[0], "spot_bouncing")) {
mbedalvaro 16:2ff1cb2ae1b1 358 int value=data[0];
mbedalvaro 16:2ff1cb2ae1b1 359 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 16:2ff1cb2ae1b1 360 timerForRendering.detach();
mbedalvaro 30:d8af03f01cd4 361
mbedalvaro 30:d8af03f01cd4 362 blobconf.initConfig(BOUNCING_SPOTS, value); // value is the nb of spots instantiated
mbedalvaro 16:2ff1cb2ae1b1 363
mbedalvaro 16:2ff1cb2ae1b1 364 lsr.setConfigToRender(&blobconf);
mbedalvaro 16:2ff1cb2ae1b1 365 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 16:2ff1cb2ae1b1 366 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 16:2ff1cb2ae1b1 367 }
mbedalvaro 16:2ff1cb2ae1b1 368 }
mbedalvaro 16:2ff1cb2ae1b1 369
mbedalvaro 30:d8af03f01cd4 370 else if (!strcmp(address[0], "spot_lorentz")) {
mbedalvaro 27:1ce994629ffc 371 int value=data[0];
mbedalvaro 27:1ce994629ffc 372 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 27:1ce994629ffc 373 timerForRendering.detach();
mbedalvaro 30:d8af03f01cd4 374
mbedalvaro 30:d8af03f01cd4 375 blobconf.initConfig(LORENTZ_SPOTS, value); // value is the nb of spots instantiated
mbedalvaro 27:1ce994629ffc 376
mbedalvaro 27:1ce994629ffc 377 lsr.setConfigToRender(&blobconf);
mbedalvaro 27:1ce994629ffc 378 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 27:1ce994629ffc 379 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 27:1ce994629ffc 380 }
mbedalvaro 27:1ce994629ffc 381 }
mbedalvaro 28:44b7b6e35548 382
mbedalvaro 28:44b7b6e35548 383 else if (!strcmp(address[0], "air_hockey")) {
mbedalvaro 28:44b7b6e35548 384 int value=data[0];
mbedalvaro 28:44b7b6e35548 385 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 28:44b7b6e35548 386 timerForRendering.detach();
mbedalvaro 30:d8af03f01cd4 387
mbedalvaro 30:d8af03f01cd4 388 blobconf.initConfig(AIR_HOCKEY_GAME, value); // value is the nb of spots instantiated
mbedalvaro 28:44b7b6e35548 389
mbedalvaro 28:44b7b6e35548 390 lsr.setConfigToRender(&blobconf);
mbedalvaro 28:44b7b6e35548 391 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 28:44b7b6e35548 392 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 28:44b7b6e35548 393 }
mbedalvaro 28:44b7b6e35548 394 }
mbedalvaro 28:44b7b6e35548 395
mbedalvaro 28:44b7b6e35548 396 else if (!strcmp(address[0], "spot_following")) {
mbedalvaro 16:2ff1cb2ae1b1 397 int value=data[0];
mbedalvaro 16:2ff1cb2ae1b1 398 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 16:2ff1cb2ae1b1 399
mbedalvaro 16:2ff1cb2ae1b1 400 timerForRendering.detach();
mbedalvaro 22:d87317d7ca91 401
mbedalvaro 30:d8af03f01cd4 402 blobconf.initConfig(FOLLOWING_SPOTS, value); // value is the nb of spots instantiated
mbedalvaro 22:d87317d7ca91 403
mbedalvaro 16:2ff1cb2ae1b1 404 lsr.setConfigToRender(&blobconf);
mbedalvaro 16:2ff1cb2ae1b1 405 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 16:2ff1cb2ae1b1 406 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 16:2ff1cb2ae1b1 407 }
mbedalvaro 16:2ff1cb2ae1b1 408 }
mbedalvaro 16:2ff1cb2ae1b1 409
mbedalvaro 30:d8af03f01cd4 410 else if (!strcmp(address[0], "circular_pong")) {
mbedalvaro 30:d8af03f01cd4 411 int value=data[0];
mbedalvaro 30:d8af03f01cd4 412 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 30:d8af03f01cd4 413
mbedalvaro 30:d8af03f01cd4 414 timerForRendering.detach();
mbedalvaro 30:d8af03f01cd4 415
mbedalvaro 30:d8af03f01cd4 416 blobconf.initConfig(CIRCULAR_PONG_GAME, value); // value is the nb of spots instantiated
mbedalvaro 30:d8af03f01cd4 417
mbedalvaro 30:d8af03f01cd4 418 lsr.setConfigToRender(&blobconf);
mbedalvaro 30:d8af03f01cd4 419 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 30:d8af03f01cd4 420 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 30:d8af03f01cd4 421 }
mbedalvaro 30:d8af03f01cd4 422 }
mbedalvaro 31:5f039cbddee8 423
mbedalvaro 31:5f039cbddee8 424 else if (!strcmp(address[0], "fish_net")) {
mbedalvaro 30:d8af03f01cd4 425 int value=data[0];
mbedalvaro 30:d8af03f01cd4 426 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 30:d8af03f01cd4 427
mbedalvaro 30:d8af03f01cd4 428 timerForRendering.detach();
mbedalvaro 30:d8af03f01cd4 429
mbedalvaro 31:5f039cbddee8 430 blobconf.initConfig(FISH_NET_GAME, value); // value is the nb of spots instantiated
mbedalvaro 31:5f039cbddee8 431
mbedalvaro 31:5f039cbddee8 432 lsr.setConfigToRender(&blobconf);
mbedalvaro 31:5f039cbddee8 433 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 31:5f039cbddee8 434 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 31:5f039cbddee8 435 }
mbedalvaro 31:5f039cbddee8 436 }
mbedalvaro 31:5f039cbddee8 437
mbedalvaro 31:5f039cbddee8 438
mbedalvaro 31:5f039cbddee8 439 else if (!strcmp(address[0], "vertical_pinball")) {
mbedalvaro 31:5f039cbddee8 440 int value=data[0];
mbedalvaro 31:5f039cbddee8 441 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 31:5f039cbddee8 442
mbedalvaro 31:5f039cbddee8 443 timerForRendering.detach();
mbedalvaro 31:5f039cbddee8 444
mbedalvaro 31:5f039cbddee8 445 blobconf.initConfig(VERTICAL_PINBALL_GAME, value);
mbedalvaro 31:5f039cbddee8 446
mbedalvaro 31:5f039cbddee8 447 lsr.setConfigToRender(&blobconf);
mbedalvaro 31:5f039cbddee8 448 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 31:5f039cbddee8 449 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 31:5f039cbddee8 450 }
mbedalvaro 31:5f039cbddee8 451 }
mbedalvaro 31:5f039cbddee8 452
mbedalvaro 31:5f039cbddee8 453 else if (!strcmp(address[0], "pac_man")) {
mbedalvaro 31:5f039cbddee8 454 timerForRendering.detach();
mbedalvaro 31:5f039cbddee8 455
mbedalvaro 30:d8af03f01cd4 456 blobconf.initConfig(PAC_MAN_GAME);
mbedalvaro 30:d8af03f01cd4 457
mbedalvaro 30:d8af03f01cd4 458 lsr.setConfigToRender(&blobconf);
mbedalvaro 30:d8af03f01cd4 459 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 30:d8af03f01cd4 460 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 30:d8af03f01cd4 461 }
mbedalvaro 30:d8af03f01cd4 462
mbedalvaro 31:5f039cbddee8 463 else if (!strcmp(address[0], "spot_tracking")) {
mbedalvaro 31:5f039cbddee8 464 timerForRendering.detach();
mbedalvaro 31:5f039cbddee8 465
mbedalvaro 31:5f039cbddee8 466 blobconf.initConfig(ONE_TRACKING_SPOT); // value is the nb of spots instantiated
mbedalvaro 31:5f039cbddee8 467
mbedalvaro 31:5f039cbddee8 468 lsr.setConfigToRender(&blobconf);
mbedalvaro 31:5f039cbddee8 469 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 31:5f039cbddee8 470 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 31:5f039cbddee8 471 }
mbedalvaro 30:d8af03f01cd4 472
mbedalvaro 16:2ff1cb2ae1b1 473 else if (!strcmp(address[0], "spot_test")) {
mbedalvaro 0:345b3bc7a0ea 474 timerForRendering.detach();
mbedalvaro 0:345b3bc7a0ea 475 // blobconf.computeBoundingBox();
mbedalvaro 1:a4050fee11f7 476 blobconf.clearConfig();
mbedalvaro 16:2ff1cb2ae1b1 477 blobconf.addOneRigidLoopTest();
mbedalvaro 1:a4050fee11f7 478 lsr.setConfigToRender(&blobconf);
mbedalvaro 9:3321170d157c 479 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 9:3321170d157c 480 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 1:a4050fee11f7 481 }
mbedalvaro 1:a4050fee11f7 482
mbedalvaro 1:a4050fee11f7 483 // other:
mbedalvaro 1:a4050fee11f7 484
mbedalvaro 24:4e52031a495b 485 else if ( !strcmp(address[0], "standby" ) ) { // will put ALL the blobs in stand by mode (no update function)
mbedalvaro 0:345b3bc7a0ea 486 blobconf.allStandBy(); // will avoid the update function
mbedalvaro 24:4e52031a495b 487 }
mbedalvaro 24:4e52031a495b 488 else if ( !strcmp(address[0], "resume" ) ) {
mbedalvaro 24:4e52031a495b 489 blobconf.allResume(); // Update function is called for all the blobs
mbedalvaro 1:a4050fee11f7 490 }
mbedalvaro 1:a4050fee11f7 491
mbedalvaro 1:a4050fee11f7 492 // (III) ========================================= Loop control (parameters, etc) ===========================================
mbedalvaro 19:228430f1350e 493
mbedalvaro 24:4e52031a495b 494 else if (!strcmp( address[0], "speedFactor" ) ) {
mbedalvaro 24:4e52031a495b 495 int value=data[0]; // value 100 means no change of speed. 200 is twice as fast, 50 is half as fast.
mbedalvaro 24:4e52031a495b 496 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 24:4e52031a495b 497 for (int i=0; i< blobconf.numBlobs; i++) {
mbedalvaro 24:4e52031a495b 498 //if ((!strcmp(blobconf.blobArray[i].spotName, "rigid_following"))||(!strcmp(blobconf.blobArray[i].spotName, "rigid_following"))) {
mbedalvaro 24:4e52031a495b 499 blobconf.blobArray[i]->speedFactor((float)(1.0*value/100.0));
mbedalvaro 24:4e52031a495b 500 }
mbedalvaro 24:4e52031a495b 501 }
mbedalvaro 24:4e52031a495b 502 }
mbedalvaro 24:4e52031a495b 503
mbedalvaro 24:4e52031a495b 504 else if (!strcmp( address[0], "adjustPlusAngle" ) ) {
mbedalvaro 24:4e52031a495b 505 int value=data[0]; // value 100 means no change of speed. 200 is twice as fast, 50 is half as fast.
mbedalvaro 24:4e52031a495b 506 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 24:4e52031a495b 507 for (int i=0; i< blobconf.numBlobs; i++) blobconf.blobArray[i]->displaySensingBuffer.addDelayMirrors(value);
mbedalvaro 24:4e52031a495b 508 }
mbedalvaro 24:4e52031a495b 509 }
mbedalvaro 24:4e52031a495b 510
mbedalvaro 24:4e52031a495b 511 else if (!strcmp( address[0], "adjustMinusAngle" ) ) {
mbedalvaro 24:4e52031a495b 512 int value=data[0]; // value 100 means no change of speed. 200 is twice as fast, 50 is half as fast.
mbedalvaro 24:4e52031a495b 513 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 24:4e52031a495b 514 for (int i=0; i< blobconf.numBlobs; i++) blobconf.blobArray[i]->displaySensingBuffer.addDelayMirrors(-value);
mbedalvaro 24:4e52031a495b 515 }
mbedalvaro 24:4e52031a495b 516 }
mbedalvaro 24:4e52031a495b 517
mbedalvaro 1:a4050fee11f7 518 else if (!strcmp( address[0], "sendOSC" ) ) {
mbedalvaro 1:a4050fee11f7 519 int value=data[0];
mbedalvaro 1:a4050fee11f7 520 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 1:a4050fee11f7 521 for (int i=0; i< blobconf.numBlobs; i++) {
mbedalvaro 1:a4050fee11f7 522 blobconf.blobArray[i]->sendOSC=(value>0);
mbedalvaro 1:a4050fee11f7 523 }
mbedalvaro 1:a4050fee11f7 524 }
mbedalvaro 1:a4050fee11f7 525 }
mbedalvaro 1:a4050fee11f7 526
mbedalvaro 1:a4050fee11f7 527 else if (!strcmp( address[0], "sendArea" ) ) {
mbedalvaro 1:a4050fee11f7 528 int value=data[0];
mbedalvaro 1:a4050fee11f7 529 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 1:a4050fee11f7 530 for (int i=0; i< blobconf.numBlobs; i++) {
mbedalvaro 1:a4050fee11f7 531 blobconf.blobArray[i]->sendingBlobArea=(value>0);
mbedalvaro 1:a4050fee11f7 532 }
mbedalvaro 1:a4050fee11f7 533 }
mbedalvaro 1:a4050fee11f7 534 }
mbedalvaro 1:a4050fee11f7 535
mbedalvaro 1:a4050fee11f7 536 else if (!strcmp( address[0], "sendPos" ) ) {
mbedalvaro 1:a4050fee11f7 537 int value=data[0];
mbedalvaro 1:a4050fee11f7 538 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 1:a4050fee11f7 539 for (int i=0; i< blobconf.numBlobs; i++) {
mbedalvaro 1:a4050fee11f7 540 blobconf.blobArray[i]->sendingLoopPositions=(value>0);
mbedalvaro 1:a4050fee11f7 541 }
mbedalvaro 1:a4050fee11f7 542 }
mbedalvaro 1:a4050fee11f7 543 }
mbedalvaro 1:a4050fee11f7 544
mbedalvaro 1:a4050fee11f7 545 else if (!strcmp( address[0], "sendRegions" ) ) {
mbedalvaro 1:a4050fee11f7 546 int value=data[0];
mbedalvaro 1:a4050fee11f7 547 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 1:a4050fee11f7 548 for (int i=0; i< blobconf.numBlobs; i++) {
mbedalvaro 1:a4050fee11f7 549 blobconf.blobArray[i]->sendingLoopRegions=(value>0);
mbedalvaro 1:a4050fee11f7 550 }
mbedalvaro 1:a4050fee11f7 551 }
mbedalvaro 1:a4050fee11f7 552 }
mbedalvaro 1:a4050fee11f7 553
mbedalvaro 1:a4050fee11f7 554 else if (!strcmp( address[0], "sendTouched" ) ) {
mbedalvaro 1:a4050fee11f7 555 int value=data[0];
mbedalvaro 1:a4050fee11f7 556 if (value!=-1) { // otherwise do nothing, this is a reception error (there was no data)
mbedalvaro 1:a4050fee11f7 557 for (int i=0; i< blobconf.numBlobs; i++) {
mbedalvaro 1:a4050fee11f7 558 blobconf.blobArray[i]->sendingTouched=(value>0);
mbedalvaro 1:a4050fee11f7 559 }
mbedalvaro 1:a4050fee11f7 560 }
mbedalvaro 1:a4050fee11f7 561 }
mbedalvaro 1:a4050fee11f7 562
mbedalvaro 1:a4050fee11f7 563
mbedalvaro 1:a4050fee11f7 564
mbedalvaro 0:345b3bc7a0ea 565 }
mbedalvaro 0:345b3bc7a0ea 566
mbedalvaro 0:345b3bc7a0ea 567 //============= RECEIVE OSC COMMANDS =========================
mbedalvaro 1:a4050fee11f7 568 // This is the callback function called when there are packets on the listening socket. It is not nice to have it
mbedalvaro 1:a4050fee11f7 569 // here, but for the time being having a "wrapping global" is the simplest solution (we cannot pass a member-function pointer
mbedalvaro 1:a4050fee11f7 570 // as handler to the upd object).
mbedalvaro 0:345b3bc7a0ea 571 void processOSC(UDPSocketEvent e) {
mbedalvaro 0:345b3bc7a0ea 572 osc.onUDPSocketEvent(e);
mbedalvaro 0:345b3bc7a0ea 573
mbedalvaro 0:345b3bc7a0ea 574 if (osc.newMessage) {
mbedalvaro 1:a4050fee11f7 575 // in fact, there is no need to check this if using the method of a global callback function - it is clear this is a new packet... however, it may be
mbedalvaro 1:a4050fee11f7 576 // interesting to use a timer, and process data (answers, etc) only after a certain amount of time, so as to avoid blocking the program in IRQ context...
mbedalvaro 1:a4050fee11f7 577
mbedalvaro 1:a4050fee11f7 578 // Acquire the addresses and arguments and put them in the GLOBAL variables:
mbedalvaro 1:a4050fee11f7 579 strcpy(address[0],"");
mbedalvaro 1:a4050fee11f7 580 strcpy(address[1],"");
mbedalvaro 1:a4050fee11f7 581 for (int i=0; i<recMes.getAddressNum(); i++) strcpy(address[i],recMes.getAddress(i)); // NOTE: up to the rest of the program to check if address[1] is really not null
mbedalvaro 1:a4050fee11f7 582 // Acquire data:
mbedalvaro 1:a4050fee11f7 583 data[0]=-1;
mbedalvaro 1:a4050fee11f7 584 data[1]=-1;
mbedalvaro 1:a4050fee11f7 585 for (int i=0; i<recMes.getArgNum(); i++) data[i]=(int)recMes.getArgInt(i);
mbedalvaro 1:a4050fee11f7 586
mbedalvaro 1:a4050fee11f7 587 // Finally, interpret the command:
mbedalvaro 1:a4050fee11f7 588 interpretCommand();//address, data);
mbedalvaro 1:a4050fee11f7 589
mbedalvaro 0:345b3bc7a0ea 590 }
mbedalvaro 1:a4050fee11f7 591 }
mbedalvaro 1:a4050fee11f7 592
mbedalvaro 0:345b3bc7a0ea 593 //============= RECEIVE SERIAL COMMANDS =========================
mbedalvaro 0:345b3bc7a0ea 594 //
mbedalvaro 1:a4050fee11f7 595 // NOTE: - NUMERIC PARAMETERS have to be send BEFORE the command word. They must be sent as ASCII DEC, without end character.
mbedalvaro 0:345b3bc7a0ea 596 // - Commands words SHOULD NOT have numbers in it. They should be C compliant STRINGS (ended with character '0')
mbedalvaro 1:a4050fee11f7 597 // - order is irrelevant: we can send 10 RADIUS or RADIUS 10.
mbedalvaro 0:345b3bc7a0ea 598
mbedalvaro 1:a4050fee11f7 599 // String to store ALPHANUMERIC DATA (i.e., integers, floating point numbers, unsigned ints, etc represented as DEC) sent wirelessly:
mbedalvaro 0:345b3bc7a0ea 600 char stringData[24]; // note: an integer is two bytes long, represented with a maximum of 5 digits, but we may send floats or unsigned int...
mbedalvaro 0:345b3bc7a0ea 601 int indexStringData=0;//position of the byte in the string
mbedalvaro 0:345b3bc7a0ea 602
mbedalvaro 0:345b3bc7a0ea 603 // String to store COMMAND WORDS:
mbedalvaro 0:345b3bc7a0ea 604 char stringCommand[24]; // note: an integer is two bytes long, represented with a maximum of 5 digits, but we may send floats or unsigned int...
mbedalvaro 0:345b3bc7a0ea 605 int indexStringCommand=0;
mbedalvaro 0:345b3bc7a0ea 606 bool commandReady=false; // will become true when receiving the byte 0 (i.e. the '/0' string terminator)
mbedalvaro 0:345b3bc7a0ea 607
mbedalvaro 0:345b3bc7a0ea 608 void processSerial() {
mbedalvaro 0:345b3bc7a0ea 609
mbedalvaro 1:a4050fee11f7 610 while (pc.readable()>0) {
mbedalvaro 1:a4050fee11f7 611
mbedalvaro 14:0fc33a3a7b4b 612
mbedalvaro 1:a4050fee11f7 613 char val =pc.getc();
mbedalvaro 0:345b3bc7a0ea 614 // pc.printf("Got :%d\n", incomingByte);
mbedalvaro 1:a4050fee11f7 615 //pc.putc(incomingByte);
mbedalvaro 1:a4050fee11f7 616
mbedalvaro 1:a4050fee11f7 617 // Save ASCII numeric characters (ASCII 0 - 9) on stringData:
mbedalvaro 1:a4050fee11f7 618 if ((val >= '0') && (val <= '9')) { // this is 45 to 57 (included)
mbedalvaro 1:a4050fee11f7 619 stringData[indexStringData] = val;
mbedalvaro 1:a4050fee11f7 620 indexStringData++;
mbedalvaro 1:a4050fee11f7 621 }
mbedalvaro 1:a4050fee11f7 622
mbedalvaro 1:a4050fee11f7 623 // Save ASCII letters in stringCommand:
mbedalvaro 1:a4050fee11f7 624 if ((val >= 'A') && (val <= 'z')) { // this is 65 to 122 (included)
mbedalvaro 1:a4050fee11f7 625 stringCommand[indexStringCommand] = val;
mbedalvaro 1:a4050fee11f7 626 indexStringCommand++;
mbedalvaro 1:a4050fee11f7 627 }
mbedalvaro 1:a4050fee11f7 628 // is command ready?
mbedalvaro 1:a4050fee11f7 629 if (val=='/') {
mbedalvaro 1:a4050fee11f7 630 commandReady=true;
mbedalvaro 1:a4050fee11f7 631 stringCommand[indexStringCommand] = 0; // string termination.
mbedalvaro 1:a4050fee11f7 632 indexStringCommand=0; // reset index string for acquiring next command
mbedalvaro 1:a4050fee11f7 633 //Serial.println(stringCommand);
mbedalvaro 1:a4050fee11f7 634 }
mbedalvaro 1:a4050fee11f7 635
mbedalvaro 1:a4050fee11f7 636 // COMMANDS (with or without numeric parameters):
mbedalvaro 1:a4050fee11f7 637 if (commandReady==true) { // it means we can interpret the command string:
mbedalvaro 1:a4050fee11f7 638 commandReady=false;
mbedalvaro 1:a4050fee11f7 639
mbedalvaro 1:a4050fee11f7 640 stringData[indexStringData] = 0 ;// string termination for numeric values;
mbedalvaro 1:a4050fee11f7 641 indexStringData=0;
mbedalvaro 1:a4050fee11f7 642
mbedalvaro 1:a4050fee11f7 643 // PARSE DATA: (TO DO!!!!!!!!!!!!!!):
mbedalvaro 0:345b3bc7a0ea 644
mbedalvaro 1:a4050fee11f7 645 // (a) Parse command (get address[0] and address[1]):
mbedalvaro 1:a4050fee11f7 646 //ex: "/1/standBy" -- > address[0]="1" and address[1]="standBy"
mbedalvaro 1:a4050fee11f7 647 // address[2]
mbedalvaro 1:a4050fee11f7 648
mbedalvaro 1:a4050fee11f7 649 // Serial.println(stringCommand);
mbedalvaro 1:a4050fee11f7 650 // Serial.println(stringData);
mbedalvaro 1:a4050fee11f7 651
mbedalvaro 1:a4050fee11f7 652 // (b) Parse data:
mbedalvaro 1:a4050fee11f7 653
mbedalvaro 1:a4050fee11f7 654 // char address[2][24];
mbedalvaro 1:a4050fee11f7 655 //long auxdata[2]; // to store a max of two arguments (note: we will only use LONGs)
mbedalvaro 1:a4050fee11f7 656 //int data[2]; // this is to have -1 as NO DATA, to detect errors.
mbedalvaro 1:a4050fee11f7 657
mbedalvaro 1:a4050fee11f7 658 // FOR THE TIME BEING there is no parsing for serial commands:
mbedalvaro 1:a4050fee11f7 659
mbedalvaro 1:a4050fee11f7 660 // SCANNING:
mbedalvaro 1:a4050fee11f7 661 if (!strcmp(stringCommand , "takeSnapshot")) {
mbedalvaro 1:a4050fee11f7 662 // First, we need to disable the threaded display for the loop:
mbedalvaro 1:a4050fee11f7 663 timerForRendering.detach();
mbedalvaro 1:a4050fee11f7 664
mbedalvaro 1:a4050fee11f7 665 // Then, do the scan (sending values on serial port):
mbedalvaro 24:4e52031a495b 666 IO.scan_serial(atoi(stringData));
mbedalvaro 1:a4050fee11f7 667
mbedalvaro 1:a4050fee11f7 668 // Finally, start again threaded display:
mbedalvaro 4:f9d364f10335 669 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 9:3321170d157c 670 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 16:2ff1cb2ae1b1 671 } else if (!strcmp(stringCommand , "REDON")) IO.setRedPower(1); // pc.printf("%d\n",incomingByte);
mbedalvaro 1:a4050fee11f7 672
mbedalvaro 14:0fc33a3a7b4b 673 else if (!strcmp(stringCommand , "REDOFF")) IO.setRedPower(0);
mbedalvaro 14:0fc33a3a7b4b 674
mbedalvaro 14:0fc33a3a7b4b 675 else if (!strcmp(stringCommand , "READVALUE")) pc.printf("Value read: %f", lockin.getSmoothValue());//lockin.getLastValue());/
mbedalvaro 1:a4050fee11f7 676
mbedalvaro 14:0fc33a3a7b4b 677 else if (!strcmp(stringCommand , "mbedReset")) mbed_reset();
mbedalvaro 16:2ff1cb2ae1b1 678
mbedalvaro 14:0fc33a3a7b4b 679 else if (!strcmp(stringCommand , "calibrate")) {
mbedalvaro 16:2ff1cb2ae1b1 680 // First, we need to disable the threaded display for the loop:
mbedalvaro 16:2ff1cb2ae1b1 681 timerForRendering.detach();
mbedalvaro 16:2ff1cb2ae1b1 682 // RESCAN (and save LUT table):
mbedalvaro 16:2ff1cb2ae1b1 683 IO.scanLUT();
mbedalvaro 16:2ff1cb2ae1b1 684 // Finally, start again threaded display:
mbedalvaro 16:2ff1cb2ae1b1 685 timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThread, RENDER_INTERVAL);
mbedalvaro 16:2ff1cb2ae1b1 686 // timerForRendering.attach(&lsr, &simpleLaserSensingRenderer::laserRenderThreadONEBLOBONLY, RENDER_INTERVAL);
mbedalvaro 16:2ff1cb2ae1b1 687 }
mbedalvaro 1:a4050fee11f7 688
mbedalvaro 1:a4050fee11f7 689 // FINALLY, interpret commands (but only after parsing):
mbedalvaro 1:a4050fee11f7 690 // interpretCommand();//address, data);
mbedalvaro 0:345b3bc7a0ea 691
mbedalvaro 0:345b3bc7a0ea 692 }
mbedalvaro 1:a4050fee11f7 693 }
mbedalvaro 1:a4050fee11f7 694 }
mbedalvaro 0:345b3bc7a0ea 695
mbedalvaro 0:345b3bc7a0ea 696
mbedalvaro 0:345b3bc7a0ea 697
mbedalvaro 0:345b3bc7a0ea 698 // ================ MISCELANEA
mbedalvaro 0:345b3bc7a0ea 699
mbedalvaro 0:345b3bc7a0ea 700 /* EXAMPLE SEND/RECEIVE on PROCESSING:
mbedalvaro 0:345b3bc7a0ea 701
mbedalvaro 0:345b3bc7a0ea 702 // oscP5sendreceive by andreas schlegel
mbedalvaro 0:345b3bc7a0ea 703 // example shows how to send and receive osc messages.
mbedalvaro 0:345b3bc7a0ea 704 // oscP5 website at http://www.sojamo.de/oscP5
mbedalvaro 0:345b3bc7a0ea 705
mbedalvaro 0:345b3bc7a0ea 706 import oscP5.*;
mbedalvaro 0:345b3bc7a0ea 707 import netP5.*;
mbedalvaro 1:a4050fee11f7 708
mbedalvaro 0:345b3bc7a0ea 709 OscP5 oscP5;
mbedalvaro 0:345b3bc7a0ea 710 NetAddress myRemoteLocation;
mbedalvaro 0:345b3bc7a0ea 711
mbedalvaro 0:345b3bc7a0ea 712 void setup() {
mbedalvaro 0:345b3bc7a0ea 713 size(400,400);
mbedalvaro 0:345b3bc7a0ea 714 frameRate(25);
mbedalvaro 1:a4050fee11f7 715 // start oscP5, listening for incoming messages at port 12000
mbedalvaro 0:345b3bc7a0ea 716 oscP5 = new OscP5(this,12000);
mbedalvaro 1:a4050fee11f7 717
mbedalvaro 0:345b3bc7a0ea 718 // myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
mbedalvaro 0:345b3bc7a0ea 719 // an ip address and a port number. myRemoteLocation is used as parameter in
mbedalvaro 1:a4050fee11f7 720 // oscP5.send() when sending osc packets to another computer, device,
mbedalvaro 0:345b3bc7a0ea 721 // application. usage see below. for testing purposes the listening port
mbedalvaro 0:345b3bc7a0ea 722 // and the port of the remote location address are the same, hence you will
mbedalvaro 0:345b3bc7a0ea 723 // send messages back to this sketch.
mbedalvaro 0:345b3bc7a0ea 724 myRemoteLocation = new NetAddress("10.0.0.2",10000);
mbedalvaro 0:345b3bc7a0ea 725 }
mbedalvaro 0:345b3bc7a0ea 726
mbedalvaro 0:345b3bc7a0ea 727
mbedalvaro 0:345b3bc7a0ea 728 void draw() {
mbedalvaro 1:a4050fee11f7 729 background(0);
mbedalvaro 0:345b3bc7a0ea 730 }
mbedalvaro 0:345b3bc7a0ea 731
mbedalvaro 0:345b3bc7a0ea 732 void mousePressed() {
mbedalvaro 1:a4050fee11f7 733 // in the following different ways of creating osc messages are shown by example
mbedalvaro 0:345b3bc7a0ea 734 OscMessage myMessage = new OscMessage("/mbed/test1");
mbedalvaro 1:a4050fee11f7 735
mbedalvaro 1:a4050fee11f7 736 myMessage.add(123); // add an int to the osc message
mbedalvaro 0:345b3bc7a0ea 737
mbedalvaro 1:a4050fee11f7 738 // send the message
mbedalvaro 1:a4050fee11f7 739 oscP5.send(myMessage, myRemoteLocation);
mbedalvaro 0:345b3bc7a0ea 740 }
mbedalvaro 0:345b3bc7a0ea 741
mbedalvaro 0:345b3bc7a0ea 742
mbedalvaro 1:a4050fee11f7 743 // incoming osc message are forwarded to the oscEvent method.
mbedalvaro 0:345b3bc7a0ea 744 void oscEvent(OscMessage theOscMessage) {
mbedalvaro 1:a4050fee11f7 745 // print the address pattern and the typetag of the received OscMessage
mbedalvaro 0:345b3bc7a0ea 746 print("### received an osc message.");
mbedalvaro 0:345b3bc7a0ea 747 print(" addrpattern: "+theOscMessage.addrPattern());
mbedalvaro 0:345b3bc7a0ea 748 println(" typetag: "+theOscMessage.typetag());
mbedalvaro 0:345b3bc7a0ea 749 }
mbedalvaro 0:345b3bc7a0ea 750
mbedalvaro 0:345b3bc7a0ea 751 */