awootton

Member Since: August 22, 2011

Country: United States

  • I know there's an esp8266 port for rtos (expressif,mattcallow) but does anyone know if it's been made into an Arduino Library like the avr and amd versions here? Is anyone working on that?

  • Watson, I think I get it. Mind blown. It's like there are threads now on an Arduino and somehow, magically, they all have stacks. Wow. How is that done? I suppose the whole rest of of the program after vTaskStartScheduler() is a series of interupts that schedule more interrupts but I'm still working on how a single cpu stack is managed to do that. Greatest hack ever my vote you have. This means that kids can write simple controllers to run stepper motors in a coordinate way. Robot time. But, I must ask. Will it run two tasks on an Uno R3? 4 tasks?

  • I'm taking this as a lesson where it's good to read the sparkfun blogs. Because, I wrote this a couple of days ago! So, yea, everyone, read sparkfun. Btw, I think I know a better trick for when the millis() timer rolls over. The trick is to never comare two times with each other but to, instead, subtract first and then compare with zero. Here's my version:

        // An example of two things running at different times.
    
        // This would be the .h file.
        #ifndef WootTimedItem_H_
        #define WootTimedItem_H_
    
        class WootTimedItem {
          private:
            long interval = 1000L * 60 * 10;// 10 min is default
            unsigned long nextEvent = 0;
          public:
            WootTimedItem( long _interval ) {
              interval = _interval;
              nextEvent = millis() + interval;
            }
            virtual void setup() = 0;
            virtual void work() = 0;
            // call this from the arduino loop()
            void main_loop(unsigned long currentTime);
        };
        #endif
    
        // this would go in the .cpp file
        void WootTimedItem::main_loop(unsigned long currentTime) {
          long delta = nextEvent - currentTime;// now it's signed!
          if ( delta < 0 ) {
            work();
            nextEvent += interval;
          }
        };
    
        // examples: classes should override setup() and work()
    
        // blink pin 13 led
        class Blinker : public WootTimedItem {
          private:
            int  pin =  13;
            char state = LOW;
          public:
            Blinker( long interval, int pin ): WootTimedItem(interval) {
              this->pin = pin;
            }
            void setup() {
              pinMode(pin, OUTPUT);
            }
            void work() {
              if ( state == LOW ) {
                digitalWrite(pin, HIGH);
                state = HIGH;
              } else {
                digitalWrite(pin, LOW);
                state = LOW;
              }
            }
        };
    
        // print a message
        class Printer : public WootTimedItem {
          private:
            char * msg = "";
          public:
            Printer( long interval, char * msg ): WootTimedItem(interval) {
              this->msg = msg;
            }
            void setup() {
              Serial.begin(9600);
            }
            void work() {
              Serial.println(msg);
            }
        };
    
        // test it out
        Blinker blinker(500, 13);
        Printer msg(1900, "its alive!");
    
        void setup() {
          // put your setup code here, to run once:
          blinker.setup();
          msg.setup();
        }
    
        void loop() {
          // put your main code here, to run repeatedly:
          // it should just be the main_loop's
          long now = millis();
          msg.main_loop(now);
          blinker.main_loop(now);
    
        }
    

No public wish lists :(