|
-
-
-
-
-
-
-
-
- #include <EthernetV2_0.h> //<EthernetV1_0.h>
- #include <EthernetUdpV2_0.h> //<EthernetUdpV1_0.h>
- #include <SPI.h>
- #include <OSCBundle.h>
- #include <OSCBoards.h>
-
- EthernetUDP Udp;
-
-
- IPAddress ip(128, 32, 122, 252);
-
-
- const unsigned int inPort = 8888;
- const unsigned int outPort = 9999;
-
-
- #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__)
-
- static byte mac[6];
- void read(uint8_t word, uint8_t *mac, uint8_t offset) {
- FTFL_FCCOB0 = 0x41;
- FTFL_FCCOB1 = word;
-
-
- FTFL_FSTAT = FTFL_FSTAT_CCIF;
- while(!(FTFL_FSTAT & FTFL_FSTAT_CCIF));
-
- *(mac+offset) = FTFL_FCCOB5;
- *(mac+offset+1) = FTFL_FCCOB6;
- *(mac+offset+2) = FTFL_FCCOB7;
- }
- void read_mac() {
- read(0xe,mac,0);
- read(0xf,mac,3);
- }
- #else
- void read_mac() {}
- byte mac[] = {
- 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- #endif
-
-
-
-
- OSCBundle bundleOUT;
-
-
- char * numToOSCAddress( int pin){
- static char s[10];
- int i = 9;
-
- s[i--]= '\0';
- do
- {
- s[i] = "0123456789"[pin % 10];
- --i;
- pin /= 10;
- }
- while(pin && i);
- s[i] = '/';
- return &s[i];
- }
-
-
-
-
-
- void routeDigital(OSCMessage &msg, int addrOffset ){
-
- for(byte pin = 0; pin < NUM_DIGITAL_PINS; pin++){
-
- int pinMatched = msg.match(numToOSCAddress(pin), addrOffset);
- if(pinMatched){
-
- if (msg.isInt(0)){
- pinMode(pin, OUTPUT);
- digitalWrite(pin, (msg.getInt(0)>0) ? HIGH:LOW);
- }
- else if(msg.isFloat(0)){
- analogWrite(pin, (int)(msg.getFloat(0)*255.0f));
- }
-
-
-
-
- else if (msg.fullMatch("/u", pinMatched+addrOffset)){
-
- pinMode(pin, INPUT_PULLUP);
-
- char outputAddress[9];
- strcpy(outputAddress, "/d");
- strcat(outputAddress, numToOSCAddress(pin));
- strcat(outputAddress,"/u");
-
- bundleOUT.add(outputAddress).add(digitalRead(pin));
- }
- else {
-
- pinMode(pin, INPUT);
-
- char outputAddress[6];
- strcpy(outputAddress, "/d");
- strcat(outputAddress, numToOSCAddress(pin));
-
- bundleOUT.add(outputAddress).add(digitalRead(pin));
- }
- }
- }
- }
-
-
-
- void routeAnalog(OSCMessage &msg, int addrOffset ){
-
- for(byte pin = 0; pin < NUM_ANALOG_INPUTS; pin++){
-
- int pinMatched = msg.match(numToOSCAddress(pin), addrOffset);
- if(pinMatched){
-
- if (msg.isInt(0)){
- pinMode(analogInputToDigitalPin(pin), OUTPUT);
- digitalWrite(analogInputToDigitalPin(pin), (msg.getInt(0) > 0)? HIGH: LOW);
- }
- else if(msg.isFloat(0)){
- analogWrite(pin, (int)(msg.getFloat(0)*255.0f));
- }
- #ifdef BOARD_HAS_ANALOG_PULLUP
-
- else if (msg.fullMatch("/u", pinMatched+addrOffset)){
-
-
- pinMode(analogInputToDigitalPin(pin), INPUT_PULLUP);
-
-
- char outputAddress[9];
- strcpy(outputAddress, "/a");
- strcat(outputAddress, numToOSCAddress(pin));
- strcat(outputAddress,"/u");
- strcat(outputAddress,"/u");
-
- bundleOUT.add(outputAddress).add((int32_t)analogRead(pin));
- }
- #endif
- else {
-
-
- pinMode(analogInputToDigitalPin(pin), INPUT);
-
- char outputAddress[6];
- strcpy(outputAddress, "/a");
- strcat(outputAddress, numToOSCAddress(pin));
-
- bundleOUT.add(outputAddress).add((int32_t)analogRead(pin));
- }
- }
- }
- }
-
- #ifdef BOARD_HAS_TONE
-
-
-
- void routeTone(OSCMessage &msg, int addrOffset ){
-
- for(byte pin = 0; pin < NUM_DIGITAL_PINS; pin++){
-
- int pinMatched = msg.match(numToOSCAddress(pin), addrOffset);
- if(pinMatched){
- unsigned int frequency = 0;
-
- if (msg.isInt(0)){
- frequency = msg.getInt(0);
- }
- else if(msg.isFloat(0)){
- frequency = msg.getFloat(0);
- }
- else
- noTone(pin);
- if(frequency>0)
- {
- if(msg.isInt(1))
- tone(pin, frequency, msg.getInt(1));
- else
- tone(pin, frequency);
- }
- }
- }
- }
-
- #endif
-
- #ifdef BOARD_HAS_CAPACITANCE_SENSING
- #define NTPINS 12
- const int cpins[NTPINS] = {22,23,19,18,17,16,15,0,1,25,32, 33 };
- void routeTouch(OSCMessage &msg, int addrOffset )
- {
- for(int i=0;i<NTPINS;++i)
- {
- const char *name = numToOSCAddress(cpins[i]);
- int pinMatched = msg.match(name, addrOffset);
- if(pinMatched)
- {
- char outputAddress[9];
- strcpy(outputAddress, "/c");
- strcat(outputAddress, name);
- bundleOUT.add(outputAddress).add(touchRead(cpins[i]));
-
- }
- }
- }
- #endif
-
-
- #ifdef BOARD_HAS_DIE_POWER_SUPPLY_MEASUREMENT
- #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__)
- float getSupplyVoltage()
- {
- int val = analogRead(39);
- return val>0? (1.20f*1023/val):0.0f;
- }
-
- #else
-
- float getSupplyVoltage(){
-
- int result;
-
- #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
- ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
- #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
- ADMUX = _BV(MUX5) | _BV(MUX0);
- #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
- ADMUX = _BV(MUX3) | _BV(MUX2);
- #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) || defined(__AVR_ATmega1280__)
- ADMUX = 0x40| _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1) ;
- ADCSRB = 0;
- #else
- ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
- #endif
- delayMicroseconds(300);
- ADCSRA |= _BV(ADSC);
- while (bit_is_set(ADCSRA,ADSC));
- result = ADCL;
- result |= ADCH<<8;
-
- float supplyvoltage = 1.1264f *1023 / result;
- return supplyvoltage;
- }
- #endif
-
- #endif
-
- #ifdef BOARD_HAS_DIE_TEMPERATURE_SENSOR
-
-
- #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__)
- float getTemperature()
- {
- analogReference(INTERNAL);
- delay(1);
- int val = analogRead(38);
- analogReference(DEFAULT);
-
- return val;
- }
- #else
-
- float getTemperature(){
- int result;
-
- #if defined(__AVR_ATmega32U4__)
- ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0);
- ADCSRB = _BV(MUX5);
- #else
- ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(MUX3);
- #endif
- delayMicroseconds(200);
- ADCSRA |= _BV(ADSC);
- while (bit_is_set(ADCSRA,ADSC));
- result = ADCL;
- result |= ADCH<<8;
-
- analogReference(DEFAULT);
-
- return result/1023.0f;
- }
- #endif
-
- #endif
-
-
- void routeSystem(OSCMessage &msg, int addrOffset ){
-
- #ifdef BOARD_HAS_DIE_TEMPERATURE_SENSOR
- if (msg.fullMatch("/t", addrOffset)){
- bundleOUT.add("/s/t").add(getTemperature());
- }
- #endif
- #ifdef BOARD_HAS_DIE_POWER_SUPPLY_MEASUREMENT
- if (msg.fullMatch("/s", addrOffset)){
- bundleOUT.add("/s/s").add(getSupplyVoltage());
- }
- #endif
- if (msg.fullMatch("/m", addrOffset)){
- bundleOUT.add("/s/m").add((int32_t)micros());
- }
- if (msg.fullMatch("/d", addrOffset)){
- bundleOUT.add("/s/d").add(NUM_DIGITAL_PINS);
- }
- if (msg.fullMatch("/a", addrOffset)){
- bundleOUT.add("/s/a").add(NUM_ANALOG_INPUTS);
- }
-
-
-
- #if LED_BUILTIN!=13
-
- if (msg.fullMatch("/l", addrOffset)){
-
- if (msg.isInt(0)){
- pinMode(LED_BUILTIN, OUTPUT);
- int i = msg.getInt(0);
- pinMode(LED_BUILTIN, OUTPUT);
- digitalWrite(LED_BUILTIN, (i > 0)? HIGH: LOW);
- bundleOUT.add("/s/l").add(i);
- }
- }
- #endif
- }
-
-
- void setup() {
-
- read_mac();
- Ethernet.begin(mac,ip);
- Udp.begin(inPort);
-
- }
-
-
- void loop(){
- OSCBundle bundleIN;
- int size;
-
- if( (size = Udp.parsePacket())>0)
- {
-
-
- while(size--)
- bundleIN.fill(Udp.read());
-
- if(!bundleIN.hasError())
- {
- bundleIN.route("/s", routeSystem);
- bundleIN.route("/a", routeAnalog);
- bundleIN.route("/d", routeDigital);
- #ifdef BOARD_HAS_TONE
- bundleIN.route("/tone", routeTone);
- #endif
-
- #ifdef TOUCHSUPPORT
- bundleIN.route("/c", routeTouch);
- #endif
-
- }
-
- Udp.beginPacket(Udp.remoteIP(),outPort);
- bundleOUT.send(Udp);
- Udp.endPacket();
- bundleOUT.empty();
- }
- }
-
-
-
-
-
-
-
|