Developer documentation - sensors

'Sensor' is the broad definition of the driver-like code that lives in a node and operates to interface to, and/or from, the outside world.
Lavalamp is designed to be easily extensible via a multitude of sensor nodes. Lavalamp uses only timer1 and the memory defined in main.h to operate, leaving a wide variety of hardware at the disposal of the sensor developer.
Under Lavalamp, the end-user is free to, at compile-time, 'instantiate' any number of sensors, attatched to any I/O pins, via a straightforward handful of #define directives. For example, take the following except from sensorcfg.h:


; config for first sensor
#define SENSOR_1_PRESENT
#define SENSOR_1_TYPE SENSOR_ID_GENERIC_DIGITAL_IN
#define SENSOR_1_PORT PORTA
#define SENSOR_1_TRIS TRISA
#define SENSOR_1_PIN 01

This is pretty self explainitory. Note that the Lavalamp engine itself only requres SENSOR_1_PRESENT and SENSOR_1_TYPE, and that the remaining lines are used exclusively by the sensor specified.
This functionality is implemented via a simple perl script, regen.pl. This is called from prebuild.bat before compilation, which instructs it to generate code based on some template files. For example, the initialisation code for all sensors is handled in autogen_sensorcode_init.asm, generated from . If we look inside the template, we find the following (edited for relevance):


(AUTOGEN_BEGIN_REPLICATING_BLOCK)
; SENSOR_(AUTOGEN_EVERY_SENSOR_ID)
#ifdef SENSOR_(AUTOGEN_EVERY_SENSOR_ID)_PRESENT
#if (SENSOR_ID_GENERIC_DIGITAL_IN == SENSOR_(AUTOGEN_EVERY_SENSOR_ID)_TYPE)
bsf SENSOR_(AUTOGEN_EVERY_SENSOR_ID)_TRIS, SENSOR_(AUTOGEN_EVERY_SENSOR_ID)_PIN
#endif
#if (SENSOR_ID_GENERIC_DIGITAL_OUT == SENSOR_(AUTOGEN_EVERY_SENSOR_ID)_TYPE)
bcf SENSOR_(AUTOGEN_EVERY_SENSOR_ID)_TRIS, SENSOR_(AUTOGEN_EVERY_SENSOR_ID)_PIN
#endif
#endif
(AUTOGEN_END_REPLICATING_BLOCK)

return

This will be expanded by the code generation perl script, to generate the following:


; SENSOR_0
#ifdef SENSOR_0_PRESENT
#if (SENSOR_ID_GENERIC_DIGITAL_IN == SENSOR_0_TYPE)
bsf SENSOR_0_TRIS, SENSOR_0_PIN
#endif
#if (SENSOR_ID_GENERIC_DIGITAL_OUT == SENSOR_0_TYPE)
bcf SENSOR_0_TRIS, SENSOR_0_PIN
#endif
#endif
; SENSOR_1
#ifdef SENSOR_1_PRESENT
#if (SENSOR_ID_GENERIC_DIGITAL_IN == SENSOR_1_TYPE)
bsf SENSOR_1_TRIS, SENSOR_1_PIN
#endif
#if (SENSOR_ID_GENERIC_DIGITAL_OUT == SENSOR_1_TYPE)
bcf SENSOR_1_TRIS, SENSOR_1_PIN
#endif
#endif

...

As you can see, the code block has been replicated for each possible sensor. It is important to preserve this functionality with any sensors you might create, so please use this functionality where possible!

If you are intending to add a new command, you should follow this rough sequence of actions:

For adding a new command handler on the node:

  1. Create a new template file, and add it to the project. Fill it with code (have fun).
  2. Add an extra line to prebuild.bat, which calls the code autogeneration script and builds from your newly-creted template
  3. Add the autogenerated file to the project.
  4. Add an EXTERN to cmds.h to the entrypoint of your command
  5. Add handler call in main.c, in the function processpacketcmd, to call your new command handler when a certain code is recieved.