Appiko
main.c
1 /*
2  * main.c : Application to test LSM6D sensor
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 
28 #include <stdbool.h>
29 #include <stdint.h>
30 #include "math.h"
31 
32 #include "nrf.h"
33 
34 #include "boards.h"
35 #include "hal_clocks.h"
36 #include "hal_nop_delay.h"
37 #include "hal_gpio.h"
38 #include "hal_twim.h"
39 #include "common_util.h"
40 #include "log.h"
41 #include "nrf_util.h"
42 #include "nrf_sdm.h"
43 #include "ble_adv.h"
44 #include "ble.h"
45 #include "ms_timer.h"
46 #include "LSM6DS3.h"
47 #include "lsm_testing_ble.h"
48 #include "hal_pin_analog_input.h"
49 #include "simple_adc.h"
50 
51 #define SMOKE_DETECT_PIN 2
52 
53 #define ANI_SMOKE_PIN PIN_TO_ANALOG_INPUT(SMOKE_DETECT_PIN)
54 
55 int16_t x_data, y_data, z_data;
56 
57 static uint32_t my_sqrt (uint32_t num)
58 {
59  float xn, xm, flag, temp;
60  xn = 0;
61  xm = 800;
62  flag = 0;
63  while (flag == 0)
64  {
65  xn = (xm + (num / xm)) / 2;
66  if (xn == xm)
67  {
68  flag = 1;
69  }
70  else
71  {
72  xm = xn;
73  }
74  }
75  temp = xn * 1000;
76  return (uint32_t) temp;
77 }
78 
79 bool compare_percent (uint32_t data, uint32_t ref, float per)
80 {
81  if ((ref - (ref * per / 100)) <= data && data <= (ref + (ref * per / 100)))
82  {
83  return 1;
84  }
85  else
86  {
87  return 0;
88  }
89 }
90 
91 uint32_t avg_uint32_x100 (uint32_t * p_arr, uint32_t no_of_entries)
92 {
93  uint32_t add = 0;
94  float avg = 0;
95  for(uint32_t cnt = 0; cnt < no_of_entries; cnt++)
96  {
97  add += p_arr[cnt];
98  }
99  avg = add/no_of_entries;
100  return (uint32_t)(avg * 100);
101 }
102 
103 static mod_ble_data_t ble_data;
104 
105 void lsm_100ms_handler ()
106 {
107 
108  LSM6DS3_read_accl_data (&x_data, &y_data, &z_data);
109  x_data = (LSM6DS3_accelData_in_g(x_data));
110  x_data = (x_data * ((x_data > 0) - (x_data < 0)));
111  y_data = (LSM6DS3_accelData_in_g(y_data) );
112  y_data = (y_data * ((y_data > 0) - (y_data < 0)));
113  z_data = (LSM6DS3_accelData_in_g(z_data) );
114  z_data = (z_data * ((z_data > 0) - (z_data < 0)));
115 }
116 
117 void ms_timer_handler ()
118 {
119  static uint32_t curr_res_acce = 0;
120  static uint32_t max_res_acce = 0;
121  static uint32_t res_acce_loc = 0;
122  static uint32_t arr_smoke_val[10];
123  static uint32_t curr_avg_smoke_val_x100 = 0;
124  lsm_100ms_handler ();
125  curr_res_acce = my_sqrt ((x_data * x_data) + (y_data * y_data) + (z_data * z_data))/1000;
126  arr_smoke_val[res_acce_loc] = simple_adc_get_value (SIMPLE_ADC_GAIN1_6, ANI_SMOKE_PIN);
127  if(curr_res_acce > max_res_acce)
128  {
129  max_res_acce = curr_res_acce;
130  }
131  if(res_acce_loc == 9)
132  {
133  ble_data.avg_acce = max_res_acce;
134  max_res_acce = 0;
135  curr_avg_smoke_val_x100 = avg_uint32_x100 (arr_smoke_val, 10);
136  ble_data.avg_smoke = curr_avg_smoke_val_x100 / 100;
137  }
138  lsm_ble_update_status_byte (&ble_data);
139  res_acce_loc = (res_acce_loc + 1) % 10;
140 
141 
142 
143 }
144 
146 static void rgb_led_init(void)
147 {
148  hal_gpio_cfg_output(LED_RED, !(LEDS_ACTIVE_STATE));
149  hal_gpio_cfg_output(LED_GREEN, !(LEDS_ACTIVE_STATE));
150  hal_gpio_cfg_output(LED_BLUE, !(LEDS_ACTIVE_STATE));
151 }
152 
154 static void rgb_led_cycle(void)
155 {
156  hal_gpio_pin_write(LED_RED, (LEDS_ACTIVE_STATE));
157  hal_gpio_pin_write(LED_GREEN, !(LEDS_ACTIVE_STATE));
158  hal_gpio_pin_write(LED_BLUE, !(LEDS_ACTIVE_STATE));
159  hal_nop_delay_ms(250);
160  hal_gpio_pin_write(LED_RED, !(LEDS_ACTIVE_STATE));
161  hal_gpio_pin_write(LED_GREEN, (LEDS_ACTIVE_STATE));
162  hal_gpio_pin_write(LED_BLUE, !(LEDS_ACTIVE_STATE));
163  hal_nop_delay_ms(250);
164  hal_gpio_pin_write(LED_RED, !(LEDS_ACTIVE_STATE));
165  hal_gpio_pin_write(LED_GREEN, !(LEDS_ACTIVE_STATE));
166  hal_gpio_pin_write(LED_BLUE, (LEDS_ACTIVE_STATE));
167  hal_nop_delay_ms(250);
168  hal_gpio_pin_write(LED_RED, !(LEDS_ACTIVE_STATE));
169  hal_gpio_pin_write(LED_GREEN, !(LEDS_ACTIVE_STATE));
170  hal_gpio_pin_write(LED_BLUE, !(LEDS_ACTIVE_STATE));
171 }
172 
173 void on_connect ()
174 {
175 
176 }
180 void slumber(void)
181 {
182  uint8_t is_sd_enabled;
183  sd_softdevice_is_enabled(&is_sd_enabled);
184  // Would in the SENSING mode
185  if(is_sd_enabled == 0)
186  {
187  __WFI();
188  }
189  else
190  {
191  sd_app_evt_wait();
192  }
193 }
194 
198 int main(void)
199 {
200  rgb_led_init();
201  rgb_led_cycle();
202  /* Initial printf */
203  log_init();
204  log_printf("Hello World from LSM6D..!!\n");
205 
206  ms_timer_init(APP_IRQ_PRIORITY_LOWEST);
208  LSM6DS3_init();
209  {
210  lsm_ble_stack_init ();
211  lsm_ble_gap_params_init ();
212  lsm_ble_adv_init ();
213  lsm_ble_adv_start (on_connect);
214  lsm_ble_service_init ();
215  }
216  ms_timer_start (MS_TIMER1, MS_REPEATED_CALL, MS_TIMER_TICKS_MS(100), ms_timer_handler);
217  while (true)
218  {
219  slumber ();
220  }
221 }
222 
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
#define LEDS_ACTIVE_STATE
Definition: bluey_1v1.h:44
void lfclk_init(lfclk_src_t lfclk_src)
Function to initialize the LF clock.
Definition: hal_clocks.c:23
Repeated call of the timer.
Definition: ms_timer.h:83
#define MS_TIMER_TICKS_MS(ms)
Definition: ms_timer.h:64
void ms_timer_init(uint32_t irq_priority)
Definition: ms_timer.c:111
Millisecond Timer 1.
Definition: ms_timer.h:70
void ms_timer_start(ms_timer_num id, ms_timer_mode mode, uint64_t ticks, void(*handler)(void))
Definition: ms_timer.c:134
void slumber(void)
Definition: main.c:180
int main(void)
Function for application main entry.
Definition: main.c:90
Gain factor 1/6.
Definition: simple_adc.h:70
External crystal.
Definition: hal_clocks.h:38