mini RTOS [sic]

I would think that such an implementation would go through a period of time available as an external library before it would "qualify" to be added to the core. For that matter, I don't see any big reason to add it to the core at all, other than making things simpler for users...
(but it might be better in general to add some sort of improved library handling to the arduino environment, so that the issue of whether things should be added to "the core" comes up less often...)

mellis, are you looking to potentially include a solution for this in the Arduino environment? Because, if so, I think that it would heavily influence the design, especially with respect to whether a static or dynamic approach is used. If it is something aimed at the core I would suspect that limiting resources when NOT using the feature would become a priority over limiting resources when using the feature. But, however if this were an addon library, it would be reasonable to make the opposite assumption and to focus on limiting resources when using the library since when not using it, it would simply not be compiled in. These two priorities could lead to drastically different designs. What do you think?

My personal view is that the design requirements are influenced far more by usability issues than resource usage. However frugal a piece of code, I don't think it would be appropriate for the arduino unless it could be easily understood and used by people with little or no prior experience with programming.

My understanding of the core requirement is to provide arduino users the ability to easily schedule a few different things to happen in different periods of time ranging from milliseconds to days or longer. To me, easy means using expressions consistent with the existing arduino abstractions.

That's pretty vague and leaves lots of decisions unresolved but if there is consensus on this point I think it will not be difficult to address thing like:

  • should the user explicitly instantiate the scheduled objects or should a fixed number be created for him, if the latter how many ?
  • should there be a single callback or can the user explicitly declare his callbacks
  • What is the most intuitive naming for the various objects that provide this functionality
  • Is there a requirement for both periodic scheduling (do something after this amount of time has elapsed ) and date/time scheduling (do something at this specific date and time)
  • (How ) should the co-operative scheduler handle slip (something should happen now but another task is still executing). Should the amount of slip be subtracted from the next scheduled trigger (minimizing slip accumulation) or ignored (guaranteeing that at least the scheduled time will elapse before the next trigger)]

I would be very interested in hearing other views on what the simple scheduler should do and how it should look..

Being able to write/use your own callback functions would be nice, I think. So +1 from me on that idea :slight_smile:

I would suggest a slightly different view of the sample code. The term Alarm would be replaced by Task. Rather than one loop with testing there would be separate loops.

The setup should allow for delta time, absolute time as well as one time and repeating schedules.

I would default the maximum number of tasks at 6 and wait for user feedback.

#include <DateTime.h>
#include <DateTimeStrings.h>
#include <TaskScheduler.h>


void onTask1(){
      Serial.println("15 seconds Alarm");         
  }

void onTask2(){
      Serial.println("90 second Alarm");    
  }

void onTask3(){
      Serial.print("One Shot alarm, elapsed period was ");    
      time_t alarmValue  = dtAlarms.getValue(Sender);
      Serial.println(alarmValue,DEC); 
  }

void onTask4(){
      Serial.print("re-trigged One Shot alarm, elapsed period was ");    
      time_t alarmValue  = dtAlarms.getValue(Sender);
      Serial.println(alarmValue,DEC); 
      dtAlarms.setValue(Sender, alarmValue + 2); //re-enable with a new value two seconds longer
    }
}

void setup(){
   pinMode(13,OUTPUT);
   Serial.begin(19200);

   Task1 = dtAlarms.createRepeating( 15 );  // alarm every 15 seconds
   Task2 = dtAlarms.createRepeating( AlarmHMS(0,1,30) );  // alarm every 1 minute 30 seconds
   Task3 = dtAlarms.createOneshot( 10 );  // one shot alarm in 10 seconds
   Task4 = dtAlarms.createOneshot( 12 );  // one shot alarm that will be manually retriggered

   Serial.println("started");
 }

void loop(){
  dtAlarms.waitUntilThisSecond(0); //  this code blocks waiting for start of the next minute, background alarms are still serviced 
  Serial.println("turning LED on");
  digitalWrite(13, HIGH);
  dtAlarms.delay(2000);  // note we call the alarm delay to service the background alarms 
  Serial.println("turning LED off");
  digitalWrite(13,LOW);  

}

