[PD-cvs] externals/hardware/arduino/Pd_firmware Pd_firmware.pde, 1.23, 1.24

Hans-Christoph Steiner eighthave at users.sourceforge.net
Thu Feb 22 07:16:45 CET 2007


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

Modified Files:
	Pd_firmware.pde 
Log Message:

- got analog input working
- version reporting works, but checkInput() isn't run enough, so it misses the
  data if there is a lot of traffic on the serial port


Index: Pd_firmware.pde
===================================================================
RCS file: /cvsroot/pure-data/externals/hardware/arduino/Pd_firmware/Pd_firmware.pde,v
retrieving revision 1.23
retrieving revision 1.24
diff -C2 -d -r1.23 -r1.24
*** Pd_firmware.pde	20 Feb 2007 06:25:56 -0000	1.23
--- Pd_firmware.pde	22 Feb 2007 06:16:43 -0000	1.24
***************
*** 169,173 ****
  #define MINOR_VERSION 0  // for backwards compatible changes
  
! /* total number of digital pins supported */  
  #define TOTAL_DIGITAL_PINS 14
  
--- 169,174 ----
  #define MINOR_VERSION 0  // for backwards compatible changes
  
! /* total number of pins currently supported */  
! #define TOTAL_ANALOG_PINS 16
  #define TOTAL_DIGITAL_PINS 14
  
***************
*** 207,210 ****
--- 208,214 ----
  int pwmStatus = 0;
  
+ /* bit-wise array to store pin reporting */
+ unsigned int analogPinsToReport = 65536;
+ 
  
  /*==========================================================================
***************
*** 216,230 ****
   */
  void outputDigitalBytes(byte pin0_6, byte pin7_13) {
! 	int i;
! 	int mask;
! 	int twoBytesForPorts;
! 	
! 	twoBytesForPorts = pin0_6 + (pin7_13 << 7);
! 	for(i=0; i<14; ++i) {
! 		mask = 1 << i;
! 		if( (digitalPinStatus & mask) && !(pwmStatus & mask) ) {
! 			digitalWrite(i, twoBytesForPorts & mask);
! 		} 
! 	}
  }
  
--- 220,234 ----
   */
  void outputDigitalBytes(byte pin0_6, byte pin7_13) {
!   int i;
!   int mask;
!   int twoBytesForPorts;
!     
!   twoBytesForPorts = pin0_6 + (pin7_13 << 7);
!   for(i=0; i<14; ++i) {
!     mask = 1 << i;
!     if( (digitalPinStatus & mask) && !(pwmStatus & mask) ) {
!       digitalWrite(i, twoBytesForPorts & mask);
!     } 
!   }
  }
  
***************
*** 234,291 ****
   */
  void processInput(int inputData) {
! 	int command, channel;
  
! 	// a few commands have byte(s) of data following the command
! 	if( (waitForData > 0) && (inputData < 128) ) {  
! 		waitForData--;
! 		storedInputData[waitForData] = inputData;
! 		if( (waitForData==0) && executeMultiByteCommand ) {
! 			//we got everything
! 			switch(executeMultiByteCommand) {
! 			case ANALOG_MESSAGE:
! 				channel = inputData & 0x0F; // get channel from command byte
! 				break;
! 			case DIGITAL_MESSAGE:
! 				outputDigitalBytes(storedInputData[1], storedInputData[0]);
! 				break;
! 			case SET_DIGITAL_PIN_MODE:
! 				setPinMode(storedInputData[1], storedInputData[0]);
! 				break;
! 			case REPORT_ANALOG_PIN:
! 				break;
! 			case REPORT_DIGITAL_PORTS:
! 				break;
! 			}
! 			executeMultiByteCommand = 0;
! 		}
! 	} else {
! 		// remove channel info from command byte if less than 0xF0
! 		if(inputData < 0xF0) {
! 			command = inputData & 0xF0;
! 		} else {
! 			command = inputData;
! 		}
! 		switch (inputData) {
! 		case ANALOG_MESSAGE:
! 		case DIGITAL_MESSAGE:
! 		case SET_DIGITAL_PIN_MODE:
! 			waitForData = 2; // two data bytes needed
! 			executeMultiByteCommand = inputData;
! 			break;
! 		case REPORT_ANALOG_PIN:
! 		case REPORT_DIGITAL_PORTS:
! 			waitForData = 1; // two data bytes needed
! 			executeMultiByteCommand = inputData;
! 			break;
! 		case SYSTEM_RESET:
! 			// this doesn't do anything yet
! 			break;
! 		case REPORT_VERSION:
! 			Serial.print(REPORT_VERSION, BYTE);
! 			Serial.print(MAJOR_VERSION, BYTE);
! 			Serial.print(MINOR_VERSION, BYTE);
! 			break;
! 		}
! 	}
  }
  
