From 7aa756cc6833a36d27bac43a7c6bd32756c5c33b Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Tue, 6 Mar 2007 06:59:43 +0000 Subject: - got digital inputs working reading by port, sends only on change - ignore Rx/Tx pins (0 and 1) since they are used for serial/USB - removed a fair amount of cruft svn path=/trunk/externals/hardware/arduino/; revision=7469 --- Pd_firmware/Pd_firmware.pde | 67 +++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 27 deletions(-) (limited to 'Pd_firmware/Pd_firmware.pde') diff --git a/Pd_firmware/Pd_firmware.pde b/Pd_firmware/Pd_firmware.pde index ca8c79a..e678917 100644 --- a/Pd_firmware/Pd_firmware.pde +++ b/Pd_firmware/Pd_firmware.pde @@ -51,7 +51,7 @@ * TODO: use Program Control to load stored profiles from EEPROM */ -/* cvs version: $Id: Pd_firmware.pde,v 1.27 2007-03-05 17:08:51 eighthave Exp $ */ +/* cvs version: $Id: Pd_firmware.pde,v 1.28 2007-03-06 06:59:43 eighthave Exp $ */ /*============================================================================== * MESSAGE FORMATS @@ -183,6 +183,11 @@ // for comparing along with INPUT and OUTPUT #define PWM 2 +// for selecting digital inputs +#define PB 2 // digital input, pins 8-13 +#define PC 3 // analog input port +#define PD 4 // digital input, pins 0-7 + #define MAX_DATA_BYTES 2 // max number of data bytes in non-SysEx messages /* message command bytes */ #define DIGITAL_MESSAGE 0x90 // send data for a digital pin @@ -203,16 +208,14 @@ /* input message handling */ byte waitForData = 0; // this flag says the next serial input will be data -byte executeMultiByteCommand = 0; // command to execute after getting multi-byte data +byte executeMultiByteCommand = 0; // execute this after getting multi-byte data byte multiByteChannel = 0; // channel data for multiByteCommands byte storedInputData[MAX_DATA_BYTES] = {0,0}; // multi-byte data /* digital pins */ boolean digitalInputsEnabled = false; // output digital inputs or not -byte digitalInputHighByte = 0; -byte digitalInputLowByte = 0; -byte previousDigitalInputHighByte = 0; // previous output to test for change -byte previousDigitalInputLowByte = 0; // previous output to test for change -int digitalPinStatus = 0; // bitwise array to store pin status 0=INPUT,1=OUTPUT +int digitalInputs; +int previousDigitalInputs; // previous output to test for change +int digitalPinStatus = 3; // bitwise array to store pin status, ignore RxTx pins /* PWM/analog outputs */ int pwmStatus = 0; // bitwise array to store PWM status /* analog inputs */ @@ -243,7 +246,7 @@ void outputDigitalBytes(byte pin0_6, byte pin7_13) { // this should be converted to use PORTs twoBytesForPorts = pin0_6 + (pin7_13 << 7); - for(i=0; i<14; ++i) { + for(i=2; i> 7, BYTE); // Tx pins 7-13 + } } } @@ -265,23 +277,25 @@ void checkDigitalInputs(void) { * two bit-arrays that track Digital I/O and PWM status */ void setPinMode(byte pin, byte mode) { - if(mode == INPUT) { - digitalPinStatus = digitalPinStatus &~ (1 << pin); - pwmStatus = pwmStatus &~ (1 << pin); - digitalWrite(pin,LOW); // turn off pin before switching to INPUT - 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); - } + if(pin > 1) { // ignore RxTx pins (0,1) + if(mode == INPUT) { + digitalPinStatus = digitalPinStatus &~ (1 << pin); + pwmStatus = pwmStatus &~ (1 << pin); + digitalWrite(pin,LOW); // turn off pin before switching to INPUT + 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 + } } // ----------------------------------------------------------------------------- @@ -424,12 +438,11 @@ void setup() { * LOOP() *============================================================================*/ void loop() { - ///analogWrite(11, tmp);++tmp;delay(2); /* DIGITALREAD - as fast as possible, check for changes and output them to the * FTDI buffer using Serial.print() */ checkDigitalInputs(); if(timer0_overflow_count > nextExecuteTime) { - nextExecuteTime = timer0_overflow_count + 4; // run this every 4ms + nextExecuteTime = timer0_overflow_count + 19; // run this every 20ms /* SERIALREAD - Serial.read() uses a 128 byte circular buffer, so handle * all serialReads at once, i.e. empty the buffer */ checkForSerialReceive(); -- cgit v1.2.1