Appiko
oper_manage.c
1 /*
2  * oper_manage.c : Module to keep track of operation conditions for a device
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 #include "oper_manage.h"
20 #include "simple_adc.h"
21 #include "common_util.h"
22 
23 static uint32_t light_sense_pin;
24 
25 static uint32_t arr_start_cond [OPER_MANAGE_MAX_SLOTS];
26 static uint32_t arr_end_cond [OPER_MANAGE_MAX_SLOTS];
27 
28 static oper_list_t arr_oper_cond [OPER_MANAGE_MAX_SLOTS];
29 
30 volatile uint8_t active_slots = OPER_MANAGE_INVALID_SLOTS;
31 
32 void oper_manage_set_light_sense_pin (uint32_t light_sense_adc)
33 {
34  light_sense_pin = light_sense_adc;
35 
36 }
37 
38 
40 {
41  if(new_slot->slot_no < OPER_MANAGE_MAX_SLOTS)
42  {
43  arr_start_cond[new_slot->slot_no] = new_slot->start_cond;
44  arr_end_cond[new_slot->slot_no] = new_slot->end_cond;
45  arr_oper_cond[new_slot->slot_no] = new_slot->oper_sel;
46  uint32_t current_time = time_tracker_get_current_time_s();
47  if(current_time >= new_slot->start_cond
48  && current_time <= new_slot->end_cond)
49  {
50  active_slots |= 1 << new_slot->slot_no;
51  }
52  }
53 }
54 
56 {
57  uint32_t current_time = time_tracker_get_current_time_s();
58  uint32_t light_val = simple_adc_get_value (SIMPLE_ADC_GAIN1_6, light_sense_pin);
59  for(uint32_t slot_no = 0; slot_no < OPER_MANAGE_MAX_SLOTS; slot_no++)
60  {
61  if(arr_start_cond[slot_no] == arr_end_cond[slot_no])
62  {
63  active_slots = CLR_BIT_VAR(active_slots, slot_no);
64  }
65  else if(arr_oper_cond[slot_no] == OPER_MANAGE_TIME_OF_DAY)
66  {
67  if(current_time >= arr_start_cond[slot_no]
68  && current_time <= arr_end_cond[slot_no])
69  {
70  active_slots = SET_BIT_VAR(active_slots, slot_no);
71  }
72  else
73  {
74  active_slots = CLR_BIT_VAR(active_slots, slot_no);
75  }
76  }
77  else if(arr_oper_cond[slot_no] == OPER_MANAGE_AMBI_LIGHT)
78  {
79  if(light_val >= arr_start_cond[slot_no]
80  && light_val <= arr_end_cond[slot_no])
81  {
82  active_slots = SET_BIT_VAR(active_slots, slot_no);
83  }
84  else
85  {
86  active_slots = CLR_BIT_VAR(active_slots, slot_no);
87  }
88  }
89  }
90  return active_slots;
91 }
92 
94 {
95  return active_slots;
96 }
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
oper_list_t
Definition: oper_manage.h:42
uint32_t time_tracker_get_current_time_s()
Function to get current time in ms.
Definition: time_tracker.c:120
uint8_t oper_manage_check_update()
Function to check if there are any changes in active slots.
Definition: oper_manage.c:55
#define SET_BIT_VAR(VAR, BIT_NO)
Macro to set particular bit in a given variable without affecting other bits.
Definition: common_util.h:121
#define OPER_MANAGE_INVALID_SLOTS
Definition: oper_manage.h:39
uint8_t oper_manage_get_active_slots()
Definition: oper_manage.c:93
void oper_manage_set_light_sense_pin(uint32_t light_sense_adc)
Function to store ADC pin number.
Definition: oper_manage.c:32
#define OPER_MANAGE_MAX_SLOTS
Definition: oper_manage.h:36
#define CLR_BIT_VAR(VAR, BIT_NO)
Macro to clear particular bit in a given variable without affecting other bits.
Definition: common_util.h:129
uint32_t start_cond
Definition: oper_manage.h:58
Gain factor 1/6.
Definition: simple_adc.h:70
void oper_manage_set_slot(oper_manage_slot_t *new_slot)
Function to initiate new operation condition slot.
Definition: oper_manage.c:39
oper_list_t oper_sel
Definition: oper_manage.h:54