19 #include "sensepi_cam_trigger.h" 21 #include "sensepi_ble.h" 23 #include "pir_sense.h" 24 #include "led_sense.h" 25 #include "mcp4012_x.h" 28 #include "out_pattern_gen.h" 31 #include "hal_pin_analog_input.h" 32 #include "hal_nop_delay.h" 36 #include "nrf_assert.h" 37 #include "common_util.h" 44 #include "sensepi_store_config.h" 49 #define LED_WAIT_TIME_MS 301 51 #define PIR_SENSE_INTERVAL_MS 40 54 #define PIR_THRESHOLD_MULTIPLY_FACTOR 8 57 #define LIGHT_THRESHOLD_MULTIPLY_FACTOR 32 59 #define LIGHT_SENSE_INTERVAL_MS (5*60*1000) 61 #define LIGHT_SENSE_INTERVAL_TICKS (MS_TIMER_TICKS_MS(LIGHT_SENSE_INTERVAL_MS)) 64 #define SENSE_FEEDBACK_TIMEOUT_MS (9.8*60*1000) 67 #define SENSE_FEEDBACK_TIMEOUT_TICKS (MS_TIMER_TICKS_MS(SENSE_FEEDBACK_TIMEOUT_MS)) 69 #define SENSEPI_CAM_TRIGGER_MS_TIMER_USED MS_TIMER2 74 #define SIZE_OF_BYTE 8 82 #define MODE_MSK 0x000000FF 84 #define INPUT1_MSK 0x00FFFF00 86 #define INPUT2_MSK 0xFF000000 89 #define SINGLE_SHOT_TRANSITIONS 2 91 #define SINGLE_SHOT_DURATION MS_TIMER_TICKS_MS(250) 93 #define BULB_SHOT_TRANSITIONS 2 95 #define FOCUS_TRANSITIONS 2 97 #define TIMER_VIDEO_TRANSITIONS 3 99 #define VIDEO_START_PULSE MS_TIMER_TICKS_MS(250) 101 #define VIDEO_END_PULSE MS_TIMER_TICKS_MS(300) 103 #define NO_OF_VIDEO_EXTN_ALLOWED 3 105 #define VIDEO_PIR_ON 2000 141 #define VIDEO_ETXN_CONFIG TIMER_IDLE 150 static uint32_t video_extn_ticks;
152 static bool pir_on_flag =
false;
154 static uint32_t sense_count;
156 static bool sense_feedback =
false;
158 static bool is_light_sense_on =
false;
164 {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
165 {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
166 {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
167 {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1}
175 void pir_set_state(
bool state);
179 void pir_handler(int32_t adc_val);
184 void pir_handler_video(int32_t adc_val);
188 void out_gen_config_updater_pir(
void);
195 void timer_set_state(
bool state);
199 void timer_handler(
void);
203 void out_gen_config_updater_timer(
void);
210 void out_gen_done_handler(uint32_t out_gen_state);
216 void light_sense_init(
void);
221 void light_sense_set_state(
bool state);
229 bool light_sense_light_check(oper_time_t oper_time);
239 void module_manager_start_check(
void);
243 void module_manager_disable_all(
void);
254 void out_gen_config_single_shot(cam_trig_state_t data_process_mode);
261 void out_gen_config_multi_shot(cam_trig_state_t data_process_mode,
262 uint32_t burst_duration, uint32_t burst_num);
268 void out_gen_config_bulb_expo(cam_trig_state_t data_process_mode, uint32_t bulb_time);
273 void out_gen_config_focus_only(cam_trig_state_t data_process_mode);
278 void out_gen_config_none(cam_trig_state_t data_process_mode);
285 void out_gen_config_part_video_end();
290 void out_gen_config_part_video_extn(uint32_t extn_len);
296 void out_gen_config_part_video_start(uint32_t video_len);
302 void out_gen_config_full_video(cam_trig_state_t data_process_mode, uint32_t video_len);
313 log_printf(
"%s\n", str);
314 for(uint32_t row = 0; row< NUM_PIN_OUT; row++)
318 log_printf(
"%x ", next[row][arr_p]);
326 void pir_set_state(
bool state)
328 log_printf(
"%s %x\n",__func__, state);
339 void pir_handler(int32_t adc_val)
341 log_printf(
"%s", __func__);
342 log_printf(
" %d\n", adc_val);
343 pir_set_state(
false);
346 if(sense_feedback ==
true)
354 void pir_handler_video(int32_t adc_val)
356 log_printf(
"%s", __func__);
357 log_printf(
" %d\n", adc_val);
358 pir_set_state(
false);
359 static uint32_t no_of_extn_remain;
362 if(sense_feedback ==
true)
366 no_of_extn_remain = NO_OF_VIDEO_EXTN_ALLOWED;
369 else if(no_of_extn_remain > 0)
372 if(ticks_done < video_extn_ticks)
375 (video_extn_ticks - ticks_done);
387 void out_gen_config_updater_pir()
389 uint32_t config_mode = config.config_sensepi->pir_conf.mode;
390 uint32_t mode = ((config_mode & MODE_MSK)
391 >> (MODE_POS * SIZE_OF_BYTE));
392 uint32_t input1 = ((config_mode & INPUT1_MSK)
393 >> (INPUT1_POS * SIZE_OF_BYTE));
394 uint32_t input2 = ((config_mode & INPUT2_MSK)
395 >> (INPUT2_POS * SIZE_OF_BYTE));
396 uint32_t single_input = config_mode>> (INPUT1_POS * SIZE_OF_BYTE);
397 switch((operational_mode_t)mode)
399 case MODE_SINGLE_SHOT:
401 out_gen_config_single_shot(PIR_IDLE);
406 out_gen_config_multi_shot(PIR_IDLE, input1, input2);
412 out_gen_config_bulb_expo(PIR_IDLE, single_input);
419 out_gen_config_full_video (PIR_IDLE, input1);
423 out_gen_config_part_video_start(input1);
424 out_gen_config_part_video_extn(input2);
425 out_gen_config_part_video_end(input2);
431 out_gen_config_focus_only(PIR_IDLE);
436 out_gen_config_none(PIR_IDLE);
441 void timer_set_state(
bool state)
466 void out_gen_config_updater_timer(
void)
468 uint32_t config_mode = config.config_sensepi->timer_conf.mode;
469 uint32_t mode = ((config_mode & MODE_MSK)
470 >> (MODE_POS * SIZE_OF_BYTE));
471 uint32_t input1 = ((config_mode & INPUT1_MSK)
472 >> (INPUT1_POS * SIZE_OF_BYTE));
473 uint32_t input2 = ((config_mode & INPUT2_MSK)
474 >> (INPUT2_POS * SIZE_OF_BYTE));
475 uint32_t single_input = config_mode>> (INPUT1_POS * SIZE_OF_BYTE);
476 switch((operational_mode_t)mode)
478 case MODE_SINGLE_SHOT:
480 out_gen_config_single_shot(TIMER_IDLE);
485 out_gen_config_multi_shot(TIMER_IDLE, input1, input2);
491 out_gen_config_bulb_expo(TIMER_IDLE, single_input);
496 out_gen_config_full_video(TIMER_IDLE,input1);
501 out_gen_config_focus_only(TIMER_IDLE);
506 out_gen_config_none(PIR_IDLE);
511 void out_gen_done_handler (uint32_t out_gen_state)
513 log_printf(
"%s",__func__);
514 log_printf(
" %d\n", out_gen_state);
515 switch((cam_trig_state_t)out_gen_state)
519 pir_set_state(pir_on_flag);
526 pir_set_state(pir_on_flag);
533 pir_set_state(pir_on_flag);
538 log_printf(
"Something went seriously wrong..!!\n");
544 void light_sense_init(
void)
546 log_printf(
"%s\n",__func__);
552 void light_sense_set_state(
bool state)
558 hal_nop_delay_ms(LED_WAIT_TIME_MS);
566 bool light_sense_light_check(oper_time_t oper_time)
568 log_printf(
"%d\n", __func__);
570 uint8_t light_sense_config = oper_time.day_or_night;
571 uint32_t light_threshold =
572 (uint32_t)((oper_time.threshold) * LIGHT_THRESHOLD_MULTIPLY_FACTOR);
575 log_printf(
"Light Intensity : %d\n", light_intensity);
577 static bool light_check_flag = 0;
579 if(((light_sense_config == 1) && (light_intensity >= light_threshold))
581 ((light_sense_config == 0) && (light_intensity <= light_threshold)))
583 light_check_flag = 1;
587 light_check_flag = 0;
589 return light_check_flag;
592 void module_manager_start_check(
void)
594 log_printf(
"%s\n", __func__);
596 pir_set_state(
false);
598 switch(config.config_sensepi->trig_conf)
602 light_flag = light_sense_light_check(config.config_sensepi->pir_conf.oper_time);
603 pir_on_flag = light_flag;
604 pir_set_state(light_flag);
610 light_flag = light_sense_light_check(config.config_sensepi->timer_conf.oper_time);
611 timer_set_state(light_flag);
617 light_flag = light_sense_light_check(config.config_sensepi->pir_conf.oper_time);
618 pir_on_flag = light_flag;
620 light_flag = light_sense_light_check(config.config_sensepi->timer_conf.oper_time);
621 timer_set_state(light_flag);
622 pir_set_state(pir_on_flag);
630 void module_manager_disable_all(
void)
632 pir_set_state(
false);
633 timer_set_state(
false);
634 light_sense_set_state(
false);
637 void out_gen_config_single_shot(cam_trig_state_t data_process_mode)
639 log_printf(
"%s\n", __func__);
640 uint32_t number_of_transition = SINGLE_SHOT_TRANSITIONS;
643 if(data_process_mode == PIR_IDLE)
645 time_remain =
MS_TIMER_TICKS_MS(config.config_sensepi->pir_conf.intr_trig_timer * 100)
646 - (SINGLE_SHOT_DURATION);
647 if((int32_t)time_remain <= 0)
652 else if(data_process_mode == TIMER_IDLE)
660 .done_handler = out_gen_done_handler,
661 .out_gen_state = data_process_mode,
662 .transitions_durations = { SINGLE_SHOT_DURATION, time_remain },
663 .next_out = {{0, 1, 1},
667 debug_print_bool_array(local_out_gen_config.
next_out,
"single shot");
668 memcpy(&out_gen_config[data_process_mode],&local_out_gen_config,
672 void out_gen_config_multi_shot(cam_trig_state_t data_process_mode,
673 uint32_t burst_duration, uint32_t burst_num)
675 log_printf(
"%s\n", __func__);
677 uint32_t number_of_transition = SINGLE_SHOT_TRANSITIONS * burst_num;
679 uint32_t repeat_delay_array[SINGLE_SHOT_TRANSITIONS] = {SINGLE_SHOT_DURATION,
684 .done_handler = out_gen_done_handler,
685 .out_gen_state = data_process_mode,
688 for(uint32_t i = 0; i< burst_num; i++)
691 repeat_delay_array, SINGLE_SHOT_TRANSITIONS*
sizeof(uint32_t));
693 for(uint32_t j = 0; j < NUM_PIN_OUT; j++)
695 memcpy(*(local_out_gen_config.
next_out +j),*(multishot_generic + j),
696 burst_num* SINGLE_SHOT_TRANSITIONS*
sizeof(
bool));
698 local_out_gen_config.
next_out[j][burst_num* SINGLE_SHOT_TRANSITIONS] = 1;
702 if(data_process_mode == PIR_IDLE)
704 time_remain =
MS_TIMER_TICKS_MS(config.config_sensepi->pir_conf.intr_trig_timer * 100)
705 - SINGLE_SHOT_DURATION*burst_num -
706 (
MS_TIMER_TICKS_MS(burst_duration * 100) - SINGLE_SHOT_DURATION)*(burst_num - 1);
707 if((int32_t)time_remain <= 0)
712 else if(data_process_mode == TIMER_IDLE)
720 debug_print_bool_array(local_out_gen_config.
next_out,
"multi shot");
721 memcpy(&out_gen_config[data_process_mode], &local_out_gen_config,
725 void out_gen_config_bulb_expo(cam_trig_state_t data_process_mode, uint32_t bulb_time)
727 log_printf(
"%s\n", __func__);
729 uint32_t number_of_transition = BULB_SHOT_TRANSITIONS;
732 if(data_process_mode == PIR_IDLE)
734 time_remain =
MS_TIMER_TICKS_MS(config.config_sensepi->pir_conf.intr_trig_timer * 100)
736 if((int32_t)time_remain <= 0)
741 else if(data_process_mode == TIMER_IDLE)
749 .done_handler = out_gen_done_handler,
750 .out_gen_state = data_process_mode,
751 .transitions_durations =
752 {bulb_time_ticks, time_remain},
753 .next_out = { {0, 1, 1},
757 debug_print_bool_array(local_out_gen_config.
next_out,
"bulb mode");
758 memcpy(&out_gen_config[data_process_mode],&local_out_gen_config,
762 void out_gen_config_full_video(cam_trig_state_t data_process_mode, uint32_t video_len)
765 video_len = video_len*1000;
766 if(data_process_mode == TIMER_IDLE)
768 time_remain = config.config_sensepi->timer_conf.timer_interval*100 -
769 video_len - VIDEO_START_PULSE -VIDEO_END_PULSE;
771 video_len = (time_remain < 0) ?
772 (config.config_sensepi->timer_conf.timer_interval*100 - 1000) :
777 .next_out = {{0,1,0,1},
779 .transitions_durations = {VIDEO_START_PULSE,
782 .done_handler = out_gen_done_handler,
783 .out_gen_state = data_process_mode,
786 debug_print_bool_array(local_out_gen_config.
next_out,
"Timer video mode");
787 memcpy(&out_gen_config[data_process_mode],&local_out_gen_config,
793 void out_gen_config_part_video_start(uint32_t video_len)
795 config_pir.
handler = pir_handler_video;
796 video_len = video_len * 1000;
797 int video_len_check = video_len - VIDEO_PIR_ON;
798 video_len = (video_len_check <= 0) ? 0 : video_len_check;
804 .transitions_durations = {VIDEO_START_PULSE,
806 .done_handler = out_gen_done_handler,
807 .out_gen_state = VIDEO_IDLE,
809 debug_print_bool_array(local_out_gen_config.
next_out,
"PIR video mode");
810 memcpy(&out_gen_config[PIR_IDLE],&local_out_gen_config,
814 void out_gen_config_part_video_extn (uint32_t extn_len)
820 .next_out = {{1,1}, {1,1}},
821 .transitions_durations = {video_extn_ticks},
822 .done_handler = out_gen_done_handler,
823 .out_gen_state = VIDEO_IDLE,
825 debug_print_bool_array(local_out_gen_config.
next_out,
"PIR video extension");
826 memcpy(&out_gen_config[VIDEO_ETXN_CONFIG],&local_out_gen_config,
831 void out_gen_config_part_video_end()
836 .next_out = {{1,0,1},
839 .done_handler = out_gen_done_handler,
840 .out_gen_state = PIR_IDLE,
842 debug_print_bool_array(local_out_gen_config.
next_out,
"PIR video mode");
843 memcpy(&out_gen_config[VIDEO_IDLE],&local_out_gen_config,
847 void out_gen_config_focus_only(cam_trig_state_t data_process_mode)
849 log_printf(
"%s\n", __func__);
851 uint32_t number_of_transition = FOCUS_TRANSITIONS;
853 if(data_process_mode == PIR_IDLE)
855 time_remain =
MS_TIMER_TICKS_MS(config.config_sensepi->pir_conf.intr_trig_timer * 100)
856 - SINGLE_SHOT_DURATION ;
857 if((int32_t)time_remain <= 0)
862 else if(data_process_mode == TIMER_IDLE)
870 .done_handler = out_gen_done_handler,
871 .out_gen_state = data_process_mode,
872 .transitions_durations =
873 {SINGLE_SHOT_DURATION ,time_remain},
874 .next_out = { {0, 1, 1},
878 debug_print_bool_array(local_out_gen_config.
next_out,
"focus mode");
879 memcpy(&out_gen_config[data_process_mode],&local_out_gen_config,
883 void out_gen_config_none(cam_trig_state_t data_process_mode)
888 .done_handler = out_gen_done_handler,
889 .out_gen_state = data_process_mode,
890 .transitions_durations = {1},
895 memcpy(&out_gen_config[data_process_mode],&local_out_gen_config,
901 log_printf(
"%s\n", __func__);
903 memcpy(&config, config_sensepi_cam_trigger,
sizeof(config));
910 ((uint32_t)config.config_sensepi->pir_conf.threshold)*PIR_THRESHOLD_MULTIPLY_FACTOR,
911 APP_IRQ_PRIORITY_HIGH, pir_handler,
913 memcpy(&config_pir, &local_config_pir,
sizeof(
pir_sense_cfg));
915 (
bool *) out_gen_end_all_on);
920 log_printf(
"%s\n", __func__);
921 memcpy(config.config_sensepi, update_config,
sizeof(sensepi_config_t));
926 log_printf(
"%s\n", __func__);
929 sizeof(sensepi_config_t)) != 0)
934 sense_feedback =
true;
935 oper_time_t pir_oper_time = config.config_sensepi->pir_conf.oper_time;
936 oper_time_t timer_oper_time = config.config_sensepi->timer_conf.oper_time;
937 bool pir_light_flag =
true;
938 bool timer_light_flag =
true;
939 if(config.config_sensepi->trig_conf !=
PIR_ONLY)
941 if((timer_oper_time.day_or_night == 1 && timer_oper_time.threshold == 0b0000000)||
942 (timer_oper_time.day_or_night == 0 && timer_oper_time.threshold == 0b1111111))
944 timer_light_flag =
false;
948 timer_light_flag =
true;
953 timer_light_flag =
false;
955 if(config.config_sensepi->trig_conf != TIMER_ONLY)
957 if((pir_oper_time.day_or_night == 1 && pir_oper_time.threshold == 0b0000000)||
958 (pir_oper_time.day_or_night == 0 && pir_oper_time.threshold == 0b1111111))
960 pir_light_flag =
false;
964 pir_light_flag =
true;
969 pir_light_flag =
false;
971 is_light_sense_on = pir_light_flag || timer_light_flag;
973 config_pir.
threshold = ((uint32_t) config.config_sensepi->pir_conf.threshold)
974 *PIR_THRESHOLD_MULTIPLY_FACTOR;
975 config_pir.
handler = pir_handler;
979 out_gen_config_updater_timer();
980 out_gen_config_updater_pir();
982 light_sense_set_state(
true);
983 module_manager_start_check();
988 static uint32_t light_sense_count = 0;
990 log_printf(
"%s\n", __func__);
991 log_printf(
"SensePi Add ticks : %d\n", interval);
993 sense_count += interval;
994 if(sense_count > SENSE_FEEDBACK_TIMEOUT_TICKS)
996 sense_feedback =
false;
999 light_sense_count += interval;
1000 if(light_sense_count > LIGHT_SENSE_INTERVAL_TICKS && sense_feedback ==
false 1001 && is_light_sense_on ==
true)
1003 module_manager_start_check();
1004 light_sense_count = (light_sense_count - LIGHT_SENSE_INTERVAL_TICKS);
1010 log_printf(
"%s\n",__func__);
1011 module_manager_disable_all();
1018 log_printf(
"%s\n", __func__);
1019 return (config.config_sensepi);
uint32_t amp_ud_pin
Value at which LED is off.
void sensepi_cam_trigger_stop()
Function to stop PIR sensing.
void pir_sense_stop(void)
Disable the peripherals involved - SAADC, PPIs and RTC0.
void sensepi_cam_trigger_add_tick(uint32_t interval)
Function to decide what to decide at current tick.
uint32_t transitions_durations[OUT_GEN_MAX_TRANSITIONS]
1 dimensional array containing the durations in terms of LFCLK frequency ticks for the transitions.
uint32_t amp_spi_sck_pin
Pin number for CSbar pin for MCP4012.
uint32_t amp_cs_pin
Pin number for UDbar pin for MCP4012.
void led_ui_single_start(led_sequences seq, led_ui_priority_t priority, bool reset)
Start a sequence to play once.
uint32_t num_transitions
The number of transitions in this pattern.
bool next_out[OUT_GEN_MAX_NUM_OUT][OUT_GEN_MAX_TRANSITIONS]
A 2 dimensional boolean array containing the next digital output value for the various transitions ac...
uint32_t pir_sense_offset_input
Pin number for PIR signal output.
uint32_t led_sense_out_pin
Pin number for PIR offset output.
Repeated call of the timer.
void led_sense_init(uint32_t led_out_pin, uint32_t led_sense_analog_pin, uint32_t led_off_val)
Initialize the LED light sensing module.
uint32_t * signal_out_pin_array
MCP4012.
void sensepi_cam_trigger_init(sensepi_cam_trigger_init_config_t *config_sensepi_cam_trigger)
Function to initiate SensePi_PIR module.
Trigger only on motion detection.
sensepi_config_t * sensepi_store_config_get_last_config()
#define MS_TIMER_TICKS_MS(ms)
void out_gen_init(uint32_t num_out, uint32_t *out_pins, bool *out_init_value)
Initialize the output pattern generator module with the information of the pins on which the pattern ...
void out_gen_stop(bool *out_vals)
Stop the output pattern generation and sets the output pins as specified in the arguments.
bool ms_timer_get_on_status(ms_timer_num id)
#define ASSERT(expression)
Macro for runtime assertion of an expression. If the expression is false the assert_nrf_callback func...
bool out_gen_is_on(void)
To know if the output pattern generator module is on.
void pir_sense_start(pir_sense_cfg *init)
sensepi_config_t * sensepi_cam_trigger_get_sensepi_config()
Function to get the current configuration to send it to mobile app.
void(* handler)(int32_t adc_val)
The interrupt priority for calling the handler.
void led_ui_type_stop_all(led_ui_seq_t type)
Stop all sequence of a particular type.
#define PIR_SENSE_INTERVAL_MS
void sensepi_cam_trigger_update(sensepi_config_t *update_config)
Function to update the configuration at every instance when it is changed in the program.
Run a sequence repeatedly.
uint32_t led_sense_off_val
Pin number for light sensing.
uint32_t pir_sense_signal_input
Configuration received from mobile app.
void mcp4012_init(uint32_t CS_bar_pin_no, uint32_t UD_bar_pin_no, uint32_t SCK_pin_no)
This function is used to initialize MCP4012 in our program. This will set wiper value of MCP4012 to z...
void led_sense_cfg_input(bool is_input_on)
Configure the LED as either an light sensing input device or as an light emitting actuation device.
#define OUT_GEN_MAX_TRANSITIONS
uint32_t threshold
The analog input number of PIR offset.
void sensepi_store_config_write(sensepi_config_t *latest_config)
Function to write the sensepi_config_t at address location received from get_next_location().
void mcp4012_set_value(uint32_t value_sent_by_user)
This function is used to set the value of MCP4012 wiper value.
void ms_timer_start(ms_timer_num id, ms_timer_mode mode, uint64_t ticks, void(*handler)(void))
Configuration structure for output pattern generation.
uint32_t led_sense_analog_in_pin
Pin number for LED driving pin.
#define OUT_GEN_MAX_NUM_OUT
void out_gen_start(out_gen_config_t *out_gen_config)
Start the generation of the pattern with the information provided.
uint32_t out_gen_get_ticks(void)
Function to get ticks since last call of out_gen_start.
Strcture to configure SensePi_PIR module.
Stucture for passing the configuration for initializing the PIR Sense module.
uint32_t led_sense_get(void)
Get the light value by measuring the voltage a LED's anode.
void ms_timer_stop(ms_timer_num id)
void sensepi_cam_trigger_start()
Function to start PIR sensing.