--- 238,295 ----
   */
  void processInput(int inputData) {
!   int command, channel;
  
!   // a few commands have byte(s) of data following the command
!   if( (waitForData > 0) && (inputData < 128) ) {  
!     waitForData--;
!     storedInputData[waitForData] = inputData;
!     if( (waitForData==0) && executeMultiByteCommand ) {
!       //we got everything
!       switch(executeMultiByteCommand) {
!       case ANALOG_MESSAGE:
!         channel = inputData & 0x0F; // get channel from command byte
!         break;
!       case DIGITAL_MESSAGE:
!         outputDigitalBytes(storedInputData[1], storedInputData[0]);
!         break;
!       case SET_DIGITAL_PIN_MODE:
!         setPinMode(storedInputData[1], storedInputData[0]);
!         break;
!       case REPORT_ANALOG_PIN:
!         break;
!       case REPORT_DIGITAL_PORTS:
!         break;
!       }
!       executeMultiByteCommand = 0;
!     }
!   } else {
!     // remove channel info from command byte if less than 0xF0
!     if(inputData < 0xF0) {
!       command = inputData & 0xF0;
!     } else {
!       command = inputData;
!     }
!     switch (inputData) {
!     case ANALOG_MESSAGE:
!     case DIGITAL_MESSAGE:
!     case SET_DIGITAL_PIN_MODE:
!       waitForData = 2; // two data bytes needed
!       executeMultiByteCommand = inputData;
!       break;
!     case REPORT_ANALOG_PIN:
!     case REPORT_DIGITAL_PORTS:
!       waitForData = 1; // two data bytes needed
!       executeMultiByteCommand = inputData;
!       break;
!     case SYSTEM_RESET:
!       // this doesn't do anything yet
!       break;
!     case REPORT_VERSION:
!       Serial.print(REPORT_VERSION, BYTE);
!       Serial.print(MINOR_VERSION, BYTE);
!       Serial.print(MAJOR_VERSION, BYTE);
!       break;
!     }
!   }
  }
  
***************
*** 301,306 ****
   */
  void checkForInput() {
! 	if(Serial.available())
! 		processInput( Serial.read() );
  }
  
--- 305,310 ----
   */
  void checkForInput() {
!   if(Serial.available())
!     processInput( Serial.read() );
  }
  
***************
*** 310,329 ****
   */
  void setPinMode(byte pin, byte 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 ) {
! 		digitalPinStatus = digitalPinStatus | (1 << pin);
! 		pwmStatus = pwmStatus | (1 << pin);
! 		pinMode(pin,OUTPUT);
! 	}
! // TODO: save status to EEPROM here, if changed
  }
  
--- 314,333 ----
   */
  void setPinMode(byte pin, byte 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 ) {
!     digitalPinStatus = digitalPinStatus | (1 << pin);
!     pwmStatus = pwmStatus | (1 << pin);
!     pinMode(pin,OUTPUT);
!   }
!   // TODO: save status to EEPROM here, if changed
  }
  
