diff options
-rw-r--r-- | Pd_firmware/Pd_firmware.pde | 765 | ||||
-rw-r--r-- | arduino-help.pd | 158 | ||||
-rw-r--r-- | arduino.pd | 62 |
3 files changed, 547 insertions, 438 deletions
diff --git a/Pd_firmware/Pd_firmware.pde b/Pd_firmware/Pd_firmware.pde index 3d667b1..8a2de1d 100644 --- a/Pd_firmware/Pd_firmware.pde +++ b/Pd_firmware/Pd_firmware.pde @@ -1,329 +1,436 @@ -/* Pd_firmware aka Pduino
- * ------------------
- *
- * It was designed to work with the Pd object [arduino]
- * which is included in Pd-extended. This firmware could
- * easily be used with other programs like Max/MSP, Processing,
- * or whatever can do serial communications.
- *
- * (copyleft) 2006 Hans-Christoph Steiner <hans@at.or.at>
- * @author: Hans-Christoph Steiner
- * @date: 2006-05-19
- * @location: STEIM, Amsterdam, Netherlands
- */
-
-/* TODO
- *
- * - get digitalInput working
- * - add pulseIn functionality
- * - add software PWM for servos, etc
- * - redesign protocol to accomodate boards with more I/Os
- * - add cycle markers to mark start of analog, digital, pulseIn, and PWM
- */
-
-/*
- * Pduino protocol
- * ===============
- * data: 0-127
- * control: 128-255
- *
- * Pd->Arduino commands
- * --------------------
- * 200-213 - set digital pin 0-13 to INPUT
- * 214-227 - set digital pin 0-13 to OUTPUT
- * 228 - UNUSED
- * 229 - UNUSED
- * 230 - next byte sets PWM0 value
- * 231 - next byte sets PWM1 value
- * 232 - next byte sets PWM2 value
- * 233 - UNUSED
- * 234 - UNUSED
- * 235 - UNUSED
- * 236 - UNUSED
- * 237 - UNUSED
- * 238 - disable all digital inputs
- * 239 - enable all digital inputs
- * 240 - disable all analog inputs
- * 241 - enable 1 analog input (0)
- * 242 - enable 2 analog inputs (0,1)
- * 243 - enable 3 analog inputs (0-2)
- * 244 - enable 4 analog inputs (0-3)
- * 245 - enable 5 analog inputs (0-4)
- * 246 - enable 6 analog inputs (0-5)
- * 255 - cycle marker
- *
- * Pd->Arduino byte cycle
- * ----------------------
- * 0 start of cycle marker (255/11111111)
- * 1 digitalOut 0-6 bitmask
- * 2 digitalOut 7-13 bitmask
- *
- * Arduino->Pd byte cycle
- * ----------------------
- * 0 start of cycle marker (255/11111111)
- * 1 digitalIn 0-6 bitmask
- * 2 digitalIn 7-13 bitmask
- * 3 analogIn0 byte0
- * 4 analogIn0 byte1
- * 5 analogIn1 byte0
- * 6 analogIn1 byte1
- * 7 analogIn2 byte0
- * 8 analogIn2 byte1
- * 9 analogIn3 byte0
- * 10 analogIn3 byte1
- * 11 analogIn4 byte0
- * 12 analogIn4 byte1
- * 13 analogIn5 byte0
- * 14 analogIn5 byte1
- */
-
-/*
- * 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
-
-// for comparing along with INPUT and OUTPUT
-#define PWM 2
-
-// this flag says the next serial input will be PWM data
-byte waitForPWMData = 0;
-
-// this flag says the first data byte for the digital outs is next
-boolean firstInputByte = false;
-
-/* this int serves as an array of bits to store pin status
- * 0 = INPUT, 1 = OUTPUT
- */
-int digitalPinStatus;
-
-/* this byte stores the status off whether PWM is on or not
- * bit 9 = PWM0, bit 10 = PWM1, bit 11 = PWM2
- * the rest of the bits are unused and should remain 0
- */
-int pwmStatus;
-
-boolean digitalInputsEnabled = true;
-byte analogInputsEnabled = 6;
-
-byte analogPin;
-int analogData;
-
-// -------------------------------------------------------------------------
-void transmitDigitalInput(byte startPin) {
- byte i;
- byte digitalPin;
- byte digitalPinBit;
- byte transmitByte;
- 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 if( (digitalPin >= 9) && (pwmStatus & (1 << digitalPin)) ) {
- digitalData = 0; // pin set to PWM, don't read
- }
- else {
- digitalData = digitalRead(digitalPin);
- }
-/* the next line probably needs to be re-thought (i.e. it might not work...) since my
- first attempt was miserably wrong. <hans@at.or.at> */
- transmitByte = transmitByte + ((2^i)*digitalData);
- }
- printByte(transmitByte);
-}
-
-// -------------------------------------------------------------------------
-/* this function sets the pin mode to the correct state and sets the relevant
- * bits in the two bit-arrays that track Digital I/O and PWM status
- */
-void setPinMode(int pin, int mode) {
- if(mode == INPUT) {
- digitalPinStatus = digitalPinStatus &~ (1 << pin);
- pwmStatus = pwmStatus &~ (1 << pin);
- pinMode(pin,INPUT);
- }
- else if(mode == OUTPUT) {
- digitalPinStatus = digitalPinStatus | (1 << pin);
- pwmStatus = pwmStatus &~ (1 << pin);
- pinMode(pin,OUTPUT);
- }
- else if( (mode == PWM) && (pin >= 9) && (pin <= 11) ) {
- digitalPinStatus = digitalPinStatus | (1 << pin);
- pwmStatus = pwmStatus | (1 << pin);
- pinMode(pin,OUTPUT);
- }
-}
-
-// -------------------------------------------------------------------------
-/* this function checks to see if there is data waiting on the serial port
- * then processes all of the stored data
- */
-void checkForInput() {
- if(serialAvailable()) {
- while(serialAvailable()) {
- processInput( (byte)serialRead() );
- }
- }
-}
-
-// -------------------------------------------------------------------------
-void processInput(byte inputData) {
- int i;
- int mask;
-
- // the PWM commands (230-232) have a byte of data following the command
- if (waitForPWMData > 0) {
- // printByte(150);
- // printByte(inputData);
- analogWrite(waitForPWMData,inputData);
- waitForPWMData = 0;
- }
- else if(inputData < 128) {
- // printByte(151);
- if(firstInputByte) {
- // printByte(160);
- for(i=0; i<7; ++i) {
- mask = 1 << i;
- //printByte(254);
- //printByte(i);
- //printByte(mask);
- if(digitalPinStatus & mask) {
- digitalWrite(i, inputData & mask);
- //printByte(inputData & mask);
- }
- }
- firstInputByte = false;
- }
- else {
- // printByte(161);
- // output data for pins 7-13
- for(i=7; i<TOTAL_DIGITAL_PINS; ++i) {
- mask = 1 << i;
- //printByte(254);
- //printByte(i);
- //printByte(mask);
- if( (digitalPinStatus & mask) && !(pwmStatus & mask) ) {
- // inputData is a byte and mask is an int, so align the high part of mask
- digitalWrite(i, inputData & (mask >> 7));
- //printByte(inputData & (mask >> 7));
- }
- }
- }
- }
- else {
- // printByte(152);
- // printByte(inputData);
- 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,INPUT);
- 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,OUTPUT);
- break;
- case 230:
- case 231:
- case 232:
- waitForPWMData = inputData - 221; // set waitForPWMData to the PWM pin number
- setPinMode(waitForPWMData, PWM);
- break;
- case 238: // all digital inputs off
- digitalInputsEnabled = false;
- break;
- case 239: // all digital inputs on
- digitalInputsEnabled = true;
- break;
- case 240: // analog input off
- case 241: // analog 0 on
- case 242: // analog 0,1 on
- case 243: // analog 0-2 on
- case 244: // analog 0-3 on
- case 245: // analog 0-4 on
- case 246: // analog 0-5 on
- analogInputsEnabled = inputData - 240;
- break;
- case 255: // incoming digital output bytes
- firstInputByte = true;
- break;
- }
- }
-}
-
-// =========================================================================
-
-// -------------------------------------------------------------------------
-void setup() {
- byte i;
-
- beginSerial(9600);
- for(i=0; i<TOTAL_DIGITAL_PINS; ++i) {
- setPinMode(i,INPUT);
- }
-}
-
-// -------------------------------------------------------------------------
-void loop() {
- checkForInput();
-
- // read all digital pins, in enabled
- if(digitalInputsEnabled) {
- transmitDigitalInput(0);
- checkForInput();
- transmitDigitalInput(7);
- checkForInput();
- }
- else if(analogInputsEnabled) {
- // filler bytes, since the first thing sent is always the digitalInputs
- printByte(0);
- printByte(0);
- checkForInput();
- }
-
- /* get analog in, for the number enabled
- */
- for(analogPin=0; analogPin<analogInputsEnabled; ++analogPin) {
- analogData = analogRead(analogPin);
- // these two bytes get converted back into the whole number in Pd
- // the higher bits should be zeroed so that the 8th bit doesn't get set
- printByte(analogData >> 7); // bitshift the big stuff into the output byte
- printByte(analogData % 128); // mod by 32 for the small byte
- checkForInput();
- }
-
- /* end with the cycle marker, if any of the inputs are enabled */
- if( digitalInputsEnabled || analogInputsEnabled) {
- printByte(255);
- }
-}
+/* Arduino firmware aka Firmata + * ------------------ + * + * It was designed to work with the Pd object [arduino] + * which is included in Pd-extended. This firmware could + * easily be used with other programs like Max/MSP, Processing, + * or whatever can do serial communications. + * + * (copyleft) 2006 Hans-Christoph Steiner <hans@at.or.at> + * @author: Hans-Christoph Steiner + * @date: 2006-05-19 + * @location: STEIM, Amsterdam, Netherlands + */ + +/* TODO + * + * - get digitalInput working + * - add pulseIn functionality + * - add software PWM for servos, etc + * - redesign protocol to accomodate boards with more I/Os + * - add cycle markers to mark start of analog, digital, pulseIn, and PWM + */ + +/* firmata protocol + * =============== + * data: 0-127 + * control: 128-255 + */ + +/* computer->Arduino commands + * -------------------- */ + /* 128-129 // UNUSED */ +#define SET_PIN_ZERO_TO_IN 130 // set digital pin 0 to INPUT +#define SET_PIN_ONE_TO_IN 131 // set digital pin 1 to INPUT +#define SET_PIN_TWO_TO_IN 132 // set digital pin 2 to INPUT +#define SET_PIN_THREE_TO_IN 133 // set digital pin 3 to INPUT +#define SET_PIN_FOUR_TO_IN 134 // set digital pin 4 to INPUT +#define SET_PIN_FIVE_TO_IN 135 // set digital pin 5 to INPUT +#define SET_PIN_SIX_TO_IN 136 // set digital pin 6 to INPUT +#define SET_PIN_SEVEN_TO_IN 137 // set digital pin 7 to INPUT +#define SET_PIN_EIGHT_TO_IN 138 // set digital pin 8 to INPUT +#define SET_PIN_NINE_TO_IN 139 // set digital pin 9 to INPUT +#define SET_PIN_TEN_TO_IN 140 // set digital pin 10 to INPUT +#define SET_PIN_ELEVEN_TO_IN 141 // set digital pin 11 to INPUT +#define SET_PIN_TWELVE_TO_IN 142 // set digital pin 12 to INPUT +#define SET_PIN_THIRTEEN_TO_IN 143 // set digital pin 13 to INPUT +/* 144-149 // UNUSED */ +#define DISABLE_DIGITAL_INPUTS 150 // disable reporting of digital inputs +#define ENABLE_DIGITAL_INPUTS 151 // enable reporting of digital inputs +/* 152-159 // UNUSED */ +#define DISABLE_ALL_ANALOG_INS 160 // disable reporting on all analog ins +#define ENABLE_ONE_ANALOG_IN 161 // enable reporting for 1 analog in (0) +#define ENABLE_TWO_ANALOG_INS 162 // enable reporting for 2 analog ins (0,1) +#define ENABLE_THREE_ANALOG_INS 163 // enable reporting for 3 analog ins (0-2) +#define ENABLE_FOUR_ANALOG_INS 164 // enable reporting for 4 analog ins (0-3) +#define ENABLE_FIVE_ANALOG_INS 165 // enable reporting for 5 analog ins (0-4) +#define ENABLE_SIX_ANALOG_INS 166 // enable reporting for 6 analog ins (0-5) +/* 167-199 // UNUSED */ +#define SET_PIN_ZERO_TO_OUT 200 // set digital pin 0 to OUTPUT +#define SET_PIN_ONE_TO_OUT 201 // set digital pin 1 to OUTPUT +#define SET_PIN_TWO_TO_OUT 202 // set digital pin 2 to OUTPUT +#define SET_PIN_THREE_TO_OUT 203 // set digital pin 3 to OUTPUT +#define SET_PIN_FOUR_TO_OUT 204 // set digital pin 4 to OUTPUT +#define SET_PIN_FIVE_TO_OUT 205 // set digital pin 5 to OUTPUT +#define SET_PIN_SIX_TO_OUT 206 // set digital pin 6 to OUTPUT +#define SET_PIN_SEVEN_TO_OUT 207 // set digital pin 7 to OUTPUT +#define SET_PIN_EIGHT_TO_OUT 208 // set digital pin 8 to OUTPUT +#define SET_PIN_NINE_TO_OUT 209 // set digital pin 9 to OUTPUT +#define SET_PIN_TEN_TO_OUT 210 // set digital pin 10 to OUTPUT +#define SET_PIN_ELEVEN_TO_OUT 211 // set digital pin 11 to OUTPUT +#define SET_PIN_TWELVE_TO_OUT 212 // set digital pin 12 to OUTPUT +#define SET_PIN_THIRTEEN_TO_OUT 213 // set digital pin 13 to OUTPUT +/* 214-228 // UNUSED */ +#define OUTPUT_TO_DIGITAL_PINS 229 // next two bytes set digital output data +/* 230-249 // UNUSED */ +#define DISABLE_PWM 250 // next byte sets pin # to disable +#define ENABLE_PWM 251 // next two bytes set pin # and duty cycle +#define DISABLE_SOFTWARE_PWM 252 // next byte sets pin # to disable +#define ENABLE_SOFTWARE_PWM 253 // next two bytes set pin # and duty cycle +#define SET_SOFTWARE_PWM_FREQ 254 // set master frequency for software PWMs +/* 252-254 // UNUSED */ +#define INPUT_CYCLE_MARKER 255 // input cycle marker + + +/* two byte digital output data format + * ---------------------- + * 0 get ready for digital input bytes (229) + * 1 digitalOut 0-6 bitmask + * 2 digitalOut 7-13 bitmask + */ + + /* two byte software PWM data format + * ---------------------- + * 0 get ready for digital input bytes (250/251) + * 1 pin # + * 2 pulse width + */ + + +/* Arduino->Computer byte cycle + * ---------------------- + * 0 start of cycle marker (255/11111111) + * 1 digital read from Arduino // 0-6 bitmask + * 2 digital read from Arduino // 7-13 bitmask + * 3 analog input pin 0 from Arduino // byte 0 + * 4 analog input pin 1 from Arduino // byte 1 + * 5 analog input pin 2 from Arduino // byte 2 + * 6 analog input pin 3 from Arduino // byte 3 + * 7 analog input pin 4 from Arduino // byte 4 + * 8 analog input pin 5 from Arduino // byte 5 + * 9 analog input pin 6 from Arduino // byte 6 + * 10 analog input pin 7 from Arduino // byte 10 + * 11 analog input pin 8 from Arduino // byte 11 + * 12 analog input pin 9 from Arduino // byte 12 + * 13 analog input pin 10 from Arduino // byte 13 + * 14 analog input pin 11 from Arduino // byte 14 + */ + +/* + * 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 + +// for comparing along with INPUT and OUTPUT +#define PWM 2 +#define SOFTPWM 3 + +// maximum number of post-command data bytes +#define MAX_DATA_BYTES 2 +// this flag says the next serial input will be data +byte waitForData = 0; +byte executeMultiByteCommand = 0; // command to execute after getting multi-byte data +byte storedInputData[MAX_DATA_BYTES] = {0,0}; // multi-byte data + +// this flag says the first data byte for the digital outs is next +boolean firstInputByte = false; + +/* this int serves as a bit-wise array to store pin status + * 0 = INPUT, 1 = OUTPUT + */ +int digitalPinStatus; + +/* this byte stores the status off whether PWM is on or not + * bit 9 = PWM0, bit 10 = PWM1, bit 11 = PWM2 + * the rest of the bits are unused and should remain 0 + */ +int pwmStatus; + +/* this byte stores the status of whether software PWM is on or not */ +/* 00000010 00000000 means bit 10 is softWarePWM enabled */ +int softPwmStatus; + +boolean digitalInputsEnabled = true; +byte analogInputsEnabled = 6; + +byte analogPin; +int analogData; + +// ------------------------------------------------------------------------- +void transmitDigitalInput(byte startPin) { + byte i; + byte digitalPin; + byte digitalPinBit; + byte transmitByte; + 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 if( (digitalPin >= 9) && (pwmStatus & (1 << digitalPin)) ) { + digitalData = 0; // pin set to PWM, don't read + } + else { + digitalData = digitalRead(digitalPin); + } +/* the next line probably needs to be re-thought (i.e. it might not work...) since my + first attempt was miserably wrong. <hans@at.or.at> */ + transmitByte = transmitByte + ((2^i)*digitalData); + } + printByte(transmitByte); +} + + + +// ------------------------------------------------------------------------- +/* this function sets the pin mode to the correct state and sets the relevant + * bits in the two bit-arrays that track Digital I/O and PWM status + */ +void setPinMode(int pin, int mode) { + if(mode == INPUT) { + digitalPinStatus = digitalPinStatus &~ (1 << pin); + pwmStatus = pwmStatus &~ (1 << pin); + pinMode(pin,INPUT); + } + else if(mode == OUTPUT) { + digitalPinStatus = digitalPinStatus | (1 << pin); + pwmStatus = pwmStatus &~ (1 << pin); + pinMode(pin,OUTPUT); + } + else if( (mode == PWM) && (pin >= 9) && (pin <= 11) ) { + digitalPinStatus = digitalPinStatus | (1 << pin); + pwmStatus = pwmStatus | (1 << pin); + softPwmStatus = softPwmStatus &~ (1 << pin); + pinMode(pin,OUTPUT); + } + else if(mode == SOFTPWM) { + digitalPinStatus = digitalPinStatus | (1 << pin); + pwmStatus = pwmStatus &~ (1 << pin); + softPwmStatus = softPwmStatus | (1 << pin); + pinMode(pin,OUTPUT); + } +} + +void setSoftPwm (int pin, byte pulsePeriod) { + byte i; +/* for(i=0; i<7; ++i) { + mask = 1 << i; + if(digitalPinStatus & mask) { + digitalWrite(i, inputData & mask); + } + } + */ + //read timer type thing + + //loop through each pin, turn them on if selected + //softwarePWMStatus + //check timer type thing against pulsePeriods for each pin + //throw pin low if expired +} + +void setSoftPwmFreq(byte freq) { +} + + +void disSoftPwm(int pin) { + //throw pin low + +} + + +// ------------------------------------------------------------------------- +/* this function checks to see if there is data waiting on the serial port + * then processes all of the stored data + */ +void checkForInput() { + if(serialAvailable()) { + while(serialAvailable()) { + processInput( (byte)serialRead() ); + } + } +} + +// ------------------------------------------------------------------------- +void processInput(byte inputData) { + int i; + int mask; + + // a few commands have byte(s) of data following the command + if( waitForData > 0 ) { + storedInputData[waitForData - 1] = inputData; + //analogWrite(waitForPWMData,inputData); + waitForData--; + } + else if(executeMultiByteCommand) { + //we got everything + switch(executeMultiByteCommand) { + case ENABLE_PWM: + case DISABLE_PWM: + //PWM 0 on the board is PIN 9 + analogWrite(storedInputData[0] + 9, storedInputData[1]); + break; + case ENABLE_SOFTWARE_PWM: + setPinMode(storedInputData[0],SOFTPWM); + setSoftPwm(storedInputData[0], storedInputData[1]); + break; + case DISABLE_SOFTWARE_PWM: + disSoftPwm(storedInputData[0]); + break; + case SET_SOFTWARE_PWM_FREQ: + setSoftPwmFreq(storedInputData[0]); + break; + } + executeMultiByteCommand = 0; + } + + else if(inputData < 128) { + if(firstInputByte) { // + for(i=0; i<7; ++i) { + mask = 1 << i; + if(digitalPinStatus & mask) { + digitalWrite(i, inputData & mask); + } + } + firstInputByte = false; + } + else { // + // output data for pins 7-13 + for(i=7; i<TOTAL_DIGITAL_PINS; ++i) { + mask = 1 << i; + if( (digitalPinStatus & mask) && !(pwmStatus & mask) && !(softPwmStatus & mask) ) { + // inputData is a byte and mask is an int, so align the high part of mask + digitalWrite(i, inputData & (mask >> 7)); + } + } + } + } + else { + switch (inputData) { + case SET_PIN_ZERO_TO_IN: + case SET_PIN_ONE_TO_IN: + case SET_PIN_TWO_TO_IN: + case SET_PIN_THREE_TO_IN: + case SET_PIN_FOUR_TO_IN: + case SET_PIN_FIVE_TO_IN: + case SET_PIN_SIX_TO_IN: + case SET_PIN_SEVEN_TO_IN: + case SET_PIN_EIGHT_TO_IN: + case SET_PIN_NINE_TO_IN: + case SET_PIN_TEN_TO_IN: + case SET_PIN_ELEVEN_TO_IN: + case SET_PIN_TWELVE_TO_IN: + case SET_PIN_THIRTEEN_TO_IN: + setPinMode(inputData - SET_PIN_ZERO_TO_IN, INPUT); + break; + case SET_PIN_ZERO_TO_OUT: + case SET_PIN_ONE_TO_OUT: + case SET_PIN_TWO_TO_OUT: + case SET_PIN_THREE_TO_OUT: + case SET_PIN_FOUR_TO_OUT: + case SET_PIN_FIVE_TO_OUT: + case SET_PIN_SIX_TO_OUT: + case SET_PIN_SEVEN_TO_OUT: + case SET_PIN_EIGHT_TO_OUT: + case SET_PIN_NINE_TO_OUT: + case SET_PIN_TEN_TO_OUT: + case SET_PIN_ELEVEN_TO_OUT: + case SET_PIN_TWELVE_TO_OUT: + case SET_PIN_THIRTEEN_TO_OUT: + setPinMode(inputData - SET_PIN_ZERO_TO_OUT, OUTPUT); + break; + case DISABLE_DIGITAL_INPUTS: // all digital inputs off + digitalInputsEnabled = false; + break; + case ENABLE_DIGITAL_INPUTS: // all digital inputs on + digitalInputsEnabled = true; + break; + case DISABLE_ALL_ANALOG_INS: // analog input off + case ENABLE_ONE_ANALOG_IN: // analog 0 on + case ENABLE_TWO_ANALOG_INS: // analog 0,1 on + case ENABLE_THREE_ANALOG_INS: // analog 0-2 on + case ENABLE_FOUR_ANALOG_INS: // analog 0-3 on + case ENABLE_FIVE_ANALOG_INS: // analog 0-4 on + case ENABLE_SIX_ANALOG_INS: // analog 0-5 on + analogInputsEnabled = inputData - DISABLE_ALL_ANALOG_INS; + break; + case ENABLE_PWM: + waitForData = 2; // (pin#, dutyCycle) + executeMultiByteCommand = inputData; + break; + case DISABLE_PWM: + waitForData = 1; // pin# + executeMultiByteCommand = inputData; + break; + case SET_SOFTWARE_PWM_FREQ: + waitForData = 1; // pin# + executeMultiByteCommand = inputData; + break; + case ENABLE_SOFTWARE_PWM: + waitForData = 2; // (pin#, dutyCycle) + executeMultiByteCommand = inputData; + break; + case DISABLE_SOFTWARE_PWM: + waitForData = 1; // pin# + executeMultiByteCommand = inputData; + break; + case OUTPUT_TO_DIGITAL_PINS: // bytes to send to digital outputs + firstInputByte = true; + break; + } + } +} + + +// ========================================================================= + +// ------------------------------------------------------------------------- +void setup() { + byte i; + + beginSerial(9600); + for(i=0; i<TOTAL_DIGITAL_PINS; ++i) { + setPinMode(i,INPUT); + } +} + +// ------------------------------------------------------------------------- +void loop() { + checkForInput(); + + // read all digital pins, in enabled + if(digitalInputsEnabled) { + transmitDigitalInput(0); + checkForInput(); + transmitDigitalInput(7); + checkForInput(); + } + else if(analogInputsEnabled) { + // filler bytes, since the first thing sent is always the digitalInputs + printByte(0); + printByte(0); + checkForInput(); + } + + /* get analog in, for the number enabled + */ + for(analogPin=0; analogPin<analogInputsEnabled; ++analogPin) { + analogData = analogRead(analogPin); + // these two bytes get converted back into the whole number in Pd + // the higher bits should be zeroed so that the 8th bit doesn't get set + printByte(analogData >> 7); // bitshift the big stuff into the output byte + printByte(analogData % 128); // mod by 32 for the small byte + checkForInput(); + } + + /* end with the cycle marker, if any of the inputs are enabled */ + if( digitalInputsEnabled || analogInputsEnabled) { + printByte(255); + } +} diff --git a/arduino-help.pd b/arduino-help.pd index 7d49415..9c926ff 100644 --- a/arduino-help.pd +++ b/arduino-help.pd @@ -1,11 +1,11 @@ -#N canvas 202 94 654 546 10; +#N canvas 525 22 658 550 10; #X msg 156 146 open \$1; #N canvas 162 133 534 384 serin 0; -#X obj 120 61 cnv 15 15 15 empty \$0-number-canvas 3 4 8 0 14 -233017 +#X obj 120 61 cnv 15 15 15 empty \$0-number-canvas 1 4 8 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 0; +-1 1; #X obj 59 147 outlet; #X obj 60 13 inlet; #X msg 200 202 label \$1; @@ -28,12 +28,12 @@ #X obj 408 289 tgl 15 0 empty empty changes 0 -6 0 8 -262144 -1 -1 0 1; #X obj 278 29 hradio 15 1 0 14 empty empty empty 0 -6 0 8 -262131 -1 --1 0; +-1 11; #X obj 255 30 tgl 15 0 empty empty empty 0 -6 0 8 -262131 -1 -1 0 1 ; #X obj 355 292 tgl 15 0 empty empty all 0 -6 0 8 -262144 -1 -1 0 1 ; -#N canvas 100 289 626 345 decode 0; +#N canvas 100 289 630 349 decode 0; #X obj 241 26 inlet; #X obj 46 189 & 1; #X obj 72 189 & 2; @@ -109,7 +109,6 @@ #X obj 57 23 hsl 128 15 0 1 0 0 empty empty PWM_control_(0-1) -2 -6 1 10 -225271 -1 -1 0 0; #X msg 64 453 bang; -#X obj 255 274 arduino 3; #X text 387 15 7; #X text 282 15 0; #X text 428 15 10; @@ -131,7 +130,7 @@ #X obj 487 428 print GARBAGE; #X floatatom 430 488 6 0 0 0 - - -; #X text 393 56 how many analogIns to enable:; -#X obj 405 117 tgl 15 1 empty empty empty 0 -6 0 8 -257472 -1 -1 1 +#X obj 405 117 tgl 15 0 empty empty empty 0 -6 0 8 -257472 -1 -1 0 1; #X text 428 116 enable/disable digitalIns; #X msg 405 135 digitalIns \$1; @@ -246,7 +245,7 @@ the CPU; #X text 371 15 6; #X text 327 15 3; #X msg 255 71 outputMode \$2 \$1; -#N canvas 485 91 546 327 sending 0; +#N canvas 485 91 566 347 sending 0; #X obj 142 285 outlet; #X obj 129 84 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1 ; @@ -262,7 +261,6 @@ the CPU; ; #X obj 231 84 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1 ; -#X obj 168 125 bytemask; #X text 60 11 The 8th bit is not used in the bitmask \, there are 7 bits of digital output values per byte.; #X obj 300 85 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1 @@ -279,80 +277,82 @@ bits of digital output values per byte.; ; #X obj 402 85 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1 ; -#X obj 339 126 bytemask; #X obj 142 205 pack float float; -#X msg 142 231 255 \, \$1 \, \$2; #X text 143 65 pins 0-6; #X text 330 66 pins 7-13; #X obj 212 164 trigger bang float; -#X connect 1 0 8 0; -#X connect 2 0 8 1; -#X connect 3 0 8 2; -#X connect 4 0 8 3; -#X connect 5 0 8 4; -#X connect 6 0 8 5; -#X connect 7 0 8 6; -#X connect 8 0 18 0; -#X connect 10 0 17 0; -#X connect 11 0 17 1; -#X connect 12 0 17 2; -#X connect 13 0 17 3; -#X connect 14 0 17 4; -#X connect 15 0 17 5; -#X connect 16 0 17 6; -#X connect 17 0 22 0; -#X connect 18 0 19 0; -#X connect 19 0 0 0; -#X connect 22 0 18 0; -#X connect 22 1 18 1; +#X msg 142 231 229 \, \$1 \, \$2; +#X obj 299 126 mapping/bytemask; +#X obj 128 125 mapping/bytemask; +#X connect 1 0 22 0; +#X connect 2 0 22 1; +#X connect 3 0 22 2; +#X connect 4 0 22 3; +#X connect 5 0 22 4; +#X connect 6 0 22 5; +#X connect 7 0 22 6; +#X connect 9 0 21 0; +#X connect 10 0 21 1; +#X connect 11 0 21 2; +#X connect 12 0 21 3; +#X connect 13 0 21 4; +#X connect 14 0 21 5; +#X connect 15 0 21 6; +#X connect 16 0 20 0; +#X connect 19 0 16 0; +#X connect 19 1 16 1; +#X connect 20 0 0 0; +#X connect 21 0 19 0; +#X connect 22 0 16 0; #X restore 10 258 pd sending digital outs; -#X connect 0 0 14 0; +#X obj 255 274 arduino 3; +#X connect 0 0 62 0; #X connect 1 0 0 0; -#X connect 5 0 14 0; -#X connect 7 0 45 2; -#X connect 8 0 55 1; -#X connect 9 0 55 0; -#X connect 10 0 45 1; -#X connect 12 0 19 0; +#X connect 5 0 62 0; +#X connect 7 0 44 2; +#X connect 8 0 54 1; +#X connect 9 0 54 0; +#X connect 10 0 44 1; +#X connect 12 0 18 0; #X connect 13 0 6 0; -#X connect 14 0 25 0; -#X connect 14 1 45 0; -#X connect 18 0 14 0; -#X connect 19 0 18 0; -#X connect 20 0 53 0; -#X connect 21 0 53 0; -#X connect 22 0 53 0; -#X connect 23 0 14 0; -#X connect 24 0 54 0; -#X connect 25 0 27 0; -#X connect 26 0 25 1; -#X connect 27 0 44 0; -#X connect 27 1 44 1; -#X connect 27 2 44 2; -#X connect 27 3 44 3; -#X connect 27 4 44 4; -#X connect 27 5 44 5; -#X connect 27 6 44 6; -#X connect 28 0 11 0; -#X connect 28 0 30 0; -#X connect 28 0 39 0; -#X connect 28 1 29 0; -#X connect 32 0 34 0; -#X connect 34 0 14 0; -#X connect 35 0 14 0; -#X connect 36 0 35 0; -#X connect 39 0 41 0; -#X connect 40 0 39 1; -#X connect 42 0 14 1; -#X connect 44 0 47 0; -#X connect 44 1 48 0; -#X connect 44 2 49 0; -#X connect 44 3 50 0; -#X connect 44 4 51 0; -#X connect 44 5 52 0; -#X connect 44 6 28 0; -#X connect 53 0 19 1; -#X connect 54 0 23 0; -#X connect 55 0 61 0; -#X connect 61 0 14 0; -#X connect 62 0 14 0; +#X connect 17 0 62 0; +#X connect 18 0 17 0; +#X connect 19 0 52 0; +#X connect 20 0 52 0; +#X connect 21 0 52 0; +#X connect 22 0 62 0; +#X connect 23 0 53 0; +#X connect 24 0 26 0; +#X connect 25 0 24 1; +#X connect 26 0 43 0; +#X connect 26 1 43 1; +#X connect 26 2 43 2; +#X connect 26 3 43 3; +#X connect 26 4 43 4; +#X connect 26 5 43 5; +#X connect 26 6 43 6; +#X connect 27 0 11 0; +#X connect 27 0 29 0; +#X connect 27 0 38 0; +#X connect 27 1 28 0; +#X connect 31 0 33 0; +#X connect 33 0 62 0; +#X connect 34 0 62 0; +#X connect 35 0 34 0; +#X connect 38 0 40 0; +#X connect 39 0 38 1; +#X connect 41 0 62 1; +#X connect 43 0 46 0; +#X connect 43 1 47 0; +#X connect 43 2 48 0; +#X connect 43 3 49 0; +#X connect 43 4 50 0; +#X connect 43 5 51 0; +#X connect 43 6 27 0; +#X connect 52 0 18 1; +#X connect 53 0 22 0; +#X connect 54 0 60 0; +#X connect 60 0 62 0; +#X connect 61 0 62 0; +#X connect 62 0 24 0; +#X connect 62 1 44 0; @@ -1,4 +1,4 @@ -#N canvas 449 224 612 492 10; +#N canvas 158 31 628 508 10; #X text 414 457 released under the GNU GPL; #X text 9 457 (C) Copyright 2006 Hans-Christoph Steiner <hans@at.or.at> ; @@ -59,7 +59,7 @@ #X obj 126 425 outlet; #X obj 494 241 outlet; #X obj 11 66 comport \$1 9600; -#N canvas 528 330 562 307 command 0; +#N canvas 495 157 582 327 command 0; #X obj 79 8 inlet; #X obj 198 269 outlet; #X obj 135 115 * 255; @@ -72,47 +72,49 @@ #X msg 70 136 231 \, \$1; #X msg 5 136 230 \, \$1; #X obj 200 94 clip 0 6; -#X obj 200 135 + 240; #X obj 263 94 clip 0 1; #X obj 263 115 int; -#X obj 263 137 + 238; -#N canvas 0 22 458 308 digital-out 0; +#N canvas 0 22 462 312 digital-out 0; #X obj 67 20 inlet; #X obj 85 280 outlet; #X obj 65 108 route float; #X obj 221 191 & 127; #X obj 140 208 & 127; #X obj 140 164 trigger float float bang; -#X msg 303 186 255; #X text 104 89 break up digital-out bitmask; #X obj 140 186 >> 7; +#X msg 303 186 229; #X connect 0 0 2 0; #X connect 2 0 5 0; #X connect 2 1 1 0; #X connect 3 0 1 0; #X connect 4 0 1 0; -#X connect 5 0 8 0; +#X connect 5 0 7 0; #X connect 5 1 3 0; -#X connect 5 2 6 0; -#X connect 6 0 1 0; -#X connect 8 0 4 0; +#X connect 5 2 8 0; +#X connect 7 0 4 0; +#X connect 8 0 1 0; #X restore 431 94 pd digital-out; #X obj 80 53 route PWM0 PWM1 PWM2 analogIns digitalIns outputMode; -#N canvas 0 22 466 316 outputMode 0; +#N canvas 133 22 470 320 outputMode 0; #X obj 81 11 inlet; #X obj 105 275 outlet; #X obj 81 70 unpack float float; #X obj 105 176 +; -#X obj 178 126 * 14; -#X obj 105 197 + 200; +#X obj 105 197 + 130; +#X obj 178 126 * 70; +#X text 152 198 input commands = 130-143; +#X text 216 127 output commands = 200-213; #X connect 0 0 2 0; #X connect 2 0 3 0; -#X connect 2 1 4 0; -#X connect 3 0 5 0; -#X connect 4 0 3 1; -#X connect 5 0 1 0; +#X connect 2 1 5 0; +#X connect 3 0 4 0; +#X connect 4 0 1 0; +#X connect 5 0 3 1; #X restore 334 94 pd outputMode; -#X connect 0 0 17 0; +#X obj 200 135 + 160; +#X obj 263 137 + 150; +#X connect 0 0 15 0; #X connect 2 0 8 0; #X connect 3 0 9 0; #X connect 4 0 10 0; @@ -122,19 +124,19 @@ #X connect 8 0 1 0; #X connect 9 0 1 0; #X connect 10 0 1 0; -#X connect 11 0 12 0; -#X connect 12 0 1 0; -#X connect 13 0 14 0; -#X connect 14 0 15 0; -#X connect 15 0 1 0; +#X connect 11 0 17 0; +#X connect 12 0 13 0; +#X connect 13 0 18 0; +#X connect 14 0 1 0; +#X connect 15 0 7 0; +#X connect 15 1 6 0; +#X connect 15 2 5 0; +#X connect 15 3 11 0; +#X connect 15 4 12 0; +#X connect 15 5 16 0; +#X connect 15 6 14 0; #X connect 16 0 1 0; -#X connect 17 0 7 0; -#X connect 17 1 6 0; -#X connect 17 2 5 0; -#X connect 17 3 11 0; -#X connect 17 4 13 0; -#X connect 17 5 18 0; -#X connect 17 6 16 0; +#X connect 17 0 1 0; #X connect 18 0 1 0; #X restore 11 22 pd command processing; #X obj 66 132 moses 128; |