20 #include "sensebe_ble.h" 21 #include "sensebe_tx_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 54 #define RADIO_ON_FREQ_MS 50 102 TX_EN = SENSEBE_TX_BOARD,
104 RX_EN = SENSEBE_RX_BOARD
109 static uint32_t feedback_timepassed = 0;
111 static sensebe_config_t sensebe_config;
113 static uint32_t arr_module_tick_duration[] = {MOD_FREQ0, MOD_FREQ1, MOD_FREQ2, MOD_FREQ3};
115 static bool arr_is_light_ok [MAX_MODS];
117 static bool arr_is_light_sense_req [MAX_MODS];
119 static bool arr_is_mod_on[MAX_MODS];
121 static uint32_t timer_module_value = 0;
123 static led_sequences arr_range_indicator[] =
124 {LED_SEQ_RED_PULSE, LED_SEQ_ORANGE_PULSE, LED_SEQ_GREEN_PULSE,};
126 static ir_ranges_t tx_range = IR_SHORT;
128 static uint32_t ir_pwr1 = 0, ir_pwr2 =0;
130 static uint32_t light_check_sense_pin = 0;
132 static uint32_t light_check_en_pin = 0;
134 static uint32_t radio_mod_ticks_freq = 0;
139 void timer_module_start (
void);
141 void timer_module_add_ticks (
void);
143 void timer_module_add_mod_ticks (
void);
145 void timer_module_stop (
void);
148 void timer_trigger_handler ();
150 void radio_module_start ();
152 void radio_module_add_mod_ticks ();
154 void radio_module_stop ();
156 void radio_module_trigger_handler (
void * p_trig, uint32_t len);
160 void ir_tx_module_start (
void);
162 void ir_tx_module_add_ticks (
void);
164 void ir_tx_module_add_mod_ticks (
void);
166 void ir_tx_module_stop (
void);
169 void ir_range_short (
void);
171 void ir_range_mid (
void);
173 void ir_range_long (
void);
175 void (* arr_ir_range_select[]) () ={
189 void light_check (oper_time_t oper_time, uint32_t light_intensity, uint32_t module);
194 void light_sense_add_ticks (uint32_t interval);
201 void camera_unit_handler(uint32_t trigger);
206 void add_ticks_feedback (uint32_t interval);
215 void module_tick_handler ();
219 void add_ticks_feedback (uint32_t interval)
221 feedback_timepassed += interval;
222 if(feedback_timepassed >= DETECT_FEEDBACK_TIMEOUT_TICKS)
228 void state_change_sync ()
230 log_printf (
"%s\n", __func__);
234 void state_change_idle ()
236 log_printf (
"%s\n", __func__);
241 void state_change_stop ()
244 log_printf (
"%s\n", __func__);
249 void ir_range_short ()
251 log_printf(
"%s\n",__func__);
252 hal_gpio_pin_write (ir_pwr1, 0);
253 hal_gpio_pin_write (ir_pwr2, 0);
258 log_printf(
"%s\n",__func__);
259 hal_gpio_pin_write (ir_pwr1, 1);
260 hal_gpio_pin_write (ir_pwr2, 0);
263 void ir_range_long ()
265 log_printf(
"%s\n",__func__);
266 hal_gpio_pin_write (ir_pwr1, 1);
267 hal_gpio_pin_write (ir_pwr2, 1);
270 void camera_unit_handler(uint32_t trigger)
272 log_printf(
"%s\n", __func__);
281 void timer_trigger_handler ()
289 void light_check (oper_time_t oper_time, uint32_t light_intensity, uint32_t module)
291 uint8_t light_sense_config = oper_time.day_or_night;
292 uint32_t light_threshold =
293 (uint32_t)((oper_time.threshold) * LIGHT_THRESHOLD_MULTIPLY_FACTOR);
297 if(((light_sense_config == 1) && (light_intensity >= light_threshold))
299 ((light_sense_config == 0) && (light_intensity <= light_threshold)))
302 arr_is_light_ok[module] =
true;
306 arr_is_light_ok[module] =
false;
310 void light_sense_add_ticks (uint32_t interval)
312 static uint32_t timepassed = 0;
313 timepassed += interval;
314 if(timepassed >= LIGHT_SENSE_INTERVAL_TICKS)
316 static uint32_t light_intensity;
318 hal_gpio_pin_set (light_check_en_pin);
320 hal_nop_delay_ms (3);
322 light_check_sense_pin));
324 if(arr_is_light_sense_req[MOD_TIMER])
326 light_check (sensebe_config.timer_conf.oper_time, light_intensity, MOD_TIMER);
329 if(arr_is_light_sense_req[MOD_IR_TX])
331 light_check (sensebe_config.ir_tx_conf.oper_time, light_intensity, MOD_IR_TX);
335 hal_gpio_pin_clear (light_check_en_pin);
340 void radio_module_start ()
342 arr_is_mod_on[MOD_RADIO] = 1;
343 radio_trigger_init_t radio_init =
345 .comm_direction = RADIO_TRIGGER_Rx,
347 .irq_priority = APP_IRQ_PRIORITY_HIGH,
348 .radio_trigger_rx_callback = radio_module_trigger_handler,
351 radio_trigger_init (&radio_init);
356 .pre_focus_en = sensebe_config.cam_trigs[RADIO_ALL].pre_focus,
357 .trig_duration_100ms = 15,
358 .trig_mode = sensebe_config.cam_trigs[RADIO_ALL].mode,
359 .trig_param1 = sensebe_config.cam_trigs[RADIO_ALL].larger_value,
360 .trig_param2 = sensebe_config.cam_trigs[RADIO_ALL].smaller_value,
366 void radio_module_add_mod_ticks ()
368 static uint32_t radio_cnt = 0;
370 if(radio_cnt >= radio_mod_ticks_freq)
372 radio_trigger_listen ();
377 void radio_module_stop ()
379 arr_is_mod_on[MOD_RADIO] = 0;
380 radio_trigger_shut ();
383 void radio_module_trigger_handler (
void * p_trig, uint32_t len)
385 uint32_t * buff = (uint32_t *)p_trig;
386 log_printf(
"%s : %d\n", __func__, buff[0]);
390 void timer_module_start ()
392 oper_time_t timer_oper_time = sensebe_config.timer_conf.oper_time;
394 if((timer_oper_time.day_or_night == 1 && timer_oper_time.threshold == 0b0000000)||
395 (timer_oper_time.day_or_night == 0 && timer_oper_time.threshold == 0b1111111))
397 arr_is_light_sense_req[MOD_TIMER] =
false;
398 arr_is_light_ok [MOD_TIMER] =
true;
402 arr_is_light_sense_req[MOD_TIMER] =
true;
407 .trig_duration_100ms = 0,
408 .trig_mode = sensebe_config.cam_trigs[TIMER_ALL].mode,
409 .trig_param1 = sensebe_config.cam_trigs[TIMER_ALL].larger_value,
410 .trig_param2 = sensebe_config.cam_trigs[TIMER_ALL].smaller_value,
411 .pre_focus_en = (bool)sensebe_config.cam_trigs[TIMER_ALL].pre_focus,
415 timer_module_value = sensebe_config.timer_conf.timer_interval * 100;
417 arr_is_mod_on[MOD_TIMER] =
true;
421 void timer_module_add_ticks ()
423 if(arr_is_light_ok [MOD_TIMER] ==
true)
425 arr_is_mod_on[MOD_TIMER] =
true;
429 arr_is_mod_on[MOD_TIMER] =
false;
433 void timer_module_add_mod_ticks ()
435 static uint32_t mod_ticks;
436 mod_ticks += arr_module_tick_duration[sensebe_config.ir_tx_conf.ir_tx_speed];
437 if(mod_ticks >= timer_module_value)
439 timer_trigger_handler ();
444 void timer_module_stop ()
446 arr_is_mod_on [MOD_TIMER] =
false;
447 arr_is_light_sense_req[MOD_TIMER] =
false;
448 arr_is_light_ok[MOD_TIMER] =
false;
451 void ir_tx_module_start ()
453 oper_time_t ir_tx_oper_time = sensebe_config.ir_tx_conf.oper_time;
455 if((ir_tx_oper_time.day_or_night == 1 && ir_tx_oper_time.threshold == 0b0000000)||
456 (ir_tx_oper_time.day_or_night == 0 && ir_tx_oper_time.threshold == 0b1111111))
458 arr_is_light_sense_req[MOD_IR_TX] =
false;
459 arr_is_light_ok [MOD_IR_TX] =
true;
463 arr_is_light_sense_req[MOD_IR_TX] =
true;
466 if(sensebe_config.ir_tx_conf.ir_tx_pwr == MAX_IR_RANGES)
468 sensebe_config.ir_tx_conf.ir_tx_pwr = IR_LONG;
471 tx_range = sensebe_config.ir_tx_conf.ir_tx_pwr;
472 arr_ir_range_select[tx_range]();
475 arr_is_mod_on[MOD_IR_TX] =
true;
478 void ir_tx_module_add_ticks ()
480 if(arr_is_light_ok[MOD_IR_TX] ==
true)
482 arr_is_mod_on[MOD_IR_TX] =
true;
486 arr_is_mod_on[MOD_IR_TX] =
false;
490 void ir_tx_module_add_mod_ticks ()
495 void ir_tx_module_stop ()
498 arr_is_mod_on[MOD_IR_TX] =
false;
499 arr_is_light_sense_req[MOD_IR_TX] =
false;
500 arr_is_light_ok[MOD_IR_TX] =
false;
503 void module_tick_handler ()
505 if(arr_is_mod_on[MOD_TIMER] ==
true)
507 timer_module_add_mod_ticks ();
509 if(arr_is_mod_on[MOD_IR_TX] ==
true)
511 ir_tx_module_add_mod_ticks ();
513 if(arr_is_mod_on[MOD_RADIO] ==
true)
515 radio_module_add_mod_ticks ();
521 log_printf(
"%s\n", __func__);
524 light_check_sense_pin = sensebe_tx_init->light_sense_config.photodiode_pin;
526 light_check_en_pin = sensebe_tx_init->light_sense_config.photodiode_en_pin;
527 hal_gpio_cfg_output (light_check_en_pin, 0);
531 memcpy (&sensebe_config, sensebe_tx_init->sensebe_config,
532 sizeof(sensebe_config_t));
537 .focus_pin = sensebe_tx_init->cam_config.focus_pin_no,
538 .trigger_pin = sensebe_tx_init->cam_config.trigger_pin_no
542 tssp_ir_tx_init (sensebe_tx_init->tx_transmit_config.tx_en_pin,
543 sensebe_tx_init->tx_transmit_config.tx_in_pin);
545 ir_pwr1 = sensebe_tx_init->tx_transmit_config.tx_pwr1;
546 ir_pwr2 = sensebe_tx_init->tx_transmit_config.tx_pwr2;
549 hal_gpio_cfg_output (ir_pwr1, 0);
550 hal_gpio_cfg_output (ir_pwr2, 0);
556 log_printf(
"%s\n", __func__);
557 feedback_timepassed = 0;
559 sizeof(sensebe_config_t)) != 0)
566 log_printf(
" Trig Config : %d\n ", sensebe_config.trig_conf);
570 timer_module_start ();
574 timer_module_stop ();
577 if ((sensebe_config.trig_conf != TIMER_ONLY))
579 radio_module_start ();
583 radio_module_stop ();
586 if ((sensebe_config.ir_tx_conf.is_enable == 1))
588 ir_tx_module_start ();
592 ir_tx_module_stop ();
595 radio_mod_ticks_freq = RADIO_ON_FREQ_MS/
596 arr_module_tick_duration[sensebe_config.ir_tx_conf.ir_tx_speed];
599 MS_TIMER_TICKS_MS(arr_module_tick_duration[sensebe_config.ir_tx_conf.ir_tx_speed])
600 , module_tick_handler);
602 light_sense_add_ticks (LIGHT_SENSE_INTERVAL_TICKS);
607 log_printf(
"%s\n", __func__);
609 timer_module_stop ();
610 ir_tx_module_stop ();
611 radio_module_stop ();
617 add_ticks_feedback (interval);
619 if(arr_is_light_sense_req [MOD_TIMER] ==
true ||
620 arr_is_light_sense_req [MOD_IR_TX] ==
true)
622 light_sense_add_ticks (interval);
627 timer_module_add_ticks ();
629 if((sensebe_config.ir_tx_conf.is_enable == 1))
631 ir_tx_module_add_ticks ();
637 memcpy (&sensebe_config, update_sensebe_config,
sizeof(sensebe_config_t));
642 return &sensebe_config;
647 tx_range = (tx_range + 1)%MAX_IR_RANGES;
650 arr_ir_range_select[tx_range]();
651 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 sensebe_tx_rx_update_config(sensebe_config_t *update_sensebe_config)
Function to update SenseBe Rx configuration to config received over BLE.
void sensebe_tx_rx_init(sensebe_tx_config_t *sensebe_tx_init)
Function to initialize the Rx detect module.
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.
#define MS_TIMER_TICKS_MS(ms)
sensebe_config_t * sensebe_store_config_get_last_config()
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.
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 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.