7 #include "contiki-conf.h"
12 #include "dev/avr-handler.h"
13 #include "pb_decode.h"
15 #include "settings.pb.h"
16 #include "readings.pb.h"
18 #include "net/rpl/rpl.h"
23 PROCESS(sample_process,
"Sample Process");
29 #define SAMPLER_EVENT_SAVE_SAMPLE 1
35 #define SAMPLER_EVENT_RELOAD_CONFIG 2
40 #define CONFIG_MIN_INTERVAL 2
45 #define AVR_ACTIVATE_DELAY (RTIMER_SECOND / 10)
50 static SensorConfig config;
60 static bool is_avr_complete;
66 static uint8_t power_data[PowerInfo_size];
71 static uint8_t power_len;
82 static void avr_callback(
bool isSuccess);
88 static bool start_avr(
void);
94 static void end_avr(
bool isSuccess);
100 static bool start_power(
void);
106 static void end_power(
bool isSuccess);
112 static void save_sample(
void);
118 static void refresh_config(
void);
123 static void print_config(SensorConfig *conf);
126 static struct etimer sample_timer;
135 avr_set_callback(&avr_callback);
154 memset(&sample, 0,
sizeof(sample));
158 ms_get_time(&sample.time);
160 sample.has_temp = ms_get_temp(&sample.temp);
162 sample.has_humid = ms_get_humid(&sample.humid);
164 if (config.hasADC1) {
165 sample.has_ADC1 = ms_get_adc1(&sample.ADC1);
168 if (config.hasADC2) {
169 sample.has_ADC2 = ms_get_adc2(&sample.ADC2);
172 if (config.hasRain) {
173 sample.has_rain = ms_get_rain(&sample.rain);
176 sample.has_accX = sample.has_accY = sample.has_accZ = ms_get_acc(&sample.accX, &sample.accY, &sample.accZ);
179 if (!config.has_powerID && ms_get_batt(&sample.batt)) {
180 sample.which_battery = Sample_batt_tag;
184 is_avr_complete = !config.has_avrID;
187 if (is_avr_complete) {
189 if (config.has_powerID && start_power()) {
200 if (config.has_avrID && start_avr()) {
207 }
else if (ev == SAMPLER_EVENT_SAVE_SAMPLE) {
213 DEBUG(
"Sample saved with id %d\n",
id);
215 DEBUG(
"Failed to save sample!\n");
218 }
else if (ev == SAMPLER_EVENT_RELOAD_CONFIG) {
221 DEBUG(
"Refreshed Sensor config to:\n");
222 print_config(&config);
228 static void refresh_config(
void) {
231 DEBUG(
"No Sensor config found\n");
235 DEBUG(
"Sensor config loaded\n");
239 DEBUG(
"Setting RPL mode to %d\n", config.routingMode);
243 void print_config(SensorConfig *conf) {
244 DEBUG(
"\tInterval = %d\n", (
unsigned int)conf->interval);
246 DEBUG(
"\tADC1: %s\n", conf->hasADC1 ?
"yes" :
"no");
247 DEBUG(
"\tADC2: %s\n", conf->hasADC2 ?
"yes" :
"no");
248 DEBUG(
"\tRain: %s\n", conf->hasRain ?
"yes" :
"no");
250 if (conf->has_avrID) {
251 DEBUG(
"\tAVR: %02X\n", conf->avrID);
254 if (conf->has_powerID) {
255 DEBUG(
"\tPower: %02X\n", conf->powerID);
258 DEBUG(
"\tRoutingMode: %d\n", conf->routingMode);
262 DEBUG(
"Config marked for refresh!\n");
267 return config->interval >= CONFIG_MIN_INTERVAL;
270 void save_sample(
void) {
274 void avr_callback(
bool isSuccess) {
276 if (!is_avr_complete) {
278 is_avr_complete =
true;
281 if (config.has_powerID && start_power()) {
287 end_power(isSuccess);
293 bool start_avr(
void) {
295 rtimer_clock_t end =
RTIMER_NOW() + AVR_ACTIVATE_DELAY;
299 data.data = sample.AVR.bytes;
300 data.len = &sample.AVR.size;
301 data.id = config.avrID;
303 data.size =
sizeof(sample.AVR.bytes);
305 DEBUG(
"Getting data from avr 0x%02X\n", config.avrID);
307 return avr_get_data(&data);
310 void end_avr(
bool isSuccess) {
311 DEBUG(
"Sampled from avr. Success: %d\n", isSuccess);
312 sample.has_AVR = isSuccess;
315 bool start_power(
void) {
317 data.data = power_data;
318 data.len = &power_len;
319 data.id = config.powerID;
320 data.size =
sizeof(power_data);
322 DEBUG(
"Getting data from power 0x%02X\n", config.powerID);
324 return avr_get_data(&data);
327 void end_power(
bool isSuccess) {
328 DEBUG(
"Sampled from power board. Success: %d\n", isSuccess);
334 pb_istream_t istream = pb_istream_from_buffer(data.data, *data.len);
335 if (!pb_decode(&istream, PowerInfo_fields, &sample.power)) {
339 sample.which_battery = Sample_power_tag;
bool store_save_config(SensorConfig *config)
Save the configuration to flash.
#define RTIMER_NOW()
Get the current clock time.
bool store_get_config(SensorConfig *config)
Get the configuration from the flash.
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
#define PROCESS_END()
Define the end of a process.
#define PROCESS(name, strname)
Declare a process.
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
SensorConfig SENSOR_DEFAULT_CONFIG
Default configuration to use when no configuration is curently defined.
uint16_t store_save_sample(Sample *sample)
Store a sample in the flash.
bool sampler_check_config(SensorConfig *config)
Check a config for sanity.
#define NULL
The null pointer.
Convenience layer for storing readings and the config.
enum rpl_mode rpl_set_mode(enum rpl_mode m)
Set the RPL mode.
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
#define CLOCK_SECOND
A second, measured in system clock time.
void sampler_refresh_config(void)
Get the Sampler to reload it's config from flash.
A struct used to request data from an AVR.
Process that periodically takes samples from onboard sensors and stores them in flash.
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
#define PROCESS_BEGIN()
Define the beginning of a process.
Definitions of the default configurations to use.