Well, I know that you seemed to think that inclusion in the core was not an important question, but a non user-definable method to allocate space for these Alarms/event seems unreasonable if this library is ever going to be a part of the core. Mem has done a good job of keeping resource usage (RAM) very low for users of his suggested library, but it still uses 60 bytes! That seems unacceptable to me for core inclusion considering that some people may never even want to use these Alarms. Choosing a lower default number of Alarms does not seem like a good core solution either, and neither does making things compile time options. So, if this is ever going to be considered for core inclusion I would suggest a different approach for allocation memory. However since Mem did do a good job of minimizing RAM usage, any core implementation would probably take up more RAM and therefor as a standalone library his solution may be better.

If libraries are never going to be core solutions, compile time options may well be good solutions, especially if the library does something very specialized that could greatly benefit from compile time options to either save RAM or to increase speed. For example: I am working on an LED TDM library and have come to the conclusion that speed is paramount. Although I would like to create a library that could be easily used without recompiling, I have determined that this would potentially mean a significant (3x) speed decrease. Since this might very well push the library utility out of the problem space that it is trying to address in the first place, this does not seem worth it. I will probably leave certain things compile time options for this TDM library.

While I certainly agree that the programing interface is important to keep simple, the target user audience is also somewhat dependent on whether a library is meant for core inclusion or not. Obviously we want to keep interfaces simple, but often times a sacrifice in simplicity is warranted for better performance or flexibility. Sometimes the only way to make good decisions about which of these tradeoffs to make is by deciding who the likely target audience is.

Allowing the user to create his own callbacks is easy and probably not much more complicated for a newbie to use (it would look similar to the way attachInterrupt works). Only uses two more bytes per instance so nothing to worry about there.

Creating instances of alarms (or whatever they will be called) at runtime to minimize RAM usage to the minimum needed has also been implemented, I started out with a version of the library that allows the user to create instances of the timers he needs so memory is only consumed when needed, but it seemed a less friendly to newbies then the simpler version posted earlier.

Here is a fragment from one of my test sketches that creates three instances

AlarmClass Timer1;  
AlarmClass Timer2;
AlarmClass Timer3;

void setup(){
  // you can register time of day Alarms at any time but really shouldn't enable them until the internal clock is set

   if( dtAlarms.registerTimer( &Timer1 ) ) {        
     Timer1.value = DateTime.now() + 11;   // fire at the time of day 11 seconds from now
     Timer1.Mode.isTimeOfDay = true; // the value given above is a time of day
     Timer1.onTickHandler = &OnTimer1Tick;
     Timer1.enable();  
  }
   if( dtAlarms.registerTimer( &Timer2 ) ) {        
     Timer2.value = AlarmHMS(12,30,0)    // this is 30 minutes after 12 noon
     Timer2.onTickHandler = &OnTimer2Tick;
     Timer2.Mode.isTimeOfDay = true;
     Timer2.enable();  
  }
   if( dtAlarms.registerTimer( &Timer3 ) ) {        
     Timer3.Mode.isTimeOfDay = false; // the timer value is treated as a delay in seconds, not absolute time 
     Timer3.value =  13;   // delay in seconds from the time this alarm is enabled
     Timer3.onTickHandler = &OnTimer1Tick;
     Timer3.enable();  
  }

}

void OnTimer1Tick(void *Sender){

  if( Sender ==  &Timer1) 
      Serial.print("Timer1 event: ");            
   else  if( Sender ==  &Timer3) 
      Serial.print("Timer3 event: ");      
  timeDisplay();     
}

void OnTimer2Tick(void *Sender){
    Serial.print("Timer2 event: ");   
}

mem,
can you tell me what to modify to get your dateTimeAlarm library to run with 0012? I am seeing the
same kind of library compile errors others have seen with moving to 0012

can you tell me what to modify to get your dateTimeAlarm library to run with 0012? I am seeing the
same kind of library compile errors others have seen with moving to 0012

In the DateTimeAlarm.h file comment out the following line:

//#include <wiring.h> // this line must be commented out or removed for 0012

