/* --- MODULE.AVR.C Version 2.1 --- This is example module code for the AVR. --- It can be used as a template for creating your own modules. --- Search on "module" and replace with your module name. --- --- */ #include #include /* required for malloc() */ #include #include #include "vtypes.h" #include "module.h" #include "pbort.h" /* These user options for the sbsSet() and sbsGet() routines must appear in the module.h header file so they are accessible by other routines. */ /* One could use an enum statement */ #define MODULE_USER1 101 #define MODULE_USER2 102 #define MODULE_USER3_PTR 103 #define MODULE_USER2_PTR 104 /* ******************************************************************** */ /* Global variables */ /* ******************************************************************** */ /* ******************************************************************** */ /* module Local Structure (optional) */ /* ******************************************************************** */ /* This will likely appear in module.h */ typedef struct{ /* put any local variables your module needs to operate here, if any */ char user1; int16_t user2[5]; specialStruct_t *user3; } module_localT; /* ******************************************************************** */ /* module_on (optional) Start up the module. */ /* ******************************************************************** */ char module_on(uint8_t index) { module_localT *local = (module_localT *)spawnQueueG[index].local; /* put any code in here your module needs when it first turns on */ /* This is only run once when first turned "on". */ return I_OK; } /* ******************************************************************** */ /* module_set sbsSet() user-defined functions. */ /* ******************************************************************** */ char module_set(uint8_t index, int16_t type, int16_t arg, void *vptr) { module_localT *local = (module_localT *)spawnQueueG[index].local; switch(type){ case MODULE_USER1: /* set the value of user1 in the local structure */ /* user-defined code */ local->user1 = *(char *)vptr; return I_OK; break; case MODULE_USER2: /* set the value of an element of user2 in the local structure */ /* user-defined code */ local->user2[arg] = *(int16_t *)vptr; /* this could copy the entire array, if desired */ return I_OK; break; case MODULE_USER3_PTR: /* set the user3 pointer to point to a structure outside the module */ /* user-defined code */ local->user3 = (specialStruct_t *)vptr; return I_OK; break; default: return I_ERROR; } return I_OK; } /* ******************************************************************** */ /* module_get sbsGet() user-defined functions. */ /* ******************************************************************** */ char module_get(uint8_t index, int16_t type, int16_t arg, void *vptr) { module_localT *local = (module_localT *)spawnQueueG[index].local; switch(type){ case MODULE_USER1: /* get the value of user1 from the local structure */ /* user-defined code */ *(char *)vptr = local->user1; return I_OK; break; case MODULE_USER2: /* get the value of a specific element of the user2 array */ /* user-defined code */ *(int16_t *)vptr = local->user2[arg]; return I_OK; break; case MODULE_USER2_PTR: /* get the pointer to the user2 array and export it to main() */ /* In main(), declare a pointer variable as int16_t *mod_user2; */ /* In main(), call sbsGet(moduleID, MODULE_USER2_PTR, 0, (void *)&mod_user2); */ /* This passes a handle to sbsGet() and module_get() in vptr */ /* user-defined code */ *(int16_t **)vptr = local->user2; /* assign the user2 pointer to the value of the handle */ return I_OK; break; default: return I_ERROR; } return I_OK; } /* ******************************************************************** */ /* module_cycle (required) Process module information. */ /* ******************************************************************** */ char module_cycle(uint8_t index) { module_localT *local = (module_localT *)spawnQueueG[index].local; /* This is the main function. Put the code you want to execute periodically in here. */ return I_OK; /* you can return SBS_OFF if the module shuts itself off */ } /* ******************************************************************** */ /* module_init (required) Initialize the module */ /* ******************************************************************** */ char module_init(uint8_t index) { module_localT *local; float *fptr; int i, tmp; /* intialize the function pointers - required */ spawnQueueG[index].on_fptr = module_on; spawnQueueG[index].cycle_fptr = module_cycle; spawnQueueG[index].off_fptr = NULL; spawnQueueG[index].set_fptr = module_set; // OPTIONAL: this gets called by sbsSet(). Set to NULL if nothing spawnQueueG[index].get_fptr = module_get; // OPTIONAL: this gets called by sbsGet(). Set to NULL if nothing /* Allocate the local structure for the module - optional This struct will be freed automatically when SBS_KILL is implemented */ spawnQueueG[index].local = (pointer)malloc(sizeof(module_localT)); local = (module_localT *)spawnQueueG[index].local; /* Initialize the local structure (module dependent) */ local->user1 = 0; local->user2[0] = 0x03FF; /* put any initialization here */ return I_OK; }