Appiko
main.c
1 /*
2  * main.c : Application to study simple statemachine
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 
45 #include <stdbool.h>
46 #include <stdint.h>
47 #include "nrf.h"
48 #include "boards.h"
49 #include "hal_clocks.h"
50 #include "hal_nop_delay.h"
51 #include "hal_gpio.h"
52 #include "common_util.h"
53 #include "tinyprintf.h"
54 #include "uart_printf.h"
55 #include "ms_timer.h"
56 #include "nrf_util.h"
57 
58 #define SS_TIME_MS 5052
59 
61 typedef enum
62 {
63  OFF_STATE, RED_STATE, GREEN_STATE
64 } sm_states;
65 sm_states state = OFF_STATE;
66 
70 typedef void (*state_process)(void);
71 
76 void repeat_handler(void)
77 {
78  switch (state)
79  {
80  case RED_STATE:
81  state = OFF_STATE;
82  hal_gpio_pin_write(LED_RED, !LEDS_ACTIVE_STATE);
83  hal_gpio_pin_write(LED_GREEN, !LEDS_ACTIVE_STATE);
84  hal_gpio_pin_write(LED_BLUE, !LEDS_ACTIVE_STATE);
85  tfp_printf("RED to OFF\n");
86  break;
87  case GREEN_STATE:
88  state = RED_STATE;
89  hal_gpio_pin_write(LED_RED, LEDS_ACTIVE_STATE);
90  hal_gpio_pin_write(LED_GREEN, !LEDS_ACTIVE_STATE);
91  hal_gpio_pin_write(LED_BLUE, !LEDS_ACTIVE_STATE);
93  tfp_printf("GREEN to RED\n");
94  break;
95  case OFF_STATE:
96  default:
97  break;
98  }
99 }
100 
106 bool button_check(void)
107 {
108  static uint32_t prev_state = !BUTTONS_ACTIVE_STATE;
109  bool falling_edge;
110  uint32_t curr_state = hal_gpio_pin_read(BUTTON_1);
111  if ((curr_state == !BUTTONS_ACTIVE_STATE) && (prev_state == BUTTONS_ACTIVE_STATE))
112  {
113  falling_edge = true;
114  }
115  else
116  {
117  falling_edge = false;
118  }
119  prev_state = curr_state;
120  return falling_edge;
121 }
122 
124 void green_process(void)
125 {
126  bool btn_check = button_check();
127  if (btn_check)
128  {
130  }
131 }
132 
134 void off_process(void)
135 {
136  bool btn_check = button_check();
137  if (btn_check)
138  {
139  hal_gpio_pin_write(LED_RED, LEDS_ACTIVE_STATE);
140  hal_gpio_pin_write(LED_GREEN, !LEDS_ACTIVE_STATE);
141  hal_gpio_pin_write(LED_BLUE, !LEDS_ACTIVE_STATE);
142  state = RED_STATE;
144  tfp_printf("OFF to RED\n");
145  }
146 }
147 
149 void red_process(void)
150 {
151  bool btn_check = button_check();
152  if (btn_check)
153  {
154  hal_gpio_pin_write(LED_RED, !LEDS_ACTIVE_STATE);
155  hal_gpio_pin_write(LED_GREEN, LEDS_ACTIVE_STATE);
156  hal_gpio_pin_write(LED_BLUE, !LEDS_ACTIVE_STATE);
157  state = GREEN_STATE;
159  tfp_printf("RED to GREEN\n");
160  }
161 }
162 
164 static void rgb_led_init(void)
165 {
166  hal_gpio_cfg_output(LED_RED, !(LEDS_ACTIVE_STATE));
167  hal_gpio_cfg_output(LED_GREEN, !(LEDS_ACTIVE_STATE));
168  hal_gpio_cfg_output(LED_BLUE, !(LEDS_ACTIVE_STATE));
169 }
170 
172 static void rgb_led_cycle(void)
173 {
174  hal_gpio_pin_write(LED_RED, (LEDS_ACTIVE_STATE));
175  hal_gpio_pin_write(LED_GREEN, !(LEDS_ACTIVE_STATE));
176  hal_gpio_pin_write(LED_BLUE, !(LEDS_ACTIVE_STATE));
177  hal_nop_delay_ms(250);
178  hal_gpio_pin_write(LED_RED, !(LEDS_ACTIVE_STATE));
179  hal_gpio_pin_write(LED_GREEN, (LEDS_ACTIVE_STATE));
180  hal_gpio_pin_write(LED_BLUE, !(LEDS_ACTIVE_STATE));
181  hal_nop_delay_ms(250);
182  hal_gpio_pin_write(LED_RED, !(LEDS_ACTIVE_STATE));
183  hal_gpio_pin_write(LED_GREEN, !(LEDS_ACTIVE_STATE));
184  hal_gpio_pin_write(LED_BLUE, (LEDS_ACTIVE_STATE));
185  hal_nop_delay_ms(250);
186  hal_gpio_pin_write(LED_RED, !(LEDS_ACTIVE_STATE));
187  hal_gpio_pin_write(LED_GREEN, !(LEDS_ACTIVE_STATE));
188  hal_gpio_pin_write(LED_BLUE, !(LEDS_ACTIVE_STATE));
189 }
190 
194 int main(void)
195 {
196  rgb_led_init();
197  rgb_led_cycle();
198  /* Initial printf */
200  tfp_printf("Hello World %d!\n", 1);
201 
204 
205  ms_timer_init(APP_IRQ_PRIORITY_MID);
206 
207  hal_gpio_cfg_input(BUTTON_1, HAL_GPIO_PULL_UP);
208 
209  state_process process[3] =
211 
212  while (true)
213  {
214  process[state]();
215  //To take care of button debouncing
216  hal_nop_delay_ms(50);
217  }
218 }
219 
void repeat_handler(void)
5s timeout handler used to go from green to red state or red to off state.
Definition: main.c:76
sm_states
The three states of the state machine.
Definition: main.c:61
void red_process(void)
Red state process checks if there is a button press to go to green state.
Definition: main.c:149
#define LEDS_ACTIVE_STATE
Definition: bluey_1v1.h:44
void green_process(void)
Green state process checks if there is a button press to reset the timer.
Definition: main.c:124
void lfclk_init(lfclk_src_t lfclk_src)
Function to initialize the LF clock.
Definition: hal_clocks.c:23
UART_PRINTF_BAUD_1M.
Definition: uart_printf.h:67
#define MS_TIMER_TICKS_MS(ms)
Definition: ms_timer.h:64
void ms_timer_init(uint32_t irq_priority)
Definition: ms_timer.c:111
void uart_printf_init(uart_printf_baud_t baud_rate)
Function to initialize the parameters of UART based on the configurations in boards....
Definition: uart_printf.c:179
Millisecond Timer 0.
Definition: ms_timer.h:69
void ms_timer_start(ms_timer_num id, ms_timer_mode mode, uint64_t ticks, void(*handler)(void))
Definition: ms_timer.c:134
HAL_GPIO_PULL_UP.
Definition: hal_gpio.h:39
bool button_check(void)
Function that returns if the button was released from the previous time it was called.
Definition: main.c:106
void hfclk_xtal_init_blocking(void)
Function to start the crystal oscillator to be used for HF clock. This function blocks until the crys...
Definition: hal_clocks.c:71
int main(void)
Function for application main entry.
Definition: main.c:90
void off_process(void)
Off state process checks if there is a button press to go to red state.
Definition: main.c:134
void(* state_process)(void)
The type of function pointer that'll be used by the processing functions of the three states.
Definition: main.c:70
#define BUTTONS_ACTIVE_STATE
Definition: bluey_1v1.h:52
External crystal.
Definition: hal_clocks.h:38
One shot call of the timer.
Definition: ms_timer.h:82