mem,
Did you mean TimerAlarms.h ? I tried commenting out the include in TimerAlarms.h but it did not work. Same kind of errors at compile time. Any other ideas? I am using the Mac version.

Yes, DateTimeAlarms.h
Here is my the modified code. note that you need the latest version of DateTime.h for this to compile with 0012

/*
  DateTimeAlarms.h - Arduino Date and Time alrms library

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

*/

#ifndef DateTimeAlarms_h
#define DateTimeAlarms_h

#include <inttypes.h>
//#include <wiring.h> // commenting this out was the only change necessary for 0012

#include "dateTime.h"

typedef enum {
     dtMillisecond, dtSecond, dtMinute, dtHour, dtDay //,clkMonth,clkYear
 } dtUnits_t;

typedef struct  {
      uint8_t isEnabled              :1 ;
      uint8_t isOneShot              :1 ; 
      uint8_t isAlarm                :1 ;    
   }
     AlarmMode_t   ;

typedef uint8_t AlarmID_t;
#define dtINVALID_ALARM_ID 255
#define dtNBR_ALARMS 6

class AlarmClass;  // forward reference
typedef void (*OnTick_t)(uint8_t);  // alarm callback function typedef 

// class defining an alarm instance, only used by dtAlarmsClass
class AlarmClass
{      
private:
public:
      AlarmClass();
      OnTick_t onTickHandler;      
      void updateNextTrigger();

      time_t value;
      time_t nextTrigger;
      AlarmID_t ID;           // unique instance id (only used for debugging
      AlarmMode_t Mode;
};


// class containing the collection of alarms
class dtAlarmsClass
{
   friend class TimerClass;
private:
   AlarmClass Alarm[dtNBR_ALARMS];
   void serviceAlarms();
   boolean isServicing;
   AlarmID_t nextID;
   AlarmID_t create( time_t value, OnTick_t onTickHandler,boolean isAlarm, boolean isEnabled );
public:
      dtAlarmsClass();
      void delay(unsigned long ms);
      uint8_t getDigitsNow( dtUnits_t Units);  // returns the current digit value for the given time unit
      void waitForDigits( uint8_t Digits, dtUnits_t Units);
      void waitForRollover(dtUnits_t Units);
      
      // functions for specific alarms identifed by alarm ID
      AlarmID_t createAlarm( time_t value, OnTick_t onTickHandler, boolean isEnabled = true );
    AlarmID_t createTimer( time_t value, OnTick_t onTickHandler, boolean isEnabled = true );
      void setValue(AlarmID_t ID, time_t value);
      void enable(AlarmID_t ID);
      void disable(AlarmID_t ID);

};

extern dtAlarmsClass dtAlarms;  // make an instance for the user
/*==============================================================================
 * MACROS
 *============================================================================*/
/* public */
#define waitUntilThisSecond(_val_) waitForDigits( _val_, dtSecond)
#define waitUntilThisMinute(_val_) waitForDigits( _val_, dtMinute)
#define waitUntilThisHour(_val_)   waitForDigits( _val_, dtHour)
#define waitUntilThisDay(_val_)    waitForDigits( _val_, dtDay)
#define waitMinuteRollover() waitForRollover(dtSecond)
#define waitHourRollover()   waitForRollover(dtMinute)
#define waitDayRollover()    waitForRollover(dtHour)

#define AlarmHMS(_hr_, _min_, _sec_) (_hr_ * SECS_PER_HOUR + _min_ * SECS_PER_MIN + _sec_)

#endif /* Clock_h */

Hi everyone,

First time poster, long time forum-scourer... :slight_smile:

I've been madly searching these forums since receiving my Diecimila a few weeks ago, trying to cobble together some alarm code that I can use to trigger a camera at a given time each day. I was very pleased to find this thread, but I can't seem to work out why I get compile errors when using mem's example code from reply #2 or reply #16. For reply #2, I get:

[EDIT] Never mind, I fixed this first problem..

For reply #16, I get:

In function 'void setup()':
error: 'class dtAlarmsClass' has no member named 'registerTimer' In function 'void loop()':

