diff options
author | Hans-Christoph Steiner <eighthave@users.sourceforge.net> | 2006-05-19 12:54:11 +0000 |
---|---|---|
committer | Hans-Christoph Steiner <eighthave@users.sourceforge.net> | 2006-05-19 12:54:11 +0000 |
commit | 07698be94d5a9c695eb8da97b3b6d61ed95e0eba (patch) | |
tree | e1ad3718b6d1e689a7f1cf7d9cac8332adcfdbfb | |
parent | 7eb547273c4177d857199384898cf5e6db1f711f (diff) |
worked out 2-way protocol, everything seems to work from Pd; the data sending is commented out, but it should still work...
svn path=/trunk/externals/hardware/arduino/; revision=5089
-rw-r--r-- | Pd_firmware/Pd_firmware.pde | 335 | ||||
-rw-r--r-- | arduino.pd | 312 |
2 files changed, 536 insertions, 111 deletions
diff --git a/Pd_firmware/Pd_firmware.pde b/Pd_firmware/Pd_firmware.pde index 7e0becd..6e18df8 100644 --- a/Pd_firmware/Pd_firmware.pde +++ b/Pd_firmware/Pd_firmware.pde @@ -1,111 +1,224 @@ -/* Pd_firmware - * ------------------ - * - * - * It was designed to work with the Pd patch of the same - * name in: Help -> Browser -> examples -> hardware - * - * (cleft) 2006 Hans-Christoph Steiner - * @author: Hans-Christoph Steiner - * @date: 2006-03-10 - * @location: Polytechnic University, Brooklyn, New York, USA - */ - -/* - * Pduino protocol - * =============== - * data: 0-127 control: 128-255 - * - * Pd->Arduino commands - * -------------------- - * 150 - next byte sets PWM0 value - * 151 - next byte sets PWM1 value - * 152 - next byte sets PWM2 value - * 200-213 - set digital pin 0-13 to input - * 220-233 - set digital pin 0-13 to output - * - * - * Pd->Arduino byte cycle - * ---------------------- - * 0 cycle marker (255/11111111) - * 1 digitalOut 0-6 bitmask - * 2 digitalOut 7-13 bitmask - * - * Arduino->Pd byte cycle - * ---------------------- - * 0 digitalIn 0-6 bitmask - * 1 digitalIn 7-13 bitmask - * 2 analogIn0 byte0 - * 3 analogIn0 byte1 - * 4 analogIn1 byte0 - * 5 analogIn1 byte1 - * 6 analogIn2 byte0 - * 7 analogIn2 byte1 - * 8 analogIn3 byte0 - * 9 analogIn3 byte1 - * 10 analogIn4 byte0 - * 11 analogIn4 byte1 - * 12 analogIn5 byte0 - * 13 analogIn5 byte1 - * 14 cycle marker (255/11111111) - * - * - * TX RX - * ----------------------- - * - */ - -/* - * CAUTION!! Be careful with the Serial Monitor, it could freeze - * your computer with this firmware running! It outputs data - * without a delay() so its very fast. - */ - - -byte i; - -// this int serves as an array of bits to store pin status -int digitalPinStatus; - -byte analogPin; -int analogData; - -void outputDigital(byte startPin) { - byte digitalPin; - byte digitalPinBit; - byte digitalOutputByte; - byte digitalData; - for(i=0;i<7;++i) - { - digitalPin = i+startPin; - digitalPinBit = 2^digitalPin; - // only read the pin if its set to input - if(digitalPinStatus & digitalPinBit) - digitalData = digitalRead(digitalPin); - else - digitalData = 0; - digitalOutputByte = digitalOutputByte + (2^(i+1-startPin)*digitalData); - } - printByte(digitalOutputByte); -} - -void setup() { - beginSerial(9600); -} - -void loop() { - // read all digital pins - outputDigital(0); - outputDigital(7); - /* - * get analog in - */ - analogData = analogRead(analogPin); - // these two bytes get converted back into the whole number in Pd - printByte(analogData / 32); // div by 32 for the big byte - printByte(analogData % 32); // mod by 32 for the small byte - ++analogPin; - if (analogPin > 5) analogPin = 0; - /* end with the cycle marker */ - printByte(255); -} +/* Pd_firmware
+ * ------------------
+ *
+ *
+ * It was designed to work with the Pd patch of the same
+ * name in: Help -> Browser -> examples -> hardware
+ *
+ * (cleft) 2006 Hans-Christoph Steiner
+ * @author: Hans-Christoph Steiner
+ * @date: 2006-03-10
+ * @location: Polytechnic University, Brooklyn, New York, USA
+ */
+
+/*
+ * Pduino protocol
+ * ===============
+ * data: 0-127 control: 128-255
+ *
+ * Pd->Arduino commands
+ * --------------------
+ * 200-213 - set digital pin 0-13 to output
+ * 214-227 - set digital pin 0-13 to input
+ * 230 - next byte sets PWM0 value
+ * 231 - next byte sets PWM1 value
+ * 232 - next byte sets PWM2 value
+ *
+ *
+ * Pd->Arduino byte cycle
+ * ----------------------
+ * 0 cycle marker (255/11111111)
+ * 1 digitalOut 0-6 bitmask
+ * 2 digitalOut 7-13 bitmask
+ *
+ * Arduino->Pd byte cycle
+ * ----------------------
+ * 0 digitalIn 0-6 bitmask
+ * 1 digitalIn 7-13 bitmask
+ * 2 analogIn0 byte0
+ * 3 analogIn0 byte1
+ * 4 analogIn1 byte0
+ * 5 analogIn1 byte1
+ * 6 analogIn2 byte0
+ * 7 analogIn2 byte1
+ * 8 analogIn3 byte0
+ * 9 analogIn3 byte1
+ * 10 analogIn4 byte0
+ * 11 analogIn4 byte1
+ * 12 analogIn5 byte0
+ * 13 analogIn5 byte1
+ * 14 cycle marker (255/11111111)
+ *
+ *
+ * TX RX
+ * -----------------------
+ *
+ */
+
+/*
+ * CAUTION!! Be careful with the Serial Monitor, it could freeze
+ * your computer with this firmware running! It outputs data
+ * without a delay() so its very fast.
+ */
+
+#define TOTAL_DIGITAL_PINS 14
+
+byte i;
+
+// this flag says that the next serial input will be data, not control
+boolean waitForData = 0;
+
+/* this int serves as an array of bits to store pin status
+ * 0 = INPUT, 1 = OUTPUT
+ */
+int digitalPinStatus;
+
+byte analogPin;
+int analogData;
+
+// -------------------------------------------------------------------------
+void outputDigital(byte startPin) {
+ byte digitalPin;
+ byte digitalPinBit;
+ byte digitalOutputByte;
+ byte digitalData;
+ for(i=0;i<7;++i)
+ {
+ digitalPin = i+startPin;
+ digitalPinBit = OUTPUT << digitalPin;
+ // only read the pin if its set to input
+ if(digitalPinStatus & digitalPinBit) {
+ digitalData = 0; // pin set to OUTPUT, don't read
+ }
+ else {
+ digitalData = digitalRead(digitalPin);
+ }
+ digitalOutputByte = digitalOutputByte + (2^(i+1-startPin)*digitalData);
+ }
+ printByte(digitalOutputByte);
+}
+
+// -------------------------------------------------------------------------
+void setPinMode(int pin, int mode) {
+ pinMode(pin,mode);
+ if(mode == INPUT) {
+ digitalPinStatus = digitalPinStatus &~ (1 << pin);
+ }
+ if(mode == OUTPUT) {
+ digitalPinStatus = digitalPinStatus | (1 << pin);
+ }
+}
+
+// -------------------------------------------------------------------------
+void checkForInput() {
+ if(serialAvailable()) {
+ while(serialAvailable()) {
+ processInput(serialRead());
+ }
+ }
+}
+
+// -------------------------------------------------------------------------
+void processInput(int inputData) {
+ if (waitForData > 0) {
+ // the PWM commands (230-232) have a byte of data following the command
+ analogWrite(waitForData,inputData);
+ waitForData = 0;
+ }
+ else {
+ switch (inputData) {
+ case 200:
+ case 201:
+ case 202:
+ case 203:
+ case 204:
+ case 205:
+ case 206:
+ case 207:
+ case 208:
+ case 209:
+ case 210:
+ case 211:
+ case 212:
+ case 213:
+ setPinMode(inputData-200,OUTPUT);
+ printByte(inputData);
+ printByte(inputData);
+ printByte(inputData);
+ printByte(inputData);
+ break;
+ case 214:
+ case 215:
+ case 216:
+ case 217:
+ case 218:
+ case 219:
+ case 220:
+ case 221:
+ case 222:
+ case 223:
+ case 224:
+ case 225:
+ case 226:
+ case 227:
+ setPinMode(inputData-214,INPUT);
+ printByte(inputData);
+ printByte(inputData);
+ printByte(inputData);
+ printByte(inputData);
+ break;
+ case 230:
+ case 231:
+ case 232:
+ // set waitForData to the PWM pin number
+ waitForData = inputData - 221;
+ printByte(inputData);
+ printByte(inputData);
+ printByte(inputData);
+ printByte(inputData);
+ break;
+ case 255:
+ break;
+ }
+ }
+}
+
+// =========================================================================
+
+// -------------------------------------------------------------------------
+void setup() {
+ beginSerial(9600);
+ for(i=0; i<TOTAL_DIGITAL_PINS; ++i) {
+ setPinMode(i,INPUT);
+ }
+}
+
+// -------------------------------------------------------------------------
+void loop() {
+ // read all digital pins
+ outputDigital(0);
+ outputDigital(7);
+ /*
+ * get analog in
+ */
+/*
+ for(analogPin=0; analogPin<5; ++analogPin)
+ {
+ analogData = analogRead(analogPin);
+ // these two bytes get converted back into the whole number in Pd
+ printByte(analogData >> 7); // bitshift the big stuff into the output byte
+ printByte(analogData % 128); // mod by 32 for the small byte
+ }
+*/
+// ++analogPin;
+// if (analogPin > 5) analogPin = 0;
+ /* end with the cycle marker */
+ // bitshift the big stuff into the output byte
+ printByte(digitalPinStatus >> 7);
+ // clear the 8th bit before truncating to a byte for small byte
+ printByte(digitalPinStatus % 128);
+
+ checkForInput();
+
+ printByte(255);
+}
diff --git a/arduino.pd b/arduino.pd new file mode 100644 index 0000000..e00bcad --- /dev/null +++ b/arduino.pd @@ -0,0 +1,312 @@ +#N canvas 375 1 618 657 10;
+#X obj 478 -14 import cyclone zexy;
+#X obj 9 25 nbx 6 16 -1e+037 1e+037 0 0 empty empty empty 0 -6 0 14
+-3754 -1 -1 8191 256;
+#X obj 110 25 nbx 6 16 -1e+037 1e+037 0 0 empty empty empty 0 -6 0
+14 -3754 -1 -1 27606 256;
+#X text 239 179 On Mac OS X \, set this to the right value:;
+#X msg 179 158 open \$1;
+#X text 12 101 You might need to select another port \, check the Pd
+window to see if it connected successfully:;
+#N canvas 162 133 530 380 serin 0;
+#X obj 120 61 cnv 15 15 15 empty \$0-number-canvas 2 4 7 0 14 -233017
+-1 0;
+#X obj 200 225 s \$0-number-canvas;
+#X obj 60 61 hradio 15 1 0 4 empty empty empty 0 -6 0 8 -225271 -1
+-1 2;
+#X obj 59 147 outlet;
+#X obj 60 13 inlet;
+#X msg 200 202 label \$1;
+#X obj 200 180 makefilename %d;
+#X obj 59 108 float;
+#X connect 2 0 7 0;
+#X connect 4 0 2 0;
+#X connect 5 0 1 0;
+#X connect 6 0 5 0;
+#X connect 7 0 3 0;
+#X connect 7 0 6 0;
+#X coords 0 -1 1 1 76 17 1 60 60;
+#X restore 179 135 pd serin;
+#X text 260 136 serial port #;
+#X text 278 620 released under the GNU GPL;
+#X text 64 605 (C) Copyright 2006 Hans-Christoph Steiner <hans@at.or.at>
+;
+#X obj 211 25 nbx 6 16 -1e+037 1e+037 0 0 empty empty empty 0 -6 0
+14 -3754 -1 -1 0 256;
+#X obj 312 25 nbx 6 16 -1e+037 1e+037 0 0 empty empty empty 0 -6 0
+14 -3754 -1 -1 0 256;
+#X obj 413 25 nbx 6 16 -1e+037 1e+037 0 0 empty empty empty 0 -6 0
+14 -3754 -1 -1 0 256;
+#X obj 179 246 spigot;
+#X obj 229 248 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0
+1;
+#X msg 239 158 close;
+#X obj 64 570 print --------------------;
+#N canvas 630 546 462 312 alternate 0;
+#X obj 87 8 inlet;
+#X obj 151 244 outlet;
+#X obj 319 247 outlet;
+#X obj 159 197 spigot;
+#X obj 319 191 spigot;
+#X obj 158 57 trigger anything bang;
+#X obj 196 152 abs;
+#X floatatom 212 198 5 0 0 0 - - -;
+#X obj 356 147 abs;
+#X msg 295 77 1;
+#X obj 196 173 float;
+#X obj 355 168 float;
+#X obj 86 34 route reset;
+#X obj 356 128 -;
+#X obj 196 133 -;
+#X obj 15 35 loadbang;
+#X obj 86 54 bang;
+#X msg 73 82 1;
+#X msg 103 82 0;
+#X connect 0 0 12 0;
+#X connect 3 0 1 0;
+#X connect 4 0 2 0;
+#X connect 5 0 3 0;
+#X connect 5 0 4 0;
+#X connect 5 1 9 0;
+#X connect 6 0 10 0;
+#X connect 8 0 11 0;
+#X connect 9 0 13 0;
+#X connect 9 0 14 0;
+#X connect 10 0 3 1;
+#X connect 10 0 7 0;
+#X connect 10 0 14 1;
+#X connect 11 0 4 1;
+#X connect 11 0 13 1;
+#X connect 12 0 16 0;
+#X connect 12 1 5 0;
+#X connect 13 0 8 0;
+#X connect 14 0 6 0;
+#X connect 15 0 18 0;
+#X connect 15 0 17 0;
+#X connect 16 0 18 0;
+#X connect 16 0 17 0;
+#X connect 17 0 14 1;
+#X connect 18 0 13 1;
+#X restore 258 345 pd alternate;
+#X msg 179 299 reset;
+#X obj 297 304 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 321 386 +;
+#X obj 321 407 trigger bang float;
+#X obj 329 539 pack symbol float;
+#X obj 311 517 makefilename \$0-%c;
+#X msg 330 562 \; \$1 \$2;
+#X obj 18 57 receive \$0-A;
+#X obj 9 5 receive \$0-B;
+#X obj 110 5 receive \$0-C;
+#X obj 211 5 receive \$0-D;
+#X obj 312 5 receive \$0-E;
+#X obj 413 5 receive \$0-F;
+#X obj 311 463 +;
+#X obj 339 463 float;
+#X msg 340 442 0;
+#X msg 311 442 1;
+#X obj 311 493 + 64;
+#X obj 3 525 spigot;
+#X obj 41 497 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1
+;
+#X obj 439 335 gate 4;
+#X obj 430 359 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 448 359 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 467 359 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 486 359 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 477 314 metro 400;
+#X obj 478 294 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0
+1;
+#X obj 406 261 float;
+#X obj 449 261 + 1;
+#X obj 406 227 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 492 259 bang;
+#X msg 425 227 1;
+#X obj 406 283 moses 5;
+#X obj 179 273 select 255;
+#X msg 243 197 devicename /dev/tty.usbserial-1913;
+#X obj 124 373 spigot;
+#X obj 162 345 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 1
+1;
+#X obj 121 398 select 255;
+#X obj 510 25 nbx 6 16 -1e+037 1e+037 0 0 empty empty empty 0 -6 0
+14 -3754 -1 -1 0 256;
+#X obj 510 5 receive \$0-G;
+#X obj 179 225 comport 0 9600;
+#X obj 398 53 hradio 15 1 0 14 empty empty empty 0 -6 0 8 -262144 -1
+-1 0;
+#X obj 375 54 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1
+;
+#X obj 394 126 +;
+#X obj 375 100 * 14;
+#X obj 394 147 + 200;
+#X floatatom 480 100 5 0 0 0 - - -;
+#X obj 398 72 t b f;
+#X obj 143 430 tolist;
+#X obj 146 538 print;
+#X obj 146 519 list;
+#X obj 170 452 list-len;
+#X obj 170 472 change;
+#X obj 40 369 spigot;
+#X obj 78 341 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1
+;
+#X obj 37 394 select 255;
+#X obj 66 422 print;
+#X obj 172 493 bang;
+#X obj 337 365 * 128;
+#N canvas 295 269 586 307 decode 1;
+#X obj 241 26 inlet;
+#X obj 46 189 & 1;
+#X obj 72 189 & 2;
+#X obj 98 189 & 4;
+#X obj 46 213 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1
+;
+#X obj 72 213 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 2
+;
+#X obj 98 213 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 4
+;
+#X obj 124 213 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 8
+8;
+#X obj 150 213 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0
+16;
+#X obj 182 213 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0
+32;
+#X obj 215 213 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0
+64;
+#X obj 248 213 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 128
+128;
+#X obj 287 213 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0
+256;
+#X obj 326 213 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0
+512;
+#X obj 365 213 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 1024
+1024;
+#X obj 409 213 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0
+2048;
+#X obj 453 213 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0
+2;
+#X obj 499 213 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0
+4;
+#X obj 124 189 & 8;
+#X obj 150 189 & 16;
+#X obj 182 189 & 32;
+#X obj 215 189 & 64;
+#X obj 248 189 & 128;
+#X obj 287 189 & 256;
+#X obj 326 189 & 512;
+#X obj 365 189 & 1024;
+#X obj 409 189 & 2048;
+#X obj 453 189 & 4096;
+#X obj 499 189 & 8192;
+#X connect 0 0 1 0;
+#X connect 0 0 2 0;
+#X connect 0 0 3 0;
+#X connect 0 0 18 0;
+#X connect 0 0 19 0;
+#X connect 0 0 20 0;
+#X connect 0 0 21 0;
+#X connect 0 0 22 0;
+#X connect 0 0 23 0;
+#X connect 0 0 24 0;
+#X connect 0 0 25 0;
+#X connect 0 0 26 0;
+#X connect 0 0 27 0;
+#X connect 0 0 28 0;
+#X connect 1 0 4 0;
+#X connect 2 0 5 0;
+#X connect 3 0 6 0;
+#X connect 18 0 7 0;
+#X connect 19 0 8 0;
+#X connect 20 0 9 0;
+#X connect 21 0 10 0;
+#X connect 22 0 11 0;
+#X connect 23 0 12 0;
+#X connect 24 0 13 0;
+#X connect 25 0 14 0;
+#X connect 26 0 15 0;
+#X connect 27 0 16 0;
+#X connect 28 0 17 0;
+#X restore 34 133 pd decode buttons;
+#X msg 66 164 230 \, 128;
+#X msg 66 191 231 \, 64;
+#X msg 67 213 232 \, 32;
+#X connect 4 0 58 0;
+#X connect 6 0 4 0;
+#X connect 13 0 51 0;
+#X connect 14 0 13 1;
+#X connect 15 0 58 0;
+#X connect 17 0 20 0;
+#X connect 17 1 76 0;
+#X connect 18 0 17 0;
+#X connect 18 0 33 0;
+#X connect 19 0 17 0;
+#X connect 20 0 21 0;
+#X connect 21 0 34 0;
+#X connect 21 1 22 1;
+#X connect 22 0 24 0;
+#X connect 23 0 22 0;
+#X connect 25 0 36 0;
+#X connect 25 0 77 0;
+#X connect 26 0 1 0;
+#X connect 27 0 2 0;
+#X connect 31 0 32 0;
+#X connect 31 0 35 0;
+#X connect 32 0 31 1;
+#X connect 33 0 32 0;
+#X connect 34 0 31 0;
+#X connect 35 0 23 0;
+#X connect 36 0 16 0;
+#X connect 37 0 36 1;
+#X connect 38 0 39 0;
+#X connect 38 1 40 0;
+#X connect 38 2 41 0;
+#X connect 38 3 42 0;
+#X connect 43 0 38 1;
+#X connect 44 0 43 0;
+#X connect 45 0 50 0;
+#X connect 46 0 45 1;
+#X connect 47 0 45 0;
+#X connect 48 0 49 0;
+#X connect 49 0 45 0;
+#X connect 50 0 46 0;
+#X connect 50 0 38 0;
+#X connect 50 1 48 0;
+#X connect 51 0 18 0;
+#X connect 51 1 17 0;
+#X connect 52 0 58 0;
+#X connect 53 0 55 0;
+#X connect 54 0 53 1;
+#X connect 55 0 66 1;
+#X connect 55 1 66 0;
+#X connect 57 0 56 0;
+#X connect 58 0 13 0;
+#X connect 58 0 53 0;
+#X connect 58 0 71 0;
+#X connect 59 0 65 0;
+#X connect 60 0 62 0;
+#X connect 61 0 63 0;
+#X connect 62 0 61 0;
+#X connect 63 0 64 0;
+#X connect 63 0 58 0;
+#X connect 65 0 62 0;
+#X connect 65 1 61 1;
+#X connect 66 0 68 1;
+#X connect 66 0 69 0;
+#X connect 68 0 67 0;
+#X connect 69 0 70 0;
+#X connect 70 0 75 0;
+#X connect 71 0 73 0;
+#X connect 72 0 71 1;
+#X connect 73 0 16 0;
+#X connect 73 1 74 0;
+#X connect 75 0 68 0;
+#X connect 76 0 20 1;
+#X connect 78 0 58 0;
+#X connect 79 0 58 0;
+#X connect 80 0 58 0;
|