Appiko
sensebe_tx_rx_mod.c
1 /*
2  * sensebe_tx_rx_mod.c : Module to handle SenseBe's Tx Rx functionalities
3  * Copyright (C) 2019 Appiko
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
19 
20 #include "sensebe_ble.h"
21 #include "sensebe_tx_rx_mod.h"
22 #include "sensebe_store_config.h"
23 
24 #include "hal_gpio.h"
25 #include "ms_timer.h"
26 #include "log.h"
27 #include "led_ui.h"
28 #include "led_seq.h"
29 #include "tssp_detect.h"
30 #include "device_tick.h"
31 #include "cam_trigger.h"
32 #include "simple_adc.h"
33 #include "string.h"
34 #include "hal_nop_delay.h"
35 #include "tssp_ir_tx.h"
36 #include "hal_radio.h"
37 #include "radio_trigger.h"
38 
39 /***********MACROS***********/
41 #define DETECT_FEEDBACK_TIMEOUT_TICKS MS_TIMER_TICKS_MS(270000)
42 
43 #define LIGHT_THRESHOLD_MULTIPLY_FACTOR 32
44 
45 #define MAX_ADC_OUTPUT 4096
46 
47 #define LIGHT_SENSE_INTERVAL_TICKS MS_TIMER_TICKS_MS(300000)
48 
49 #define PULSE_REQ_FOR_SYNC 4
50 
51 #define MOTION_SYNC_ON_TIME 200
52 
53 #define MOTION_SYNC_OFF_TIME 800
54 
55 #define RADIO_TX_ON_TIME_MS 102
56 
57 #define RADIO_TX_FREQ_MS 1
58 
59 #define RADIO_RX_ON_TIME_MS 1
60 
61 #define RADIO_RX_FREQ_MS 50
62 
63 #define MS_TIMER_TICKS_500US MS_TIMER_TICKS_MS(1)/2
64 /***********ENUMS***********/
66 typedef enum
67 {
69  MOTION_SYNC,
71  MOTION_IDLE,
73  MOTION_STOP,
75  MAX_MOTION_STATE
76 }motion_detection_states_t;
77 
79 typedef enum
80 {
82  MOD_TIMER,
84  MOD_MOTION,
86  MOD_IR_TX,
88  MOD_RADIO,
90  MAX_MODS,
91 }modules_t;
92 
94 typedef enum
95 {
97  IR_SHORT,
99  IR_MID,
101  IR_LONG,
103  MAX_IR_RANGES,
104 }ir_ranges_t;
105 
107 typedef enum
108 {
110  MOD_FREQ0 = (5),
112  MOD_FREQ1 = (25),
114  MOD_FREQ2 = (50),
116  MOD_FREQ3 = (100),
118  MAX_MOD_FREQ = 4,
119 }module_freq_t;
120 
122 typedef enum
123 {
125  TX_EN = SENSEBE_TX_BOARD,
127  RX_EN = SENSEBE_RX_BOARD
128 }rx_tx_mod_en_t;
129 
130 /***********VARIABLE***********/
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};
138 static uint32_t arr_sync_validation_ticks[] = {TSSP_DETECT_TICKS_MS(MOD_FREQ0),
139 TSSP_DETECT_TICKS_MS(MOD_FREQ1), TSSP_DETECT_TICKS_MS(MOD_FREQ2), TSSP_DETECT_TICKS_MS(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];
147 static tssp_detect_config_t tssp_detect_config;
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;
165 
167 static rx_tx_mod_en_t MOD_FUNC_SEL = RX_EN;
168 
169 static uint32_t radio_on_mod_ticks;
170 
171 static uint32_t radio_ticks;
172 
173 /***********FUNCTIONS***********/
176 void wireless_trig_rx_handler (void * buff, uint32_t len);
177 
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);
190 
192 void state_change_sync (void);
194 void state_change_idle (void);
196 void state_change_stop (void);
198 void (* arr_state_change[]) () = {
199  state_change_sync,
200  state_change_idle,
201  state_change_stop
202 };
203 
205 void window_detect_handler ();
207 void pulse_detect_handler (uint32_t ticks_count);
208 
209 
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);
219 
221 void timer_trigger_handler ();
222 
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);
232 
234 void ir_range_short (void);
236 void ir_range_mid (void);
238 void ir_range_long (void);
240 void (* arr_ir_range_select[]) () ={
241  ir_range_short,
242  ir_range_mid,
243  ir_range_long,
244 };
245 
247 /***/
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);
260 
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);
290 
298 void module_tick_handler ();
299 
300 
301 /*******************Definitions*******************/
302 void add_ticks_feedback (uint32_t interval)
303 {
304  feedback_timepassed += interval;
305  if(feedback_timepassed >= DETECT_FEEDBACK_TIMEOUT_TICKS)
306  {
307  led_ui_stop_seq (LED_UI_LOOP_SEQ, LED_SEQ_DETECT_PULSE);
308  }
309 }
310 
311 void state_change_sync ()
312 {
313  log_printf ("%s\n", __func__);
315 }
316 
317 void state_change_idle ()
318 {
319  log_printf ("%s\n", __func__);
322 }
323 
324 void state_change_stop ()
325 {
326 
327  log_printf ("%s\n", __func__);
330 }
331 
332 void ir_range_short ()
333 {
334  log_printf("%s\n",__func__);
335  hal_gpio_pin_write (ir_pwr1, 0);
336  hal_gpio_pin_write (ir_pwr2, 0);
337 }
338 
339 void ir_range_mid ()
340 {
341  log_printf("%s\n",__func__);
342  hal_gpio_pin_write (ir_pwr1, 1);
343  hal_gpio_pin_write (ir_pwr2, 0);
344 }
345 
346 void ir_range_long ()
347 {
348  log_printf("%s\n",__func__);
349  hal_gpio_pin_write (ir_pwr1, 1);
350  hal_gpio_pin_write (ir_pwr2, 1);
351 }
352 
353 void camera_unit_handler(uint32_t trigger)
354 {
355  switch(trigger)
356  {
357  case MOD_MOTION :
358  break;
359  case MOD_TIMER :
360  break;
361  }
362 
363 }
364 
365 void timer_trigger_handler ()
366 {
367  if(cam_trigger_is_on () == false)
368  {
369  cam_trigger (MOD_TIMER);
370  }
371 }
372 
373 void wireless_trig_rx_handler (void * buff, uint32_t len)
374 {
375  uint8_t * temp_buff = (uint8_t * )buff;
376  cam_trigger ((uint32_t)temp_buff[0]);
377 }
378 
379 void window_detect_handler ()
380 {
381  led_ui_stop_seq (LED_UI_LOOP_SEQ, LED_SEQ_DETECT_PULSE);
382  led_ui_single_start (LED_SEQ_DETECT_WINDOW, LED_UI_MID_PRIORITY, true);
383  motion_state = MOTION_SYNC;
384  arr_state_change[motion_state] ();
386  cam_trigger (MOD_MOTION);
387 // if(is_radio_trigger_availabel ())
388  {
389  radio_trigger_yell ();
390  }
391 }
392 
393 bool compare_margin(uint32_t data, uint32_t ref, uint32_t margin)
394 {
395  if((ref-margin)<=data && data<=(ref+margin))
396  {
397  return true;
398  }
399  else
400  {
401  return false;
402  }
403 }
404 
405 bool validate_and_sync (uint32_t ticks)
406 {
407  for(uint32_t freq_cmp = 0; freq_cmp < MAX_MOD_FREQ; freq_cmp++)
408  {
409  if(compare_margin (ticks, arr_sync_validation_ticks[freq_cmp], TSSP_DETECT_TICKS_MS(1)))
410  {
411  return true;
412  }
413  }
414  return false;
415 }
416 
417 bool three_window_sync (uint32_t ticks)
418 {
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)
424  {
425  flag = false;
426  previous_pulse_tick = ticks;
427  pulse_cnt --;
428  }
429  else if(pulse_cnt > 0)
430  {
431  current_pulse_tick = ticks;
432  pulse_diff_window[pulse_cnt - 1] = ((current_pulse_tick + (1<<24))- previous_pulse_tick)
433  & 0x00FFFFFF;
434  previous_pulse_tick = current_pulse_tick;
435  pulse_cnt--;
436  }
437  else if(pulse_cnt == 0)
438  {
439  pulse_cnt = PULSE_REQ_FOR_SYNC;
440 
441  }
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])
448  && compare_margin (pulse_diff_window[1], pulse_diff_window[0], TSSP_DETECT_TICKS_MS(2))
449  && compare_margin (pulse_diff_window[2], pulse_diff_window[1], TSSP_DETECT_TICKS_MS(2)));
450 
451  if(flag == true)
452  {
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;
457  }
458  return flag;
459 }
460 
461 void pulse_detect_handler (uint32_t ticks_count)
462 {
463  if(motion_state == MOTION_SYNC)
464  {
465  if(three_window_sync (ticks_count))
466  {
467  tssp_detect_window_sync (tssp_detect_sync_time);
468  motion_state = MOTION_IDLE;
469  arr_state_change[motion_state] ();
470  if(feedback_timepassed < DETECT_FEEDBACK_TIMEOUT_TICKS)
471  {
472  led_ui_loop_start (LED_SEQ_DETECT_PULSE, LED_UI_LOW_PRIORITY);
473  }
474  }
475  else
476  {
478 // if(feedback_timepassed < DETECT_FEEDBACK_TIMEOUT_TICKS)
479 // {
480 // led_ui_single_start (LED_SEQ_DETECT_SYNC, LED_UI_LOW_PRIORITY, true);
481 // }
482  led_ui_stop_seq (LED_UI_LOOP_SEQ, LED_SEQ_DETECT_PULSE);
483  }
484  }
485 }
486 
487 void light_check (oper_time_t oper_time, uint32_t light_intensity, uint32_t module)
488 {
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);
492 
493  //respective light check
494  //Day and its brighter than the threshold
495  if(((light_sense_config == 1) && (light_intensity >= light_threshold))
496  || //Night and its dimmer than the threshold
497  ((light_sense_config == 0) && (light_intensity <= light_threshold)))
498  //assgin to respective light flag
499  {
500  arr_is_light_ok[module] = true;
501  }
502  else
503  {
504  arr_is_light_ok[module] = false;
505  }
506 }
507 
508 void light_sense_add_ticks (uint32_t interval)
509 {
510  static uint32_t timepassed = 0;
511  timepassed += interval;
512  if(timepassed >= LIGHT_SENSE_INTERVAL_TICKS)
513  {
514  static uint32_t light_intensity;
515  //Enable light sense module
516  hal_gpio_pin_set (light_check_en_pin);
517  //Take light reading
518  hal_nop_delay_ms (3);
519  light_intensity = (MAX_ADC_OUTPUT - simple_adc_get_value (SIMPLE_ADC_GAIN1_6,
520  light_check_sense_pin));
521  //motion light check
522  if(arr_is_light_sense_req[MOD_MOTION])
523  {
524  light_check (sensebe_config.tssp_conf.oper_time, light_intensity, MOD_MOTION);
525  }
526 
527  //timer light check
528  if(arr_is_light_sense_req[MOD_TIMER])
529  {
530  light_check (sensebe_config.timer_conf.oper_time, light_intensity, MOD_TIMER);
531  }
532 
533  if(arr_is_light_sense_req[MOD_IR_TX])
534  {
535  light_check (sensebe_config.ir_tx_conf.oper_time, light_intensity, MOD_IR_TX);
536  }
537 
538  //Disable light sense module
539  hal_gpio_pin_clear (light_check_en_pin);
540  timepassed = 0;
541  }
542 }
543 
544 void radio_module_start ()
545 {
546 
547  cam_trigger_config_t cam_trig_config =
548  {
549  .setup_number = MOD_MOTION,
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,
555  };
556  cam_trigger_set_trigger (&cam_trig_config);
557 
558 }
559 
560 void radio_module_add_mod_ticks ()
561 {
562  radio_ticks++;
563  if(radio_ticks >= radio_on_mod_ticks)
564  {
565  radio_ticks = 0;
566  radio_trigger_listen ();
567  }
568 }
569 
570 void motion_module_start ()
571 {
572  oper_time_t motion_oper_time = sensebe_config.tssp_conf.oper_time;
573 
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))
576  {
577  arr_is_light_sense_req[MOD_MOTION] = false;
578  arr_is_light_ok [MOD_MOTION] = true;
579  }
580  else
581  {
582  arr_is_light_sense_req[MOD_MOTION] = true;
583  }
584  cam_trigger_config_t motion_cam_trig_config =
585  {
586  .setup_number = MOD_MOTION,
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,
592  };
593  cam_trigger_set_trigger (&motion_cam_trig_config);
594 
595  tssp_detect_config.window_duration_ticks =
596  sensebe_config.tssp_conf.detect_window;
597  tssp_detect_init (&tssp_detect_config);
598 
599  motion_state = MOTION_SYNC;
600  arr_is_mod_on[MOD_MOTION] = true;
601 }
602 
603 void motion_module_add_ticks ()
604 {
605  log_printf("Machine State : %d\n", motion_state);
606  if(arr_is_light_ok [MOD_MOTION] == false)
607  {
608  motion_state = MOTION_STOP;
609  }
610  else if(motion_state == MOTION_STOP)
611  {
612  motion_state = MOTION_SYNC;
613  }
614  arr_state_change[motion_state]();
615 }
616 
617 void motion_module_add_mod_ticks ()
618 {
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)
623  {
624  if(is_rx_on == true && mod_ticks >= MOTION_SYNC_ON_TIME)
625  {
627  is_rx_on = false;
628  mod_ticks = 0;
629  }
630  else if(is_rx_on == false && mod_ticks >= MOTION_SYNC_OFF_TIME)
631  {
633  is_rx_on = true;
634  mod_ticks = 0;
635  }
636  }
637  else if(motion_state == MOTION_IDLE)
638  {
639  mod_ticks = 0;
640  }
641 }
642 
643 void motion_module_stop ()
644 {
645  motion_state = MOTION_STOP;
646  arr_state_change[motion_state] ();
647 
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;
651 }
652 
653 void timer_module_start ()
654 {
655  oper_time_t timer_oper_time = sensebe_config.timer_conf.oper_time;
656 
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))
659  {
660  arr_is_light_sense_req[MOD_TIMER] = false;
661  arr_is_light_ok [MOD_TIMER] = true;
662  }
663  else
664  {
665  arr_is_light_sense_req[MOD_TIMER] = true;
666  }
667  cam_trigger_config_t timer_cam_trig_config =
668  {
669  .setup_number = MOD_TIMER,
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,
675  };
676  cam_trigger_set_trigger (&timer_cam_trig_config);
677 
678  timer_module_value = sensebe_config.timer_conf.timer_interval * 100;
679 
680  arr_is_mod_on[MOD_TIMER] = true;
681 
682 }
683 
684 void timer_module_add_ticks ()
685 {
686  if(arr_is_light_ok [MOD_TIMER] == true)
687  {
688  arr_is_mod_on[MOD_TIMER] = true;
689  }
690  else
691  {
692  arr_is_mod_on[MOD_TIMER] = false;
693  }
694 }
695 
696 void timer_module_add_mod_ticks ()
697 {
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)
701  {
702  timer_trigger_handler ();
703  mod_ticks = 0;
704  }
705 }
706 
707 void timer_module_stop ()
708 {
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;
712 }
713 
714 void ir_tx_module_start ()
715 {
716  oper_time_t ir_tx_oper_time = sensebe_config.ir_tx_conf.oper_time;
717 
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))
720  {
721  arr_is_light_sense_req[MOD_IR_TX] = false;
722  arr_is_light_ok [MOD_IR_TX] = true;
723  }
724  else
725  {
726  arr_is_light_sense_req[MOD_IR_TX] = true;
727  }
728 
729  if(sensebe_config.ir_tx_conf.ir_tx_pwr == MAX_IR_RANGES)
730  {
731  sensebe_config.ir_tx_conf.ir_tx_pwr = IR_LONG;
732  }
733 
734  tx_range = sensebe_config.ir_tx_conf.ir_tx_pwr;
735  arr_ir_range_select[tx_range]();
736  led_ui_single_start (arr_range_indicator[tx_range], LED_UI_LOW_PRIORITY, true);
737 
738  arr_is_mod_on[MOD_IR_TX] = true;
739 }
740 
741 void ir_tx_module_add_ticks ()
742 {
743  if(arr_is_light_ok[MOD_IR_TX] == true)
744  {
745  arr_is_mod_on[MOD_IR_TX] = true;
746  }
747  else
748  {
749  arr_is_mod_on[MOD_IR_TX] = false;
750  }
751 }
752 
753 void ir_tx_module_add_mod_ticks ()
754 {
755  tssp_ir_tx_start ();
756 }
757 
758 void ir_tx_module_stop ()
759 {
760  tssp_ir_tx_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;
764 }
765 
766 void module_tick_handler ()
767 {
768  if(arr_is_mod_on[MOD_TIMER] == true)
769  {
770  timer_module_add_mod_ticks ();
771  }
772  if(arr_is_mod_on[MOD_MOTION] == true)
773  {
774  motion_module_add_mod_ticks ();
775  }
776  if(arr_is_mod_on[MOD_IR_TX] == true)
777  {
778  ir_tx_module_add_mod_ticks ();
779  }
780  if(MOD_FUNC_SEL == TX_EN)
781  {
782  radio_module_add_mod_ticks ();
783  }
784 }
785 
786 void sensebe_tx_rx_init (sensebe_tx_rx_config_t * sensebe_rx_detect_config)
787 {
788  log_printf("%s\n", __func__);
789 
790  //Assign Enable and sense pins
791  light_check_sense_pin = sensebe_rx_detect_config->rx_detect_config.photodiode_pin;
792  hal_gpio_cfg_input (light_check_sense_pin, HAL_GPIO_PULL_DOWN);
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);
795 
796  hal_gpio_cfg_input (sensebe_rx_detect_config->rx_tx_sel, HAL_GPIO_PULL_DISABLED);
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);
800 
801 
802  memcpy (&sensebe_config, sensebe_rx_detect_config->sensebe_config,
803  sizeof(sensebe_config_t));
804  log_printf("%s\n", __func__);
805 
806  tssp_detect_config_t local_tssp_detect_config =
807  {
808  .detect_logic_level = false,
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 ),
815  };
816  memcpy (&tssp_detect_config, &local_tssp_detect_config,
817  sizeof(tssp_detect_config_t));
818 
819  cam_trigger_setup_t cam_trig_setup =
820  {
821  .cam_trigger_done_handler = camera_unit_handler,
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
824  };
825  cam_trigger_init (&cam_trig_setup);
826 
827  if(MOD_FUNC_SEL == RX_EN)
828  {
829  radio_trigger_init_t radio_init =
830  {
831  .comm_direction = RADIO_TRIGGER_Tx,
832  .comm_freq = 95,
833  .irq_priority = APP_IRQ_PRIORITY_HIGH,
834  .tx_on_freq_us = 500,
835  .tx_on_time_ms = 100,
836  };
837  radio_trigger_init (&radio_init);
838  }
839  else
840  {
841  radio_trigger_init_t radio_init =
842  {
843  .comm_direction = RADIO_TRIGGER_Rx,
844  .comm_freq = 95,
845  .irq_priority = APP_IRQ_PRIORITY_HIGH,
846  .radio_trigger_rx_callback = wireless_trig_rx_handler,
847  .rx_on_time_ms = 1,
848  };
849  radio_trigger_init (&radio_init);
850  log_printf("Radio init : %d\n", radio_init.comm_direction);
851  }
852 
853  static modules_t mod_trig = MOD_MOTION;
854  radio_trigger_memorize_data (&mod_trig, sizeof(modules_t));
855 
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);
858 
859  ir_pwr1 = sensebe_rx_detect_config->tx_transmit_config.tx_pwr1;
860  ir_pwr2 = sensebe_rx_detect_config->tx_transmit_config.tx_pwr2;
861 
862 
863  hal_gpio_cfg_output (ir_pwr1, 0);
864  hal_gpio_cfg_output (ir_pwr2, 0);
865 
866 }
867 
869 {
870  feedback_timepassed = 0;
871  log_printf("%s\n", __func__);
872  if(memcmp (&sensebe_config, sensebe_store_config_get_last_config(),
873  sizeof(sensebe_config_t)) != 0)
874  {
875  sensebe_store_config_write (&sensebe_config);
876  }
877 
878  //Check if light sense is required
879 
880  log_printf(" Trig Config : %d\n ", sensebe_config.trig_conf);
881 
882  if((MOD_FUNC_SEL == RX_EN) && (sensebe_config.trig_conf != MOTION_ONLY))
883  {
884  timer_module_start ();
885  }
886  else
887  {
888  timer_module_stop ();
889  }
890 
891  if((MOD_FUNC_SEL == RX_EN) && (sensebe_config.trig_conf != TIMER_ONLY))
892  {
893  motion_module_start ();
894  }
895  else
896  {
897  motion_module_stop ();
898  }
899 
900  if ((MOD_FUNC_SEL == TX_EN) && (sensebe_config.ir_tx_conf.is_enable == 1))
901  {
902  ir_tx_module_start ();
903  }
904  else
905  {
906  ir_tx_module_stop ();
907  }
908 
909  if(MOD_FUNC_SEL == TX_EN)
910  {
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];
913  }
914 
915  ms_timer_start (SENSEBE_OPERATION_MS_TIMER, MS_REPEATED_CALL,
916  MS_TIMER_TICKS_MS(arr_module_tick_duration[sensebe_config.ir_tx_conf.ir_tx_speed])
917  , module_tick_handler);
918 
919  light_sense_add_ticks (LIGHT_SENSE_INTERVAL_TICKS);
920 }
921 
923 {
924  log_printf("%s\n", __func__);
925  cam_trigger_stop ();
926  motion_module_stop ();
927  timer_module_stop ();
928  ir_tx_module_stop ();
929  radio_trigger_shut ();
930  ms_timer_stop (SENSEBE_OPERATION_MS_TIMER);
931 }
932 
933 void sensebe_tx_rx_add_ticks (uint32_t interval)
934 {
935  add_ticks_feedback (interval);
936 
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)
940  {
941  light_sense_add_ticks (interval);
942  }
943 
944  if((MOD_FUNC_SEL == RX_EN) && (sensebe_config.trig_conf != TIMER_ONLY))
945  {
946  motion_module_add_ticks ();
947  }
948  if((MOD_FUNC_SEL == RX_EN) && (sensebe_config.trig_conf != MOTION_ONLY))
949  {
950  timer_module_add_ticks ();
951  }
952  if((MOD_FUNC_SEL == TX_EN) && (sensebe_config.ir_tx_conf.is_enable == 1))
953  {
954  ir_tx_module_add_ticks ();
955  }
956 }
957 
958 void sensebe_tx_rx_update_config (sensebe_config_t * update_sensebe_config)
959 {
960  memcpy (&sensebe_config, update_sensebe_config, sizeof(sensebe_config_t));
961 }
962 
963 sensebe_config_t * sensebe_tx_rx_last_config ()
964 {
965  return &sensebe_config;
966 }
967 
969 {
970  tx_range = (tx_range + 1)%MAX_IR_RANGES;
971  led_ui_single_start (arr_range_indicator[tx_range], LED_UI_LOW_PRIORITY, true);
972 
973  arr_ir_range_select[tx_range]();
974  sensebe_config.ir_tx_conf.ir_tx_pwr = tx_range;
975 }
void(* cam_trigger_done_handler)(uint32_t done_state)
Definition: cam_trigger.h:133
void tssp_detect_window_stop(void)
Function to stop IR missed window detection.
Definition: tssp_detect.c:172
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...
Definition: simple_adc.c:25
void tssp_detect_pulse_detect()
Function to start module is pulse detecting mode.
Definition: tssp_detect.c:196
LED_UI_LOW_PRIORITY.
Definition: led_ui.h:41
#define TSSP_DETECT_TICKS_MS(ms)
Definition: tssp_detect.h:70
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.
Definition: led_ui.c:247
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.
Definition: cam_trigger.h:122
void led_ui_single_start(led_sequences seq, led_ui_priority_t priority, bool reset)
Start a sequence to play once.
Definition: led_ui.c:236
void cam_trigger(uint32_t setup_number)
Function to trigger the camera with given setup number.
Definition: cam_trigger.c:524
bool cam_trigger_is_on(void)
Function to check status of cam_trigger module.
Definition: cam_trigger.c:563
void sensebe_tx_rx_start(void)
Function to enable detection for SenseBe Rx.
Repeated call of the timer.
Definition: ms_timer.h:83
void tssp_detect_init(tssp_detect_config_t *tssp_detect_config)
Function to initialize IR detect sub-module.
Definition: tssp_detect.c:86
#define MS_TIMER_TICKS_MS(ms)
Definition: ms_timer.h:64
void tssp_detect_window_sync(uint32_t sync_ms)
Function to Synchronize TSSP detector to IR transmitter which is being used.
Definition: tssp_detect.c:214
sensebe_config_t * sensebe_store_config_get_last_config()
LED_UI_MID_PRIORITY.
Definition: led_ui.h:42
Trigger only on timer.
Definition: sensebe_ble.h:51
Structure to store information required to use this module.
Definition: tssp_detect.h:76
void ms_timer_start(ms_timer_num id, ms_timer_mode mode, uint64_t ticks, void(*handler)(void))
Definition: ms_timer.c:134
void cam_trigger_stop(void)
Function to stop camera trigger.
Definition: cam_trigger.c:557
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.
Definition: cam_trigger.c:483
uint32_t window_duration_ticks
Definition: tssp_detect.h:88
HAL_GPIO_PULL_DISABLED.
Definition: hal_gpio.h:37
void tssp_detect_pulse_stop()
Function to stop IR pulse detection.
Definition: tssp_detect.c:156
Gain factor 1/6.
Definition: simple_adc.h:70
void cam_trigger_init(cam_trigger_setup_t *cam_trigger_setup)
Function to initiate output pins.
Definition: cam_trigger.c:469
Structure to store information related to camera trigger.
Definition: cam_trigger.h:86
void tssp_detect_window_detect()
Function to start IR pulse detection.
Definition: tssp_detect.c:131
void sensebe_tx_rx_init(sensebe_tx_rx_config_t *sensebe_rx_detect_config)
Function to initialize the Rx detect module.
HAL_GPIO_PULL_DOWN.
Definition: hal_gpio.h:38
void ms_timer_stop(ms_timer_num id)
Definition: ms_timer.c:173
void led_ui_stop_seq(led_ui_seq_t type, led_sequences seq)
Stops a particular sequence of a particular type.
Definition: led_ui.c:283