relating to:

 Wire.begin();
    // you can register time of day Alarms at any time but really shouldn't enable them until the internal clock is set
   if( dtAlarms.registerTimer( &TimeForPhoto ) ) {        
     TimeForPhoto.value = AlarmHMS(12,30,0)    // this is 30 minutes after 12 noon
     TimeForPhoto.onTickHandler = &OnTimeForPhotoTick;
     TimeForPhoto.Mode.isTimeOfDay = true;
     TimeForPhoto.enable(); // the above registers an alarm, labelled 'TimeForPhoto', as one that triggers at the given time
 of day.
  }

I'm able to follow the coding to a certain extent, but this is well above anything I've tried before... I've ordered a Mini-DS1307 board to supply the time, so until that comes I thought I'd work on the alarm code.

Any help would be greatly appreciated - and by the way, congrats on such a friendly, supportive forum!

JB

Hi again...
Fixing my first problem has left me with another; here's the code:

#include <DateTime.h>
#include <DateTimeAlarms.h>

AlarmID_t Alarm9,Alarm12,Timer11;  // id to identify what triggered alarm if callbacks are shared

void setup(){
  Serial.begin(19200);
  Alarm9 = dtAlarms.createAlarm( DateTime.now() + 9, OnAlarm); // trigger 9 seconds from now
  Alarm12 = dtAlarms.createAlarm(AlarmHMS(12,0,0), OnAlarm); // trigger at mid day
  Timer11 = dtAlarms.createTimer( 11, OnTimer); // trigger in 11 seconds
}

void OnAlarm(AlarmID_t Sender){
  // callback for time of day alarms
  if( Sender ==  Alarm9) {
    Serial.print("Alarm9: ");        
    dtAlarms.setValue(Alarm9, DateTime.now() + 9 ); // reset alarm to trigger at the time that is 9 seconds from now
  }
  else  if( Sender ==  Alarm12)
  {
    Serial.print("Alarm12: ");
    dtAlarms.setValue(Alarm12, AlarmHMS(12,0,0)); // reset alarm to trigger at noon
  }    
}

void OnTimer(AlarmID_t Sender){
  // callback for time delay alarm
  Serial.print("Timer11: 11 sec timer: ");    
  dtAlarms.setValue(Timer11, 11 ); // delay another 11 seconds
}

void  loop(){  
  
  dtAlarms.waitUntilThisSecond(0); //  this code blocks waiting for start of the next minute, background alarms are still serviced
  digitalWrite(13, HIGH);
  dtAlarms.delay(2000);  // note we call the alarm delay to service the background alarms
  digitalWrite(13,LOW);  
  dtAlarms.delay(2000); // delay is in milliseconds , all other alarm values are seconds
}

...which gives me the following error:

o: In function loop': undefined reference to dtAlarms'o: In function OnTimer(unsigned char)': o: In function OnAlarm(unsigned char)':
o: In function `setup':

Again, thanks for any thoughts!

Hi JB, the sketch you posted above compiles ok for me on version 0011 and 0012, which version are you using?

If 0012, are you using the latest version of DateTime.h (if not then download the latest DateTime from the library)

Hi mem,

The errors posted are from 0011. I've tried with 0012, but I think I've done something wrong when I instaleld that. On compiling, I get this:

o: In function `loop':
C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:39: undefined reference to `dtAlarms'


C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:39: undefined reference to `dtAlarms'


C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:39: undefined reference to `dtAlarmsClass::waitForDigits(unsigned char, dtUnits_t)'


C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:41: undefined reference to `dtAlarmsClass::delay(unsigned long)'


C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:43: undefined reference to `dtAlarmsClass::delay(unsigned long)'


o: In function `OnTimer(unsigned char)':
C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:34: undefined reference to `dtAlarms'


C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:34: undefined reference to `dtAlarms'


C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:34: undefined reference to `dtAlarmsClass::setValue(unsigned char, unsigned long)'


o: In function `OnAlarm(unsigned char)':
C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:22: undefined reference to `dtAlarms'


C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:22: undefined reference to `dtAlarms'


C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:22: undefined reference to `dtAlarmsClass::setValue(unsigned char, unsigned long)'


C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:27: undefined reference to `dtAlarms'