***************
*** 332,342 ****
  // used for flashing the pin for the version number
  void pin13strobe(int count, int onInterval, int offInterval) {
! 	byte i;
! 	for(i=0; i<count; i++) {
! 		digitalWrite(13,1);
! 		delay(onInterval);
! 		digitalWrite(13,0);
! 		delay(offInterval);
! 	}
  }
  
--- 336,346 ----
  // used for flashing the pin for the version number
  void pin13strobe(int count, int onInterval, int offInterval) {
!   byte i;
!   for(i=0; i<count; i++) {
!     digitalWrite(13,1);
!     delay(onInterval);
!     digitalWrite(13,0);
!     delay(offInterval);
!   }
  }
  
***************
*** 345,372 ****
   *==========================================================================*/
  void setup() {
! 	byte i;
  
! // TODO: load state from EEPROM here
! 	Serial.begin(115200); // 9600, 14400, 38400, 57600, 115200
  
! /* TODO: send digital inputs here, if enabled, to set the initial state on the
!  * host computer, since once in the loop(), the Arduino will only send data on
!  * change. */
  
! // flash the pin 13 with the protocol minor version (add major once > 0)
! 	pinMode(13,OUTPUT);
! 	pin13strobe(10,5,20); // separator, a quick burst
! 	delay(500);
! 	pin13strobe(MAJOR_VERSION, 200, 400);
! 	delay(500);
! 	pin13strobe(10,5,20); // separator, a quick burst
! 	delay(500);
! 	pin13strobe(MINOR_VERSION, 200, 400);
! 	delay(500);
! 	pin13strobe(10,5,20); // separator, a quick burst
! 	delay(1000);
! 	for(i=0; i<TOTAL_DIGITAL_PINS; ++i) {
! 		setPinMode(i,OUTPUT);
! 	}
  }
  
--- 349,376 ----
   *==========================================================================*/
  void setup() {
!   byte i;
  
!   // TODO: load state from EEPROM here
!   Serial.begin(115200); // 9600, 14400, 38400, 57600, 115200
  
!   /* TODO: send digital inputs here, if enabled, to set the initial state on the
!    * host computer, since once in the loop(), the Arduino will only send data on
!    * change. */
  
!   // flash the pin 13 with the protocol minor version (add major once > 0)
!   pinMode(13,OUTPUT);
!   pin13strobe(10,5,20); // separator, a quick burst
!   delay(500);
!   pin13strobe(MAJOR_VERSION, 200, 400);
!   delay(500);
!   pin13strobe(10,5,20); // separator, a quick burst
!   delay(500);
!   pin13strobe(MINOR_VERSION, 200, 400);
!   delay(500);
!   pin13strobe(10,5,20); // separator, a quick burst
!   delay(1000);
!   for(i=0; i<TOTAL_DIGITAL_PINS; ++i) {
!     setPinMode(i,OUTPUT);
!   }
  }
  
***************
*** 375,379 ****
   *==========================================================================*/
  void loop() {
! 
! 	checkForInput();
  }
--- 379,399 ----
   *==========================================================================*/
  void loop() {
!   int i; // counter for analog pins
!   int analogData;
!     
!   checkForInput();
!   /* get analog in, for the number enabled */
!   for(i=0; i<TOTAL_ANALOG_PINS; ++i) {
!     checkForInput();
!     //if( analogPinsToReport & (1 << i) ) {
!       analogData = analogRead(i);
!       /* These two bytes get converted back into the whole number on host.
!          Highest bits should be zeroed so the 8th bit doesn't get set */
!       checkForInput();
!       Serial.print(ANALOG_MESSAGE + i, BYTE);
!       Serial.print(analogData % 128, BYTE); // mod by 32 for the small byte
!       Serial.print(analogData >> 7, BYTE); // shift high bits into output byte
! 	  //}
!     checkForInput();
!   }
  }





More information about the Pd-cvs mailing list