-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_2.c
88 lines (79 loc) · 2.89 KB
/
main_2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// This is an alternative approach that is focused ontask management / without yet using RTOS.
// The code initialises the necessary peripherals and sets up global variables to track the current mode and state of the system.
// The handleModeChange function checks if the button is clicked and switches the mode accordingly. The handleTiltSwitchMode function
// uses the accelerometer to determine the current orientation of the board and updates the LED state accordingly.
// The handlePauseMode function implements the LED blinking pattern and plays a beep through the audio output in the pause mode.
// The loop function calls the appropriate mode handling function based on the current mode.
// This is just one possible way to implement the required functionality. There may be other approaches that can also be used to achieve
// the same result.
#include "stm32f4xx.h" // Header file for STM32F4xx devices
#include "utils.h" // Header file for utilities of the system
// Global variables to track current mode and state of the system
enum Mode { TILT_SWITCH, PAUSE };
enum State { HORIZONTAL, LED1, LED2, LED3, LED4 };
Mode mode = TILT_SWITCH;
State state = HORIZONTAL;
// Function to initialise the system
void setup() {
// Initialise LEDs, button, accelerometer, and audio
initLEDs();
initButton();
initAccelerometer();
initAudio();
}
// Function to handle mode changes
void handleModeChange() {
// If button is clicked, switch mode
if (isButtonClicked()) {
if (mode == TILT_SWITCH) {
mode = PAUSE;
} else {
mode = TILT_SWITCH;
}
}
}
// Function to handle tilt switch mode
void handleTiltSwitchMode() {
// Get current orientation of the board from accelerometer
Orientation orientation = getOrientation();
// Update LED state based on orientation
if (orientation == DOWN_LED1) {
state = LED1;
} else if (orientation == DOWN_LED2) {
state = LED2;
} else if (orientation == DOWN_LED3) {
state = LED3;
} else if (orientation == DOWN_LED4) {
state = LED4;
} else {
state = HORIZONTAL;
}
setLEDState(state);
}
// Function to handle pause mode
void handlePauseMode() {
// Blink LED in the specified pattern
static int counter = 0; // Counter variable to track current step in the pattern
if (counter == 0) {
setLEDState(LED1 | LED2); // Turn on red and green LED
playBeep(); // Play beep through audio output
} else if (counter == 10) {
setLEDState(0); // Turn off all LED
} else if (counter == 15) {
setLEDState(LED3 | LED4); // Turn on orange and blue LED
} else if (counter == 20) {
setLEDState(0); // Turn off all LED
counter = -1; // Reset counter
}
counter++;
}
//
// Main loop
void loop() {
handleModeChange(); // Handle mode change if button is clicked
if (mode == TILT_SWITCH) {
handleTiltSwitchMode(); // Handle tilt switch mode
} else {
handlePauseMode(); // Handle pause mode
}
}