From 851f439038566e73bf77baec4e50ade1e5c0ef15 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Fri, 13 Apr 2007 03:55:16 +0000 Subject: sketched out EEPROM library support, but it doesn't work yet ; renamed some variables for clarity ; made digital pins set to OUTPUT by default so that you don't get lots of random data, unless you want it ;) svn path=/trunk/externals/hardware/arduino/; revision=7558 --- Pd_firmware/Makefile | 11 ++- Pd_firmware/Pd_firmware.pde | 172 ++++++++++++-------------------------------- 2 files changed, 54 insertions(+), 129 deletions(-) diff --git a/Pd_firmware/Makefile b/Pd_firmware/Makefile index d31c48a..82af842 100644 --- a/Pd_firmware/Makefile +++ b/Pd_firmware/Makefile @@ -43,16 +43,20 @@ # 7. Type "make upload", reset your Arduino board, and press enter to # upload your program to the Arduino board. # -# $Id: Makefile,v 1.5 2007-03-05 04:34:32 eighthave Exp $ +# $Id: Makefile,v 1.6 2007-04-13 03:55:16 eighthave Exp $ -PORT = /dev/tty.usbserial-1* +PORT = /dev/tty.usbserial-* TARGET = Pd_firmware ARDUINO = /Applications/arduino-0007 ARDUINO_SRC = $(ARDUINO)/lib/targets/arduino -INCLUDE = -I$(ARDUINO_SRC) -I$(ARDUINO)/tools/avr/avr/include +ARDUINO_LIB_SRC = $(ARDUINO)/lib/targets/libraries +INCLUDE = -I$(ARDUINO_SRC) -I$(ARDUINO)/tools/avr/avr/include \ + -I$(ARDUINO)/lib/targets/libraries/EEPROM \ + -I$(ARDUINO)/lib/targets/libraries SRC = $(ARDUINO_SRC)/pins_arduino.c $(ARDUINO_SRC)/wiring.c \ $(ARDUINO_SRC)/WInterrupts.c CXXSRC = applet/$(TARGET).cpp $(ARDUINO_SRC)/HardwareSerial.cpp \ + $(ARDUINO_LIB_SRC)/EEPROM/EEPROM.cpp \ $(ARDUINO_SRC)/WRandom.cpp MCU = atmega8 F_CPU = 16000000 @@ -215,6 +219,7 @@ clean: $(REMOVE) applet/$(TARGET).hex applet/$(TARGET).eep applet/$(TARGET).cof applet/$(TARGET).elf \ $(TARGET).map applet/$(TARGET).sym applet/$(TARGET).lss \ $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d) $(CXXSRC:.cpp=.s) $(CXXSRC:.cpp=.d) + find $(ARDUINO)/lib/ -name '*.o' -delete depend: if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \ diff --git a/Pd_firmware/Pd_firmware.pde b/Pd_firmware/Pd_firmware.pde index 6c196af..111d8e4 100644 --- a/Pd_firmware/Pd_firmware.pde +++ b/Pd_firmware/Pd_firmware.pde @@ -39,6 +39,8 @@ * @locations: STEIM, Amsterdam, Netherlands * IDMI/Polytechnic University, Brookyn, NY, USA * Electrolobby Ars Electronica, Linz, Austria + * + * See http://www.arduino.cc/playground/Interfacing/Firmata for docs */ /* @@ -50,119 +52,9 @@ * TODO: use Program Control to load stored profiles from EEPROM */ -/* cvs version: $Id: Pd_firmware.pde,v 1.29 2007-03-08 05:37:22 eighthave Exp $ */ - -/*============================================================================== - * MESSAGE FORMATS - *============================================================================*/ - -/* ----------------------------------------------------------------------------- - * MAPPING DATA TO MIDI - * - * This protocol uses the MIDI message format, but does not use the whole - * protocol. Most of the command mappings here will not be directly usable in - * terms of MIDI controllers and synths. It should co-exist with MIDI without - * trouble and can be parsed by standard MIDI interpreters. Just some of the - * message data is used differently. - * - * MIDI format: http://www.harmony-central.com/MIDI/Doc/table1.html - * - * MIDI - * type command channel first byte second byte - * ----------------------------------------------------------------------------- - * analog I/O 0xE0 pin # LSB(bits 0-6) MSB(bits 7-13) - * digital I/O 0x90 port base LSB(bits 0-6) MSB(bits 7-13) - * report analog pin 0xC0 pin # disable/enable(0/1) - n/a - - * report digital ports 0xD0 port base disable/enable(0/1) - n/a - - * - * digital pin mode(I/O) 0xF4 - n/a - pin # (0-63) pin state(0=in) - * firmware version 0xF9 - n/a - minor version major version - * system reset 0xFF - n/a - - n/a - - n/a - - * - */ - -/* proposed extensions using SysEx - * - * type SysEx start command data bytes SysEx stop - * ----------------------------------------------------------------------------- - * pulse I/O 0xF0 0xA0 five 7-bit chunks, LSB first 0xF7 - * shiftOut 0xF0 0xF5 dataPin; clockPin; 7-bit LSB; 7-bit MSB 0xF7 - */ - -/* ----------------------------------------------------------------------------- - * DATA MESSAGE FORMAT */ +/* cvs version: $Id: Pd_firmware.pde,v 1.30 2007-04-13 03:55:16 eighthave Exp $ */ -/* two byte digital data format - * ---------------------------- - * 0 digital data, 0x90-0x9F, (MIDI NoteOn, but different data usage) - * 1 digital pins 0-6 bitmask - * 2 digital pins 7-13 bitmask - */ - -/* analog 14-bit data format - * ------------------------- - * 0 analog pin, 0xE0-0xEF, (MIDI Pitch Wheel) - * 1 analog least significant 7 bits - * 2 analog most significant 7 bits - */ - -/* version report format - * Send a single byte 0xF9, Arduino will reply with: - * ------------------------------------------------- - * 0 version report header (0xF9) (MIDI Undefined) - * 1 minor version (0-127) - * 2 major version (0-127) - */ - -/* pulseIn/Out (uses 32-bit value) - * ------------------------------- - * 0 START_SYSEX (0xF0) (MIDI System Exclusive) - * 1 pulseIn/Out (0xA0-0xAF) - * 2 bits 0-6 (least significant byte) - * 3 bits 7-13 - * 4 bits 14-20 - * 5 bits 21-27 - * 6 bits 28-34 (most significant byte) - * 7 END_SYSEX (0xF7) (MIDI End of SysEx - EOX) - */ - -/* shiftIn/Out (uses 8-bit value) - * ------------------------------ - * 0 START_SYSEX (0xF0) - * 1 shiftOut (0xF5) - * 2 dataPin (0-127) - * 3 clockPin (0-127) - * 4 bits 0-6 (least significant byte) - * 5 bit 7 (most significant bit) - * 6 END_SYSEX (0xF7) - */ - -/* ----------------------------------------------------------------------------- - * CONTROL MESSAGES */ - -/* set digital pin mode - * -------------------- - * 1 set digital pin mode (0xF4) (MIDI Undefined) - * 2 pin number (0-127) - * 3 state (INPUT/OUTPUT, 0/1) - */ - -/* toggle analogIn reporting by pin - * -------------------------------- - * 0 toggle digitalIn reporting (0xC0-0xCF) (MIDI Program Change) - * 1 disable(0)/enable(non-zero) - */ - -/* toggle digitalIn reporting by port pairs - * ---------------------------------------- - * 0 toggle digitalIn reporting (0xD0-0xDF) (MIDI Aftertouch) - * 1 disable(0)/enable(non-zero) - */ - -/* request version report - * ---------------------- - * 0 request version report (0xF9) (MIDI Undefined) - */ +#include /*============================================================================== * MACROS @@ -175,7 +67,7 @@ #define FIRMATA_MAJOR_VERSION 1 // for non-compatible changes #define FIRMATA_MINOR_VERSION 0 // for backwards compatible changes -/* total number of pins currently supported */ +// total number of pins currently supported #define TOTAL_ANALOG_PINS 6 #define TOTAL_DIGITAL_PINS 14 @@ -188,7 +80,7 @@ #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 */ +// message command bytes #define DIGITAL_MESSAGE 0x90 // send data for a digital pin #define ANALOG_MESSAGE 0xE0 // send data for an analog pin (or PWM) //#define PULSE_MESSAGE 0xA0 // proposed pulseIn/Out message (SysEx) @@ -201,6 +93,15 @@ #define REPORT_VERSION 0xF9 // report firmware version #define SYSTEM_RESET 0xFF // reset from MIDI +// these are used for EEPROM reading and writing +#define ANALOGINPUTSTOREPORT_LOW_BYTE 0x1F0 // analogInputsToReport is an int +#define ANALOGINPUTSTOREPORT_HIGH_BYTE 0x1F1 // analogInputsToReport is an int +#define REPORTDIGITALINPUTS_BYTE 0x1F2 // +#define DIGITALPINSTATUS_LOW_BYTE 0x1F3 // digitalPinStatus is an int +#define DIGITALPINSTATUS_HIGH_BYTE 0x1F4 // digitalPinStatus is an int +#define PWMSTATUS_LOW_BYTE 0x1F5 // pwmStatus is an int +#define PWMSTATUS_HIGH_BYTE 0x1F6 // pwmStatus is an int + /*============================================================================== * GLOBAL VARIABLES *============================================================================*/ @@ -211,14 +112,14 @@ 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 +boolean reportDigitalInputs = false; // output digital inputs or not int digitalInputs; int previousDigitalInputs; // previous output to test for change -int digitalPinStatus = 3; // bitwise array to store pin status, ignore RxTx pins +unsigned int digitalPinStatus = 65535; // store pin status, default OUTPUT /* PWM/analog outputs */ int pwmStatus = 0; // bitwise array to store PWM status /* analog inputs */ -unsigned int analogPinsToReport = 0; // bitwise array to store pin reporting +int analogInputsToReport = 0; // bitwise array to store pin reporting int analogPin = 0; // counter for reading analog pins int analogData; // storage variable for data from analogRead() /* timer variables */ @@ -229,7 +130,7 @@ unsigned long nextExecuteTime; // for comparison with timer0_overflow_count * FUNCTIONS *============================================================================*/ /* ----------------------------------------------------------------------------- - * output the version message to the serial port */ + * output the protocol version message to the serial port */ void printVersion() { Serial.print(REPORT_VERSION, BYTE); Serial.print(FIRMATA_MINOR_VERSION, BYTE); @@ -257,7 +158,7 @@ void outputDigitalBytes(byte pin0_6, byte pin7_13) { * check all the active digital inputs for change of state, then add any events * to the Serial output queue using Serial.print() */ void checkDigitalInputs(void) { - if(digitalInputsEnabled) { + if(reportDigitalInputs) { previousDigitalInputs = digitalInputs; digitalInputs = _SFR_IO8(port_to_input[PB]) << 8; // get pins 8-13 digitalInputs += _SFR_IO8(port_to_input[PD]); // get pins 0-7 @@ -302,10 +203,10 @@ void setPinMode(byte pin, byte mode) { */ void setAnalogPinReporting(byte pin, byte state) { if(state == 0) { - analogPinsToReport = analogPinsToReport &~ (1 << pin); + analogInputsToReport = analogInputsToReport &~ (1 << pin); } else { // everything but 0 enables reporting of that pin - analogPinsToReport = analogPinsToReport | (1 << pin); + analogInputsToReport = analogInputsToReport | (1 << pin); } // TODO: save status to EEPROM here, if changed } @@ -333,7 +234,7 @@ void processInput(int inputData) { case SET_DIGITAL_PIN_MODE: setPinMode(storedInputData[1], storedInputData[0]); // (pin#, mode) if(storedInputData[0] == INPUT) - digitalInputsEnabled = true; // enable reporting of digital inputs + reportDigitalInputs = true; // enable reporting of digital inputs break; case REPORT_ANALOG_PIN: setAnalogPinReporting(multiByteChannel,storedInputData[0]); @@ -341,9 +242,9 @@ void processInput(int inputData) { case REPORT_DIGITAL_PORTS: // TODO: implement MIDI channel as port base for more than 16 digital inputs if(storedInputData[0] == 0) - digitalInputsEnabled = false; + reportDigitalInputs = false; else - digitalInputsEnabled = true; + reportDigitalInputs = true; break; } executeMultiByteCommand = 0; @@ -388,6 +289,25 @@ void checkForSerialReceive() { processInput(Serial.read()); } +/* ----------------------------------------------------------------------------- + * these functions are for loading and saving the state of the digital pins and + * pin reporting so that the Arduino will start up again in the same state. The + * EEPROM is supposed to have a life of at least 100,000 writes. + */ +void loadSettings() { + //EEPROM.read(); +} + +void saveSettings() { + EEPROM.write(ANALOGINPUTSTOREPORT_LOW_BYTE, analogInputsToReport & 0xFF); + EEPROM.write(ANALOGINPUTSTOREPORT_HIGH_BYTE, analogInputsToReport >> 8); + EEPROM.write(REPORTDIGITALINPUTS_BYTE, reportDigitalInputs & 0xFF); + EEPROM.write(DIGITALPINSTATUS_LOW_BYTE, digitalPinStatus & 0xFF); + EEPROM.write(DIGITALPINSTATUS_HIGH_BYTE, digitalPinStatus >> 8); + EEPROM.write(PWMSTATUS_LOW_BYTE, pwmStatus & 0xFF); + EEPROM.write(PWMSTATUS_HIGH_BYTE, pwmStatus >> 8); +} + // ============================================================================= // used for flashing the pin for the version number void pin13strobe(int count, int onInterval, int offInterval) { @@ -422,7 +342,7 @@ void setup() { pin13strobe(2,1,4); // separator, a quick burst for(i=0; i