[PD-cvs] externals/hardware/arduino/Pd_firmware Pd_firmware.pde, 1.2, 1.3

Hans-Christoph Steiner eighthave at users.sourceforge.net
Sat May 20 12:18:16 CEST 2006


Update of /cvsroot/pure-data/externals/hardware/arduino/Pd_firmware
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6109/Pd_firmware

Modified Files:
	Pd_firmware.pde 
Log Message:
got PWM control working from Pd

Index: Pd_firmware.pde
===================================================================
RCS file: /cvsroot/pure-data/externals/hardware/arduino/Pd_firmware/Pd_firmware.pde,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** Pd_firmware.pde	19 May 2006 12:54:11 -0000	1.2
--- Pd_firmware.pde	20 May 2006 10:18:14 -0000	1.3
***************
*** 15,19 ****
   * Pduino protocol
   * =============== 
!  * data: 0-127   control: 128-255
   * 
   * Pd->Arduino commands
--- 15,20 ----
   * Pduino protocol
   * =============== 
!  * data: 0-127   
!  * control: 128-255
   * 
   * Pd->Arduino commands
***************
*** 24,34 ****
   * 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
--- 25,34 ----
   * 231 - next byte sets PWM1 value
   * 232 - next byte sets PWM2 value
   *
   * Pd->Arduino byte cycle
   * ----------------------
!  * 0  digitalOut 0-6 bitmask
!  * 1  digitalOut 7-13 bitmask
!  * 2  cycle marker (255/11111111)
   * 
   * Arduino->Pd byte cycle
***************
*** 64,71 ****
  #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
--- 64,75 ----
  #define TOTAL_DIGITAL_PINS 14
  
! // for comparing along with INPUT and OUTPUT
! #define PWM 2
  
! // this flag says the next serial input will be data, not control
! boolean waitForData = false;
! 
! // 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
***************
*** 74,88 ****
  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;
--- 78,99 ----
  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;
+ 
  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;
***************
*** 91,110 ****
        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);
    }
  }
--- 102,135 ----
        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);
!       digitalData = pwmStatus;
      }
!     transmitByte = transmitByte + (2^(i+1-startPin)*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);
    }
  }
***************
*** 121,183 ****
  // -------------------------------------------------------------------------
  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;
      }
    }
--- 146,207 ----
  // -------------------------------------------------------------------------
  void processInput(int inputData) {
+   // the PWM commands (230-232) have a byte of data following the command
    if (waitForData > 0) {  
!     printByte(150);
!     printByte(inputData);
      analogWrite(waitForData,inputData);
      waitForData = 0;
    }
+   else if(inputData < 128) {
+     printByte(151);
+     printByte(inputData);
+ // TODO: this section is for digital out...    
+   }
    else {
+     printByte(153);
      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:
!       printByte(inputData);
!       setPinMode(inputData-200,OUTPUT);
!       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:
!       printByte(inputData);
!       setPinMode(inputData-214,INPUT);
!       break;
!     case 230:
!     case 231:
!     case 232:
!       printByte(inputData);
!       waitForData = inputData - 221; // set waitForData to the PWM pin number
!       setPinMode(waitForData, PWM);
!       break;
!     case 255:
!       break;
      }
    }
***************
*** 188,194 ****
  // -------------------------------------------------------------------------
  void setup() {
    beginSerial(9600);
    for(i=0; i<TOTAL_DIGITAL_PINS; ++i) {
!       setPinMode(i,INPUT);
    }
  }
--- 212,220 ----
  // -------------------------------------------------------------------------
  void setup() {
+   byte i;
+ 
    beginSerial(9600);
    for(i=0; i<TOTAL_DIGITAL_PINS; ++i) {
!     setPinMode(i,INPUT);
    }
  }
***************
*** 197,216 ****
  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
--- 223,242 ----
  void loop() {
    // read all digital pins
!   transmitDigitalInput(0);
!   transmitDigitalInput(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
***************
*** 220,224 ****
  
    checkForInput();
!   
    printByte(255);   
  }
--- 246,250 ----
  
    checkForInput();
! 
    printByte(255);   
  }





More information about the Pd-cvs mailing list