Difference between revisions of "MansOS Alarm"

From DiLab
Jump to: navigation, search
Line 14: Line 14:
#include "alarm.h"
#include "alarm.h"


== Data types ==
== Basic ==
=== Data types ===


'''AlarmId_t'''
'''AlarmId_t'''
Line 22: Line 23:
Alarm callback function pointer. Points to a function that will be called when the alarm event occurs.
Alarm callback function pointer. Points to a function that will be called when the alarm event occurs.


=== Constants ===

const AlarmId_t '''ALARM_FAIL''' = -1; // Alarm operation failed

=== Functions ===

AlarmId_t '''alarmNew'''( AlarmCb_t callbackFunction );
Create a new alarm. Returns the ID for the new alarm. Returns ALARM_FAIL when no alarms are available.
Set the callback function.


void '''alarmStartOneShot'''( AlarmId_t id, int time_ms );
Starts the alarm in one-shot mode.
Resets the alarm if it was already running.
The alarm calls the callback function after the time in time_ms milliseconds is expired.
Then it stops.


void '''alarmStartPeriodic'''( AlarmId_t id, int time_ms );
Starts the alarm in periodic mode.
Resets the alarm if it was already running.
The alarm calls the callback function after the time_ms is expired.
Then it automatically restarts.

void '''alarmStop'''( AlarmId_t id );
Stops the alarm.


== Advanced ==

=== Data types ===
'''AlarmType_t'''
'''AlarmType_t'''
Alarm types. These may be combined by "or"-ing or adding.
Alarm types. These may be combined by "or"-ing or adding.


== Constants ==
=== Constants ===


typedef enum {
typedef enum {
Line 33: Line 65:
} AlarmType_t;
} AlarmType_t;


=== Functions ===
const AlarmId_t '''ALARM_FAIL''' = -1; // Alarm operation failed


int alarmUnused();
== Functions ==
Returns the number of unused (free) alarms.

This number is limited, especially on the platforms with very limited memory.
AlarmId_t '''alarmNew'''( AlarmCb_t callbackFunction );
Initially there are ALARM_COUNT alarms available.
Create a new alarm. Returns the ID for the new alarm. Returns ALARM_FAIL when no alarms are available.
Set the callback function.




Line 46: Line 77:
Do not start the alarm unless ALARM_START is added to the type.
Do not start the alarm unless ALARM_START is added to the type.
Stop the alarm if it is already running.
Stop the alarm if it is already running.



void '''alarmStart'''( AlarmId_t id );
void '''alarmStart'''( AlarmId_t id );
Start the alarm. Resets the alarm if it was already running.
Start the alarm. Resets the alarm if it was already running.


void '''alarmStop'''( AlarmId_t id );
Stop the alarm. You may start this alarm later again.



void '''alarmFree'''( AlarmId_t id );
void '''alarmFree'''( AlarmId_t id );
Stops and releases the alarm with this ID.
Stops and releases the alarm with this ID.


== Advanced ==

int alarmUnused();
Returns the number of unused (free) alarms.
This number is limited, especially on the platforms with very limited memory.
Initially there are ALARM_COUNT alarms available.




Line 76: Line 93:
AlarmId_t id = alarmNew( myAlarm );
AlarmId_t id = alarmNew( myAlarm );
if( id == ALARM_FAIL ) return false;
if( id == ALARM_FAIL ) return false;
alarmSet( id, ALARM_REPEAT+ALARM_START, 1000); // Starts the timer now and fires every second
alarmStartPeriodic( id, 1000); // Starts the timer now and fires every second
return true;
return true;
}
}

Revision as of 00:37, 4 September 2010

MansOS --> Reference --> Alarm :: #Basic :: #Advanced :: #Example

NOTE: This feature is not implemented as described currently. Older interface version exists.

Alarms are used to execute some code after a certain time period, possibly repeatedly. Note, that the number of timers is limited. Initially there are ALARM_COUNT timers available.

The more timers you run, the less precise they may be due to the possibility for several of them to fire at the same time. Only one callback function will be executed immediately, the rest of them will wait for the previous callback to finish and thus will be delayed.

Declared in:

#include "alarm.h"

Basic

Data types

AlarmId_t

Handle (ID) for identifying the alarm when calling the alarm procedures.

AlarmCb_t

Alarm callback function pointer. Points to a function that will be called when the alarm event occurs.

Constants

const AlarmId_t ALARM_FAIL = -1;               // Alarm operation failed

Functions

AlarmId_t alarmNew( AlarmCb_t callbackFunction );

Create a new alarm. Returns the ID for the new alarm. Returns ALARM_FAIL when no alarms are available. Set the callback function.


void alarmStartOneShot( AlarmId_t id, int time_ms );

Starts the alarm in one-shot mode. Resets the alarm if it was already running. The alarm calls the callback function after the time in time_ms milliseconds is expired. Then it stops.


void alarmStartPeriodic( AlarmId_t id, int time_ms );

Starts the alarm in periodic mode. Resets the alarm if it was already running. The alarm calls the callback function after the time_ms is expired. Then it automatically restarts.

void alarmStop( AlarmId_t id );

Stops the alarm.


Advanced

Data types

AlarmType_t

Alarm types. These may be combined by "or"-ing or adding.

Constants

typedef enum {
  ALARM_ONESHOOT = 0x01,   // Fires the alarm once
  ALARM_REPEAT   = 0x02,   // Fires the alarm repeatedly until stopped
  ALARM_START    = 0x04,   // Starts the timer now.
} AlarmType_t;

Functions

int alarmUnused();

Returns the number of unused (free) alarms. This number is limited, especially on the platforms with very limited memory. Initially there are ALARM_COUNT alarms available.


void alarmSet( AlarmId_t id, AlarmType_t type, uint delayInMs )

Set the alarm type and time period in milliseconds. Do not start the alarm unless ALARM_START is added to the type. Stop the alarm if it is already running.

void alarmStart( AlarmId_t id );

Start the alarm. Resets the alarm if it was already running.

void alarmFree( AlarmId_t id );

Stops and releases the alarm with this ID.


Example

#include "alarm.h"

bool myStartAlarm()
{
  AlarmId_t id = alarmNew( myAlarm );
  if( id == ALARM_FAIL ) return false;
  alarmStartPeriodic( id, 1000);  // Starts the timer now and fires every second
  return true;
}

AlarmCb_t myAlarm()
{
  LedsToggleRed();  // Blink the red LED
}

MansOS --> Reference --> Alarm :: #Basic :: #Advanced :: #Example