20 #include "sensebe_ble.h" 21 #include "sensebe_tx_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 "hal_radio.h" 37 #include "radio_trigger.h" 41 #define DETECT_FEEDBACK_TIMEOUT_TICKS MS_TIMER_TICKS_MS(270000) 43 #define LIGHT_THRESHOLD_MULTIPLY_FACTOR 32 45 #define MAX_ADC_OUTPUT 4096 47 #define LIGHT_SENSE_INTERVAL_TICKS MS_TIMER_TICKS_MS(300000) 49 #define PULSE_REQ_FOR_SYNC 4 51 #define MOTION_SYNC_ON_TIME 200 53 #define MOTION_SYNC_OFF_TIME 800 55 #define RADIO_TX_ON_TIME_MS 102 57 #define RADIO_TX_FREQ_MS 1 59 #define RADIO_RX_ON_TIME_MS 1 61 #define RADIO_RX_FREQ_MS 50 63 #define MS_TIMER_TICKS_500US MS_TIMER_TICKS_MS(1)/2 76 }motion_detection_states_t;
125 TX_EN = SENSEBE_TX_BOARD,
127 RX_EN = SENSEBE_RX_BOARD
132 static uint32_t feedback_timepassed = 0;
134 static sensebe_config_t sensebe_config;
136 static uint32_t arr_module_tick_duration[] = {MOD_FREQ0, MOD_FREQ1, MOD_FREQ2, MOD_FREQ3};
141 static bool arr_is_light_ok [MAX_MODS];
143 static bool arr_is_light_sense_req [MAX_MODS];
145 static bool arr_is_mod_on[MAX_MODS];
149 static uint32_t tssp_detect_sync_time;
151 static motion_detection_states_t motion_state = MOTION_IDLE;
153 static uint32_t timer_module_value = 0;
155 static led_sequences arr_range_indicator[] =
156 {LED_SEQ_RED_PULSE, LED_SEQ_ORANGE_PULSE, LED_SEQ_GREEN_PULSE,};
158 static ir_ranges_t tx_range = IR_SHORT;
160 static uint32_t ir_pwr1 = 0, ir_pwr2 =0;
162 static uint32_t light_check_sense_pin = 0;
164 static uint32_t light_check_en_pin = 0;
167 static rx_tx_mod_en_t MOD_FUNC_SEL = RX_EN;
169 static uint32_t radio_on_mod_ticks;
171 static uint32_t radio_ticks;
176 void wireless_trig_rx_handler (
void * buff, uint32_t len);
178 void radio_module_start ();
179 void radio_module_add_mod_ticks ();
180 void radio_module_stop ();
183 void motion_module_start (
void);
185 void motion_module_add_ticks (
void);
187 void motion_module_add_mod_ticks (
void);
189 void motion_module_stop (
void);
192 void state_change_sync (
void);
194 void state_change_idle (
void);
196 void state_change_stop (
void);
198 void (* arr_state_change[]) () = {
205 void window_detect_handler ();
207 void pulse_detect_handler (uint32_t ticks_count);
212 void timer_module_start (
void);
214 void timer_module_add_ticks (
void);
216 void timer_module_add_mod_ticks (
void);
218 void timer_module_stop (
void);
221 void timer_trigger_handler ();
225 void ir_tx_module_start (
void);
227 void ir_tx_module_add_ticks (
void);
229 void ir_tx_module_add_mod_ticks (
void);
231 void ir_tx_module_stop (
void);
234 void ir_range_short (
void);
236 void ir_range_mid (
void);
238 void ir_range_long (
void);
240 void (* arr_ir_range_select[]) () ={
254 void light_check (oper_time_t oper_time, uint32_t light_intensity, uint32_t module);
259 void light_sense_add_ticks (uint32_t interval);
266 void camera_unit_handler(uint32_t trigger);
276 bool compare_margin(uint32_t data, uint32_t ref, uint32_t margin);
284 bool three_window_sync (uint32_t ticks);
289 void add_ticks_feedback (uint32_t interval);
298 void module_tick_handler ();
302 void add_ticks_feedback (uint32_t interval)
304 feedback_timepassed += interval;
305 if(feedback_timepassed >= DETECT_FEEDBACK_TIMEOUT_TICKS)
311 void state_change_sync ()
313 log_printf (
"%s\n", __func__);
317 void state_change_idle ()
319 log_printf (
"%s\n", __func__);
324 void state_change_stop ()
327 log_printf (
"%s\n", __func__);
332 void ir_range_short ()
334 log_printf(
"%s\n",__func__);
335 hal_gpio_pin_write (ir_pwr1, 0);
336 hal_gpio_pin_write (ir_pwr2, 0);
341 log_printf(
"%s\n",__func__);
342 hal_gpio_pin_write (ir_pwr1, 1);
343 hal_gpio_pin_write (ir_pwr2, 0);
346 void ir_range_long ()
348 log_printf(
"%s\n",__func__);
349 hal_gpio_pin_write (ir_pwr1, 1);
350 hal_gpio_pin_write (ir_pwr2, 1);
353 void camera_unit_handler(uint32_t trigger)
365 void timer_trigger_handler ()
373 void wireless_trig_rx_handler (
void * buff, uint32_t len)
375 uint8_t * temp_buff = (uint8_t * )buff;
379 void window_detect_handler ()
383 motion_state = MOTION_SYNC;
384 arr_state_change[motion_state] ();
389 radio_trigger_yell ();
393 bool compare_margin(uint32_t data, uint32_t ref, uint32_t margin)
395 if((ref-margin)<=data && data<=(ref+margin))
405 bool validate_and_sync (uint32_t ticks)
407 for(uint32_t freq_cmp = 0; freq_cmp < MAX_MOD_FREQ; freq_cmp++)
417 bool three_window_sync (uint32_t ticks)
419 static uint32_t previous_pulse_tick = 0, current_pulse_tick = 0;
420 static uint32_t pulse_diff_window[] = {0,0,0};
421 static uint32_t pulse_cnt = PULSE_REQ_FOR_SYNC;
422 static bool flag =
false;
423 if(pulse_cnt == PULSE_REQ_FOR_SYNC)
426 previous_pulse_tick = ticks;
429 else if(pulse_cnt > 0)
431 current_pulse_tick = ticks;
432 pulse_diff_window[pulse_cnt - 1] = ((current_pulse_tick + (1<<24))- previous_pulse_tick)
434 previous_pulse_tick = current_pulse_tick;
437 else if(pulse_cnt == 0)
439 pulse_cnt = PULSE_REQ_FOR_SYNC;
442 log_printf(
"Window[0]: %d\n", pulse_diff_window[0]);
443 log_printf(
"Window[1]: %d\n", pulse_diff_window[1]);
444 log_printf(
"Window[2]: %d\n", pulse_diff_window[2]);
445 flag = (validate_and_sync (pulse_diff_window[0])
446 && validate_and_sync (pulse_diff_window[1])
447 && validate_and_sync (pulse_diff_window[2])
453 tssp_detect_sync_time = pulse_diff_window[1];
454 pulse_diff_window[0] = 0;
455 pulse_diff_window[1] = 0;
456 pulse_diff_window[2] = 0;
461 void pulse_detect_handler (uint32_t ticks_count)
463 if(motion_state == MOTION_SYNC)
465 if(three_window_sync (ticks_count))
468 motion_state = MOTION_IDLE;
469 arr_state_change[motion_state] ();
470 if(feedback_timepassed < DETECT_FEEDBACK_TIMEOUT_TICKS)
487 void light_check (oper_time_t oper_time, uint32_t light_intensity, uint32_t module)
489 uint8_t light_sense_config = oper_time.day_or_night;
490 uint32_t light_threshold =
491 (uint32_t)((oper_time.threshold) * LIGHT_THRESHOLD_MULTIPLY_FACTOR);
495 if(((light_sense_config == 1) && (light_intensity >= light_threshold))
497 ((light_sense_config == 0) && (light_intensity <= light_threshold)))
500 arr_is_light_ok[module] =
true;
504 arr_is_light_ok[module] =
false;
508 void light_sense_add_ticks (uint32_t interval)
510 static uint32_t timepassed = 0;
511 timepassed += interval;
512 if(timepassed >= LIGHT_SENSE_INTERVAL_TICKS)
514 static uint32_t light_intensity;
516 hal_gpio_pin_set (light_check_en_pin);
518 hal_nop_delay_ms (3);
520 light_check_sense_pin));
522 if(arr_is_light_sense_req[MOD_MOTION])
524 light_check (sensebe_config.tssp_conf.oper_time, light_intensity, MOD_MOTION);
528 if(arr_is_light_sense_req[MOD_TIMER])
530 light_check (sensebe_config.timer_conf.oper_time, light_intensity, MOD_TIMER);
533 if(arr_is_light_sense_req[MOD_IR_TX])
535 light_check (sensebe_config.ir_tx_conf.oper_time, light_intensity, MOD_IR_TX);
539 hal_gpio_pin_clear (light_check_en_pin);
544 void radio_module_start ()
550 .trig_duration_100ms = sensebe_config.tssp_conf.intr_trig_timer,
551 .trig_mode = sensebe_config.tssp_conf.cam_oper.mode,
552 .trig_param1 = sensebe_config.tssp_conf.cam_oper.larger_value,
553 .trig_param2 = sensebe_config.tssp_conf.cam_oper.smaller_value,
554 .pre_focus_en = (bool)sensebe_config.timer_conf.cam_oper.pre_focus,
560 void radio_module_add_mod_ticks ()
563 if(radio_ticks >= radio_on_mod_ticks)
566 radio_trigger_listen ();
570 void motion_module_start ()
572 oper_time_t motion_oper_time = sensebe_config.tssp_conf.oper_time;
574 if((motion_oper_time.day_or_night == 1 && motion_oper_time.threshold == 0b0000000)||
575 (motion_oper_time.day_or_night == 0 && motion_oper_time.threshold == 0b1111111))
577 arr_is_light_sense_req[MOD_MOTION] =
false;
578 arr_is_light_ok [MOD_MOTION] =
true;
582 arr_is_light_sense_req[MOD_MOTION] =
true;
587 .trig_duration_100ms = sensebe_config.tssp_conf.intr_trig_timer,
588 .trig_mode = sensebe_config.tssp_conf.cam_oper.mode,
589 .trig_param1 = sensebe_config.tssp_conf.cam_oper.larger_value,
590 .trig_param2 = sensebe_config.tssp_conf.cam_oper.smaller_value,
591 .pre_focus_en = (bool)sensebe_config.timer_conf.cam_oper.pre_focus,
596 sensebe_config.tssp_conf.detect_window;
599 motion_state = MOTION_SYNC;
600 arr_is_mod_on[MOD_MOTION] =
true;
603 void motion_module_add_ticks ()
605 log_printf(
"Machine State : %d\n", motion_state);
606 if(arr_is_light_ok [MOD_MOTION] ==
false)
608 motion_state = MOTION_STOP;
610 else if(motion_state == MOTION_STOP)
612 motion_state = MOTION_SYNC;
614 arr_state_change[motion_state]();
617 void motion_module_add_mod_ticks ()
619 static bool is_rx_on =
true;
620 static uint32_t mod_ticks;
621 mod_ticks += arr_module_tick_duration[sensebe_config.ir_tx_conf.ir_tx_speed];
622 if(motion_state == MOTION_SYNC)
624 if(is_rx_on ==
true && mod_ticks >= MOTION_SYNC_ON_TIME)
630 else if(is_rx_on ==
false && mod_ticks >= MOTION_SYNC_OFF_TIME)
637 else if(motion_state == MOTION_IDLE)
643 void motion_module_stop ()
645 motion_state = MOTION_STOP;
646 arr_state_change[motion_state] ();
648 arr_is_mod_on [MOD_MOTION] =
false;
649 arr_is_light_sense_req[MOD_MOTION] =
false;
650 arr_is_light_ok[MOD_MOTION] =
false;
653 void timer_module_start ()
655 oper_time_t timer_oper_time = sensebe_config.timer_conf.oper_time;
657 if((timer_oper_time.day_or_night == 1 && timer_oper_time.threshold == 0b0000000)||
658 (timer_oper_time.day_or_night == 0 && timer_oper_time.threshold == 0b1111111))
660 arr_is_light_sense_req[MOD_TIMER] =
false;
661 arr_is_light_ok [MOD_TIMER] =
true;
665 arr_is_light_sense_req[MOD_TIMER] =
true;
670 .trig_duration_100ms = 0,
671 .trig_mode = sensebe_config.timer_conf.cam_oper.mode,
672 .trig_param1 = sensebe_config.timer_conf.cam_oper.larger_value,
673 .trig_param2 = sensebe_config.timer_conf.cam_oper.smaller_value,
674 .pre_focus_en = (bool)sensebe_config.timer_conf.cam_oper.pre_focus,
678 timer_module_value = sensebe_config.timer_conf.timer_interval * 100;
680 arr_is_mod_on[MOD_TIMER] =
true;
684 void timer_module_add_ticks ()
686 if(arr_is_light_ok [MOD_TIMER] ==
true)
688 arr_is_mod_on[MOD_TIMER] =
true;
692 arr_is_mod_on[MOD_TIMER] =
false;
696 void timer_module_add_mod_ticks ()
698 static uint32_t mod_ticks;
699 mod_ticks += arr_module_tick_duration[sensebe_config.ir_tx_conf.ir_tx_speed];
700 if(mod_ticks >= timer_module_value)
702 timer_trigger_handler ();
707 void timer_module_stop ()
709 arr_is_mod_on [MOD_TIMER] =
false;
710 arr_is_light_sense_req[MOD_TIMER] =
false;
711 arr_is_light_ok[MOD_TIMER] =
false;
714 void ir_tx_module_start ()
716 oper_time_t ir_tx_oper_time = sensebe_config.ir_tx_conf.oper_time;
718 if((ir_tx_oper_time.day_or_night == 1 && ir_tx_oper_time.threshold == 0b0000000)||
719 (ir_tx_oper_time.day_or_night == 0 && ir_tx_oper_time.threshold == 0b1111111))
721 arr_is_light_sense_req[MOD_IR_TX] =
false;
722 arr_is_light_ok [MOD_IR_TX] =
true;
726 arr_is_light_sense_req[MOD_IR_TX] =
true;
729 if(sensebe_config.ir_tx_conf.ir_tx_pwr == MAX_IR_RANGES)
731 sensebe_config.ir_tx_conf.ir_tx_pwr = IR_LONG;
734 tx_range = sensebe_config.ir_tx_conf.ir_tx_pwr;
735 arr_ir_range_select[tx_range]();
738 arr_is_mod_on[MOD_IR_TX] =
true;
741 void ir_tx_module_add_ticks ()
743 if(arr_is_light_ok[MOD_IR_TX] ==
true)
745 arr_is_mod_on[MOD_IR_TX] =
true;
749 arr_is_mod_on[MOD_IR_TX] =
false;
753 void ir_tx_module_add_mod_ticks ()
758 void ir_tx_module_stop ()
761 arr_is_mod_on[MOD_IR_TX] =
false;
762 arr_is_light_sense_req[MOD_IR_TX] =
false;
763 arr_is_light_ok[MOD_IR_TX] =
false;
766 void module_tick_handler ()
768 if(arr_is_mod_on[MOD_TIMER] ==
true)
770 timer_module_add_mod_ticks ();
772 if(arr_is_mod_on[MOD_MOTION] ==
true)
774 motion_module_add_mod_ticks ();
776 if(arr_is_mod_on[MOD_IR_TX] ==
true)
778 ir_tx_module_add_mod_ticks ();
780 if(MOD_FUNC_SEL == TX_EN)
782 radio_module_add_mod_ticks ();
788 log_printf(
"%s\n", __func__);
791 light_check_sense_pin = sensebe_rx_detect_config->rx_detect_config.photodiode_pin;
793 light_check_en_pin = sensebe_rx_detect_config->rx_detect_config.photodiode_en_pin;
794 hal_gpio_cfg_output (light_check_en_pin, 0);
797 hal_nop_delay_us (1);
798 MOD_FUNC_SEL = hal_gpio_pin_read (sensebe_rx_detect_config->rx_tx_sel);
799 log_printf(
"MOD_FUNC_SEL %d\n",MOD_FUNC_SEL);
802 memcpy (&sensebe_config, sensebe_rx_detect_config->sensebe_config,
803 sizeof(sensebe_config_t));
804 log_printf(
"%s\n", __func__);
809 .tssp_missed_handler = window_detect_handler,
810 .tssp_detect_handler = pulse_detect_handler,
811 .rx_en_pin = sensebe_rx_detect_config->rx_detect_config.rx_en_pin,
812 .rx_in_pin = sensebe_rx_detect_config->rx_detect_config.rx_out_pin,
813 .window_duration_ticks =
814 (sensebe_config.tssp_conf.detect_window ),
816 memcpy (&tssp_detect_config, &local_tssp_detect_config,
822 .focus_pin = sensebe_rx_detect_config->rx_detect_config.focus_pin_no,
823 .trigger_pin = sensebe_rx_detect_config->rx_detect_config.trigger_pin_no
827 if(MOD_FUNC_SEL == RX_EN)
829 radio_trigger_init_t radio_init =
831 .comm_direction = RADIO_TRIGGER_Tx,
833 .irq_priority = APP_IRQ_PRIORITY_HIGH,
834 .tx_on_freq_us = 500,
835 .tx_on_time_ms = 100,
837 radio_trigger_init (&radio_init);
841 radio_trigger_init_t radio_init =
843 .comm_direction = RADIO_TRIGGER_Rx,
845 .irq_priority = APP_IRQ_PRIORITY_HIGH,
846 .radio_trigger_rx_callback = wireless_trig_rx_handler,
849 radio_trigger_init (&radio_init);
850 log_printf(
"Radio init : %d\n", radio_init.comm_direction);
853 static modules_t mod_trig = MOD_MOTION;
854 radio_trigger_memorize_data (&mod_trig,
sizeof(modules_t));
856 tssp_ir_tx_init (sensebe_rx_detect_config->tx_transmit_config.tx_en_pin,
857 sensebe_rx_detect_config->tx_transmit_config.tx_in_pin);
859 ir_pwr1 = sensebe_rx_detect_config->tx_transmit_config.tx_pwr1;
860 ir_pwr2 = sensebe_rx_detect_config->tx_transmit_config.tx_pwr2;
863 hal_gpio_cfg_output (ir_pwr1, 0);
864 hal_gpio_cfg_output (ir_pwr2, 0);
870 feedback_timepassed = 0;
871 log_printf(
"%s\n", __func__);
873 sizeof(sensebe_config_t)) != 0)
880 log_printf(
" Trig Config : %d\n ", sensebe_config.trig_conf);
882 if((MOD_FUNC_SEL == RX_EN) && (sensebe_config.trig_conf !=
MOTION_ONLY))
884 timer_module_start ();
888 timer_module_stop ();
891 if((MOD_FUNC_SEL == RX_EN) && (sensebe_config.trig_conf != TIMER_ONLY))
893 motion_module_start ();
897 motion_module_stop ();
900 if ((MOD_FUNC_SEL == TX_EN) && (sensebe_config.ir_tx_conf.is_enable == 1))
902 ir_tx_module_start ();
906 ir_tx_module_stop ();
909 if(MOD_FUNC_SEL == TX_EN)
911 radio_module_start ();
912 radio_on_mod_ticks = RADIO_RX_FREQ_MS/arr_module_tick_duration[sensebe_config.ir_tx_conf.ir_tx_speed];
916 MS_TIMER_TICKS_MS(arr_module_tick_duration[sensebe_config.ir_tx_conf.ir_tx_speed])
917 , module_tick_handler);
919 light_sense_add_ticks (LIGHT_SENSE_INTERVAL_TICKS);
924 log_printf(
"%s\n", __func__);
926 motion_module_stop ();
927 timer_module_stop ();
928 ir_tx_module_stop ();
929 radio_trigger_shut ();
935 add_ticks_feedback (interval);
937 if(arr_is_light_sense_req [MOD_MOTION] ==
true ||
938 arr_is_light_sense_req [MOD_TIMER] ==
true ||
939 arr_is_light_sense_req [MOD_IR_TX] ==
true)
941 light_sense_add_ticks (interval);
944 if((MOD_FUNC_SEL == RX_EN) && (sensebe_config.trig_conf != TIMER_ONLY))
946 motion_module_add_ticks ();
948 if((MOD_FUNC_SEL == RX_EN) && (sensebe_config.trig_conf !=
MOTION_ONLY))
950 timer_module_add_ticks ();
952 if((MOD_FUNC_SEL == TX_EN) && (sensebe_config.ir_tx_conf.is_enable == 1))
954 ir_tx_module_add_ticks ();
960 memcpy (&sensebe_config, update_sensebe_config,
sizeof(sensebe_config_t));
965 return &sensebe_config;
970 tx_range = (tx_range + 1)%MAX_IR_RANGES;
973 arr_ir_range_select[tx_range]();
974 sensebe_config.ir_tx_conf.ir_tx_pwr = tx_range;
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().
void sensebe_tx_rx_swicht_range()
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.