C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:27: undefined reference to `dtAlarms'


C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:27: undefined reference to `dtAlarmsClass::setValue(unsigned char, unsigned long)'


o: In function `setup':
C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:13: undefined reference to `dtAlarms'


C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:13: undefined reference to `dtAlarms'


C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:13: undefined reference to `dtAlarmsClass::createAlarm(unsigned long, void (*)(unsigned char), unsigned char)'


C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:14: undefined reference to `dtAlarmsClass::createAlarm(unsigned long, void (*)(unsigned char), unsigned char)'


C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp/Temporary_6757_4834.cpp:15: undefined reference to `dtAlarmsClass::createTimer(unsigned long, void (*)(unsigned char), unsigned char)'


Couldn't determine program size: C:\Documents and Settings\john\My Documents\arduino-0012\hardware/tools/avr/bin/avr-size: 'C:\DOCUME~1\john\LOCALS~1\Temp\build61366.tmp\DateTimeAlarmsSketch.hex': No such file

I've got a feeling that this is going to be an embarassingly easy thing to fix but I can't put my finger on it.

dtAlarms is an object that is made when DateTimeAlarms.cpp is compiled and is defined in DateTimeAlarms.h

Try deleting DateTimeAlarms.o and recompile your sketch and check to see if the .o file is created.

Did you change the DateTimeAlarms.h file? If so, try replacing your copy with the DateTimeAlarms.h code in the thread.

I was pretty sure that I'd replaced the DateTimeAlarms.h file, but I decided to do a clean install of v0012 in a seperate location anyway. I have a DateTimeAlarms library, but it's only got the DateTimeAlarms.h file; I don't seem to have the DateTimeAlarms.cpp or DateTimeAlarms.o file anywhere, and can't locate it on the forums or main site.

I'm guessing that if someone can point me to that file, my problems may just disappear...

Thanks again!

JB the files are posted in the begining of this thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1217881285

try it in 0011 and see if that fixes your problem. the files need a small mod for 0012 and these were posted today in that thread

The mod for 0012 is to move #include <wiring.h> from DateTimeAlarms.h and put it into DateTimeAlarms.cpp

OK, so now we get to the bit that's confused me every time I've read this thread: hotcarrier wrote -

mem,
Did you mean TimerAlarms.h ?

... to which you replied -

Yes, DateTimeAlarms.h

:-? Sorry if I've missed something here, but the thread you directed me to has 'TimerAlarms' files in it, not 'DateTimeAlarms'. I tried renaming them, and replacing any 'TimerAlarm' references with 'DateTimeAlarms' references but that didn't work. Also tried it in 0011 with similar results. I AM getting a different error now though:

In file included from C:\arduino-0012\hardware\cores\arduino/WProgram.h:4,


c:/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:80: error: expected unqualified-id before 'int'


c:/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:80: error: expected `)' before 'int'


c:/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:80: error: expected `)' before 'int'


c:/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected unqualified-id before 'int'


c:/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected `)' before 'int'


c:/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected `)' before 'int'


c:/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:144: error: expected identifier before '(' token


c:/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:144: error: expected `)' before '(' token


c:/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:144: error: expected ',' or '...' before '(' token


c:/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:144: error: expected initializer before ')' token


c:/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:176: error: '__compar_fn_t' has not been declared


In file included from C:\arduino-0012\hardware\cores\arduino/WProgram.h:6,


 In function 'void setup()':

What am I missing?!? I feel like such a newb... ::slight_smile:

Sorry, just picked up on your comment about MOVING #include <wiring.h> from one file to the other... much better, but still have this last little error:

In function 'void setup()':
error: 'class dtAlarmsClass' has no member named 'createAlarm'

Which is a lot nicer than what I had before...

Again, sorry for not reading your post correctly!! [smiley=embarassed.gif]

JB, the sketch you posted compiles on my PC without any errors in 0012 with the <wiring.h> moved from the .h file to the .cpp file.

Have you tried to use the .h and .cpp file I posted today in the other thread?

If you are still stuck, send me a Personal Message with your email address and I will email you the files you need to put in the DateTimeAlrams directory.