Quellcode durchsuchen

added automatonblink object for class testings
able to send OSC messages from class with external udp object

eLandon vor 6 Jahren
Ursprung
Commit
d059b83b4d
1 geänderte Dateien mit 114 neuen und 0 gelöschten Zeilen
  1. 114 0
      HTequi-firmware/src/blobcnc_low/main.cpp

+ 114 - 0
HTequi-firmware/src/blobcnc_low/main.cpp

@@ -1,5 +1,7 @@
  #include <Arduino.h>
 
+ #include <Automaton.h>
+
  #include <SPI.h>
  #include <Ethernet.h>
  #include <EthernetUdp.h>
@@ -26,6 +28,98 @@
  // An EthernetUDP instance to let us send and receive packets over UDP
  EthernetUDP Udp;
 
+
+ class Atm_blink : public Machine {
+
+   public:
+     Atm_blink( void ) : Machine() {};
+
+     short pin;
+     atm_timer_millis timer;
+
+     EthernetUDP* _udpRef ;
+
+     enum { IDLE, LED_ON, LED_OFF }; // STATES
+     enum { EVT_TIMER, EVT_ON, EVT_OFF, ELSE }; // EVENTS
+     enum { ENT_ON, ENT_OFF }; // ACTIONS
+
+     Atm_blink & begin( int attached_pin, uint32_t blinkrate, EthernetUDP& udpRef  ) {
+       const static state_t state_table[] PROGMEM = {
+         /*            ON_ENTER    ON_LOOP  ON_EXIT  EVT_TIMER  EVT_ON  EVT_OFF  ELSE */
+         /* IDLE    */  ENT_OFF,        -1,      -1,        -1, LED_ON,      -1,   -1,
+         /* LED_ON  */   ENT_ON,        -1,      -1,   LED_OFF,     -1,    IDLE,   -1,
+         /* LED_OFF */  ENT_OFF,        -1,      -1,    LED_ON,     -1,    IDLE,   -1,
+       };
+       Machine::begin( state_table, ELSE );
+       pin = attached_pin;
+       timer.set( blinkrate );
+       pinMode( pin, OUTPUT );
+       this->_udpRef =   &udpRef;
+       return *this;
+     }
+
+     int event( int id ) {
+       switch ( id ) {
+         case EVT_TIMER :
+           return timer.expired( this );
+       }
+       return 0;
+     }
+
+     void action( int id ) {
+       OSCMessage _msg("/blink");
+       _msg.add((int32_t)analogRead(0));
+       _udpRef->beginPacket(ipMulti, portMulti);
+       _msg.send(*_udpRef); // send the bytes to the SLIP stream
+       _udpRef->endPacket(); // mark the end of the OSC Packet
+       _msg.empty();
+       switch ( id ) {
+         case ENT_ON :
+           digitalWrite( pin, HIGH );
+
+           return;
+
+         case ENT_OFF :
+           digitalWrite( pin, LOW );
+           return;
+       }
+
+     }
+ };
+
+ Atm_blink led;
+
+void test(OSCMessage &msg, int addrOffset){
+  msg.getInt(0)?led.trigger( led.EVT_ON ):led.trigger( led.EVT_OFF );
+}
+
+void routeTone(OSCMessage &msg, int addrOffset ){
+  //iterate through all the analog pins
+  for(byte pin = 0; pin < NUM_DIGITAL_PINS; pin++){
+    //match against the pin number strings
+    int pinMatched = msg.match("5", addrOffset);
+    if(pinMatched){
+      unsigned int frequency = 0;
+      //if it has an int, then it's an integers frequency in Hz
+      if (msg.isInt(0)){
+        frequency = msg.getInt(0);
+      } //otherwise it's a floating point frequency in Hz
+      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);
+      }
+    }
+  }
+}
+
  void setup() {
    SPI.setSCK(27);
    Ethernet.init(10);
@@ -34,9 +128,27 @@
  //  Udp.beginMulti(ipMulti, portMulti);   // for modified Arduino library
    Udp.beginMulticast(ipMulti, portMulti); // for modified Teensy Ethernet library
    Serial.begin(115200); // higher Baud rate for faster refresh, CHANGE IN SERIAL MONITOR
+
+   led.begin( 14, 200, Udp);        // Setup a blink machine on pin 4
+   led.trigger( led.EVT_ON );  // Turn it on
+
+
  }
 
  void loop() {
+
+   OSCMessage msgIn;
+   int size;
+
+   if( (size = Udp.parsePacket())>0)
+   {
+     while(size--)
+       msgIn.fill(Udp.read());
+
+      if(!msgIn.hasError())
+        msgIn.route("/blink", test);
+   }
+
    OSCMessage msg("/analog/0");
    msg.add((int32_t)analogRead(0));
 
@@ -45,6 +157,8 @@
    Udp.endPacket(); // mark the end of the OSC Packet
    msg.empty(); // free space occupied by message
 
+   led.cycle();
+
    delay(20);
    //UDPsend();   // sending of packets; do not use with UDPreceive because it's including delay()!!
    //UDPreceive();