20 #include "sensebe_ble.h" 21 #include "sensebe_rx_mod.h" 22 #include "sensebe_store_config.h" 29 #include "tssp_detect.h" 30 #include "device_tick.h" 31 #include "cam_trigger.h" 32 #include "simple_adc.h" 34 #include "hal_nop_delay.h" 35 #include "tssp_ir_tx.h" 36 #include "radio_trigger.h" 40 #define DETECT_FEEDBACK_TIMEOUT_TICKS MS_TIMER_TICKS_MS(270000) 42 #define LIGHT_THRESHOLD_MULTIPLY_FACTOR 32 44 #define MAX_ADC_OUTPUT 4096 46 #define LIGHT_SENSE_INTERVAL_TICKS MS_TIMER_TICKS_MS(300000) 48 #define PULSE_REQ_FOR_SYNC 4 50 #define MOTION_SYNC_ON_TIME 200 52 #define MOTION_SYNC_OFF_TIME 800 66 }motion_detection_states_t;
98 TX_EN = SENSEBE_TX_BOARD,
100 RX_EN = SENSEBE_RX_BOARD
105 static uint32_t feedback_timepassed = 0;
107 static sensebe_config_t sensebe_config;
109 static uint32_t arr_module_tick_duration[] = {MOD_FREQ0, MOD_FREQ1, MOD_FREQ2, MOD_FREQ3};
114 static bool arr_is_light_ok [MAX_MODS];
116 static bool arr_is_light_sense_req [MAX_MODS];
118 static bool arr_is_mod_on[MAX_MODS];
122 static uint32_t tssp_detect_sync_time;
124 static motion_detection_states_t motion_state = MOTION_SYNC;
126 static uint32_t timer_module_value = 0;
128 static uint32_t light_check_sense_pin = 0;
130 static uint32_t light_check_en_pin = 0;
135 void motion_module_start (
void);
137 void motion_module_add_ticks (
void);
139 void motion_module_add_mod_ticks (
void);
141 void motion_module_stop (
void);
144 void state_change_sync (
void);
146 void state_change_idle (
void);
148 void state_change_stop (
void);
150 void (* arr_state_change[]) () = {
157 void window_detect_handler ();
159 void pulse_detect_handler (uint32_t ticks_count);
164 void timer_module_start (
void);
166 void timer_module_add_ticks (
void);
168 void timer_module_add_mod_ticks (
void);
170 void timer_module_stop (
void);
173 void timer_trigger_handler ();
183 void light_check (oper_time_t oper_time, uint32_t light_intensity, uint32_t module);
188 void light_sense_add_ticks (uint32_t interval);
195 void camera_unit_handler(uint32_t trigger);
205 bool compare_margin(uint32_t data, uint32_t ref, uint32_t margin);
213 bool three_window_sync (uint32_t ticks);
218 void add_ticks_feedback (uint32_t interval);
227 void module_tick_handler ();
231 void add_ticks_feedback (uint32_t interval)
233 feedback_timepassed += interval;
234 if(feedback_timepassed >= DETECT_FEEDBACK_TIMEOUT_TICKS)
240 void state_change_sync ()
242 log_printf (
"%s\n", __func__);
246 void state_change_idle ()
248 log_printf (
"%s\n", __func__);
253 void state_change_stop ()
256 log_printf (
"%s\n", __func__);
261 void camera_unit_handler(uint32_t trigger)
263 log_printf(
"%s\n", __func__);
274 void timer_trigger_handler ()
282 void window_detect_handler ()
286 motion_state = MOTION_SYNC;
287 arr_state_change[motion_state] ();
290 radio_trigger_yell ();
293 bool compare_margin(uint32_t data, uint32_t ref, uint32_t margin)
295 if((ref-margin)<=data && data<=(ref+margin))
305 bool validate_and_sync (uint32_t ticks)
307 for(uint32_t freq_cmp = 0; freq_cmp < MAX_MOD_FREQ; freq_cmp++)
317 bool three_window_sync (uint32_t ticks)
319 static uint32_t previous_pulse_tick = 0, current_pulse_tick = 0;
320 static uint32_t pulse_diff_window[] = {0,0,0};
321 static uint32_t pulse_cnt = PULSE_REQ_FOR_SYNC;
322 static bool flag =
false;
323 if(pulse_cnt == PULSE_REQ_FOR_SYNC)
326 previous_pulse_tick = ticks;
329 else if(pulse_cnt > 0)
331 current_pulse_tick = ticks;
332 pulse_diff_window[pulse_cnt - 1] = ((current_pulse_tick + (1<<24))- previous_pulse_tick)
334 previous_pulse_tick = current_pulse_tick;
337 else if(pulse_cnt == 0)
339 pulse_cnt = PULSE_REQ_FOR_SYNC;
342 log_printf(
"Window[0]: %d\n", pulse_diff_window[0]);
343 log_printf(
"Window[1]: %d\n", pulse_diff_window[1]);
344 log_printf(
"Window[2]: %d\n", pulse_diff_window[2]);
345 flag = (validate_and_sync (pulse_diff_window[0])
346 && validate_and_sync (pulse_diff_window[1])
347 && validate_and_sync (pulse_diff_window[2])
353 tssp_detect_sync_time = pulse_diff_window[1];
354 pulse_diff_window[0] = 0;
355 pulse_diff_window[1] = 0;
356 pulse_diff_window[2] = 0;
361 void pulse_detect_handler (uint32_t ticks_count)
363 if(motion_state == MOTION_SYNC)
365 if(three_window_sync (ticks_count))
368 motion_state = MOTION_IDLE;
369 arr_state_change[motion_state] ();
370 if(feedback_timepassed < DETECT_FEEDBACK_TIMEOUT_TICKS)
387 void light_check (oper_time_t oper_time, uint32_t light_intensity, uint32_t module)
389 uint8_t light_sense_config = oper_time.day_or_night;
390 uint32_t light_threshold =
391 (uint32_t)((oper_time.threshold) * LIGHT_THRESHOLD_MULTIPLY_FACTOR);
395 if(((light_sense_config == 1) && (light_intensity >= light_threshold))
397 ((light_sense_config == 0) && (light_intensity <= light_threshold)))
400 arr_is_light_ok[module] =
true;
404 arr_is_light_ok[module] =
false;
408 void light_sense_add_ticks (uint32_t interval)
410 static uint32_t timepassed = 0;
411 timepassed += interval;
412 if(timepassed >= LIGHT_SENSE_INTERVAL_TICKS)
414 static uint32_t light_intensity;
416 hal_gpio_pin_set (light_check_en_pin);
418 hal_nop_delay_ms (3);
420 light_check_sense_pin));
422 if(arr_is_light_sense_req[MOD_MOTION])
424 light_check (sensebe_config.tssp_conf.oper_time, light_intensity, MOD_MOTION);
428 if(arr_is_light_sense_req[MOD_TIMER])
430 light_check (sensebe_config.timer_conf.oper_time, light_intensity, MOD_TIMER);
434 hal_gpio_pin_clear (light_check_en_pin);
439 void motion_module_start ()
441 oper_time_t motion_oper_time = sensebe_config.tssp_conf.oper_time;
443 if((motion_oper_time.day_or_night == 1 && motion_oper_time.threshold == 0b0000000)||
444 (motion_oper_time.day_or_night == 0 && motion_oper_time.threshold == 0b1111111))
446 arr_is_light_sense_req[MOD_MOTION] =
false;
447 arr_is_light_ok [MOD_MOTION] =
true;
451 arr_is_light_sense_req[MOD_MOTION] =
true;
456 .trig_duration_100ms = sensebe_config.tssp_conf.intr_trig_timer,
457 .trig_mode = sensebe_config.cam_trigs[MOTION_ALL].mode,
458 .trig_param1 = sensebe_config.cam_trigs[MOTION_ALL].larger_value,
459 .trig_param2 = sensebe_config.cam_trigs[MOTION_ALL].smaller_value,
460 .pre_focus_en = (bool)sensebe_config.cam_trigs[MOTION_ALL].pre_focus,
464 radio_trigger_init_t radio_init =
466 .comm_direction = RADIO_TRIGGER_Tx,
468 .irq_priority = APP_IRQ_PRIORITY_HIGH,
469 .tx_on_freq_us = 500,
470 .tx_on_time_ms = 125,
472 radio_trigger_init (&radio_init);
474 modules_t radio_payload = MOD_MOTION;
475 radio_trigger_memorize_data (&radio_payload,
sizeof(modules_t));
479 sensebe_config.tssp_conf.detect_window;
482 motion_state = MOTION_SYNC;
483 arr_is_mod_on[MOD_MOTION] =
true;
486 void motion_module_add_ticks ()
488 log_printf(
"Machine State : %d\n", motion_state);
489 if(arr_is_light_ok [MOD_MOTION] ==
false)
491 motion_state = MOTION_STOP;
493 else if(motion_state == MOTION_STOP)
495 motion_state = MOTION_SYNC;
497 arr_state_change[motion_state]();
500 void motion_module_add_mod_ticks ()
502 static bool is_rx_on =
true;
503 static uint32_t mod_ticks;
504 mod_ticks += arr_module_tick_duration[sensebe_config.speed];
505 if(motion_state == MOTION_SYNC)
507 if(is_rx_on ==
true && mod_ticks >= MOTION_SYNC_ON_TIME)
513 else if(is_rx_on ==
false && mod_ticks >= MOTION_SYNC_OFF_TIME)
520 else if(motion_state == MOTION_IDLE)
526 void motion_module_stop ()
528 motion_state = MOTION_STOP;
529 arr_state_change[motion_state] ();
531 arr_is_mod_on [MOD_MOTION] =
false;
532 arr_is_light_sense_req[MOD_MOTION] =
false;
533 arr_is_light_ok[MOD_MOTION] =
false;
536 void timer_module_start ()
538 oper_time_t timer_oper_time = sensebe_config.timer_conf.oper_time;
540 if((timer_oper_time.day_or_night == 1 && timer_oper_time.threshold == 0b0000000)||
541 (timer_oper_time.day_or_night == 0 && timer_oper_time.threshold == 0b1111111))
543 arr_is_light_sense_req[MOD_TIMER] =
false;
544 arr_is_light_ok [MOD_TIMER] =
true;
548 arr_is_light_sense_req[MOD_TIMER] =
true;
553 .trig_duration_100ms = 0,
554 .trig_mode = sensebe_config.cam_trigs[TIMER_ALL].mode,
555 .trig_param1 = sensebe_config.cam_trigs[TIMER_ALL].larger_value,
556 .trig_param2 = sensebe_config.cam_trigs[TIMER_ALL].smaller_value,
557 .pre_focus_en = (bool)sensebe_config.cam_trigs[TIMER_ALL].pre_focus,
561 timer_module_value = sensebe_config.timer_conf.timer_interval * 100;
563 arr_is_mod_on[MOD_TIMER] =
true;
567 void timer_module_add_ticks ()
569 if(arr_is_light_ok [MOD_TIMER] ==
true)
571 arr_is_mod_on[MOD_TIMER] =
true;
575 arr_is_mod_on[MOD_TIMER] =
false;
579 void timer_module_add_mod_ticks ()
581 static uint32_t mod_ticks;
582 mod_ticks += arr_module_tick_duration[sensebe_config.speed];
583 if(mod_ticks >= timer_module_value)
585 timer_trigger_handler ();
590 void timer_module_stop ()
592 arr_is_mod_on [MOD_TIMER] =
false;
593 arr_is_light_sense_req[MOD_TIMER] =
false;
594 arr_is_light_ok[MOD_TIMER] =
false;
597 void module_tick_handler ()
599 if(arr_is_mod_on[MOD_TIMER] ==
true)
601 timer_module_add_mod_ticks ();
603 if(arr_is_mod_on[MOD_MOTION] ==
true)
605 motion_module_add_mod_ticks ();
611 log_printf(
"%s\n", __func__);
614 light_check_sense_pin = sensebe_rx_detect_config->light_sense_config.photodiode_pin;
616 light_check_en_pin = sensebe_rx_detect_config->light_sense_config.photodiode_en_pin;
617 hal_gpio_cfg_output (light_check_en_pin, 0);
619 memcpy (&sensebe_config, sensebe_rx_detect_config->sensebe_config,
620 sizeof(sensebe_config_t));
625 .tssp_missed_handler = window_detect_handler,
626 .tssp_detect_handler = pulse_detect_handler,
627 .rx_en_pin = sensebe_rx_detect_config->rx_detect_config.rx_en_pin,
628 .rx_in_pin = sensebe_rx_detect_config->rx_detect_config.rx_out_pin,
629 .window_duration_ticks =
630 (sensebe_config.tssp_conf.detect_window ),
632 memcpy (&tssp_detect_config, &local_tssp_detect_config,
638 .focus_pin = sensebe_rx_detect_config->cam_config.focus_pin_no,
639 .trigger_pin = sensebe_rx_detect_config->cam_config.trigger_pin_no
647 log_printf(
"%s\n", __func__);
648 feedback_timepassed = 0;
650 sizeof(sensebe_config_t)) != 0)
657 log_printf(
" Trig Config : %d\n ", sensebe_config.trig_conf);
661 timer_module_start ();
665 timer_module_stop ();
668 if((sensebe_config.trig_conf != TIMER_ONLY))
670 motion_module_start ();
674 motion_module_stop ();
680 , module_tick_handler);
682 light_sense_add_ticks (LIGHT_SENSE_INTERVAL_TICKS);
687 log_printf(
"%s\n", __func__);
688 radio_trigger_shut ();
690 motion_module_stop ();
691 timer_module_stop ();
697 add_ticks_feedback (interval);
699 if(arr_is_light_sense_req [MOD_MOTION] ==
true ||
700 arr_is_light_sense_req [MOD_TIMER] ==
true)
702 light_sense_add_ticks (interval);
705 if((sensebe_config.trig_conf != TIMER_ONLY))
707 motion_module_add_ticks ();
711 timer_module_add_ticks ();
717 memcpy (&sensebe_config, update_sensebe_config,
sizeof(sensebe_config_t));
722 return &sensebe_config;
void(* cam_trigger_done_handler)(uint32_t done_state)
void tssp_detect_window_stop(void)
Function to stop IR missed window detection.
sensebe_config_t * sensebe_tx_rx_last_config()
Function to get last config which is being used.
uint32_t simple_adc_get_value(simple_adc_gain_t gain, simple_adc_input_t pin)
This function initializes the SAADC peripheral, gets an ADC value and then deinitializes The function...
void tssp_detect_pulse_detect()
Function to start module is pulse detecting mode.
#define TSSP_DETECT_TICKS_MS(ms)
void sensebe_tx_rx_update_config(sensebe_config_t *update_sensebe_config)
Function to update SenseBe Rx configuration to config received over BLE.
void led_ui_loop_start(led_sequences seq, led_ui_priority_t priority)
Start a sequence to play repeatedly.
void sensebe_tx_rx_stop(void)
Function to disable detection for SenseBe Rx.
void sensebe_store_config_write(sensebe_config_t *latest_config)
Function to write the sensebe_config_t at address location received from get_next_location().
Structure to store information corresponding to hardware and application.
void led_ui_single_start(led_sequences seq, led_ui_priority_t priority, bool reset)
Start a sequence to play once.
void cam_trigger(uint32_t setup_number)
Function to trigger the camera with given setup number.
bool cam_trigger_is_on(void)
Function to check status of cam_trigger module.
void sensebe_tx_rx_start(void)
Function to enable detection for SenseBe Rx.
Repeated call of the timer.
void tssp_detect_init(tssp_detect_config_t *tssp_detect_config)
Function to initialize IR detect sub-module.
#define MS_TIMER_TICKS_MS(ms)
void tssp_detect_window_sync(uint32_t sync_ms)
Function to Synchronize TSSP detector to IR transmitter which is being used.
sensebe_config_t * sensebe_store_config_get_last_config()
Structure to store information required to use this module.
void ms_timer_start(ms_timer_num id, ms_timer_mode mode, uint64_t ticks, void(*handler)(void))
void cam_trigger_stop(void)
Function to stop camera trigger.
void sensebe_tx_rx_add_ticks(uint32_t interval)
Function to handle add tick event.
void cam_trigger_set_trigger(cam_trigger_config_t *cam_trigger_config)
Function to set a camera trigger.
uint32_t window_duration_ticks
void tssp_detect_pulse_stop()
Function to stop IR pulse detection.
void cam_trigger_init(cam_trigger_setup_t *cam_trigger_setup)
Function to initiate output pins.
Structure to store information related to camera trigger.
void tssp_detect_window_detect()
Function to start IR pulse detection.
void sensebe_tx_rx_init(sensebe_tx_rx_config_t *sensebe_rx_detect_config)
Function to initialize the Rx detect module.
void ms_timer_stop(ms_timer_num id)
void led_ui_stop_seq(led_ui_seq_t type, led_sequences seq)
Stops a particular sequence of a particular type.