Go Down

Topic: mini RTOS [sic] (Read 4 times) previous topic - next topic

mem

#20
Sep 26, 2008, 11:08 am Last Edit: Sep 26, 2008, 11:10 am by mem Reason: 1
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
Code: [Select]
/*
 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 */




JB

#21
Oct 02, 2008, 05:20 am Last Edit: Oct 02, 2008, 05:45 am by jbennett Reason: 1
Hi everyone,

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

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:

Code: [Select]
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
JB

JB

Hi again...
Fixing my first problem has left me with another; here's the code:
Code: [Select]
#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!
JB

mem

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)

JB

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:
Code: [Select]
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.
JB

Go Up