-
Notifications
You must be signed in to change notification settings - Fork 0
/
aq_interface.c
132 lines (117 loc) · 3.41 KB
/
aq_interface.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "main.h"
#include "lcd5110.h"
#include "keyboard.h"
LCD5110_display interface_lcd;
typedef enum {
start_menu,
input_menu
} menu;
// Current menu
volatile menu current_menu = start_menu;
// When in input_menu - current number, that is being typed
volatile int current_number_input = 0;
// Whether light for display is turned on or off
volatile int light_state = 0;
void print_text(char* text) {
// Auxiliary function, prints text on display
LCD5110_set_cursor(0, 0, &interface_lcd);
// LCD5110_clear_scr(&interface_lcd);
LCD5110_print(text, BLACK, &interface_lcd);
}
void interface_display() {
// Displays all the required data on the display
char text[100];
if (current_menu == start_menu) {
char temperature[100];
sprintf(temperature, "%.2f/%d", get_current_temperature(), get_required_temperature());
sprintf(text,
"Hello \n"
"t:%11s\n"
"A - set temp \n"
"B - light %s\n"
"C - feed \n",
temperature,
light_state ? "off" : "on ");
}
else if (current_menu == input_menu) {
sprintf(text,
"%13d\n"
"A - accept \n"
"C - cancel \n"
"D - delete \n"
" \n",
current_number_input);
}
print_text(text);
}
// For keyboard
static void on_number(int);
static void on_choice(button);
keyboard board;
uint16_t output_pins[] = {GPIO_PIN_8, GPIO_PIN_9, GPIO_PIN_10, GPIO_PIN_11};
uint16_t input_pins[] = {GPIO_PIN_0, GPIO_PIN_1, GPIO_PIN_2, GPIO_PIN_3};
volatile uint16_t diodes[] = {GPIO_PIN_12, GPIO_PIN_13, GPIO_PIN_14, GPIO_PIN_15};
void interface_init() {
interface_lcd.hw_conf.spi_handle = &hspi2;
interface_lcd.hw_conf.spi_cs_pin = LCD1_CS_Pin;
interface_lcd.hw_conf.spi_cs_port = LCD1_CS_GPIO_Port;
interface_lcd.hw_conf.rst_pin = LCD1_RST_Pin;
interface_lcd.hw_conf.rst_port = LCD1_RST_GPIO_Port;
interface_lcd.hw_conf.dc_pin = LCD1_DC_Pin;
interface_lcd.hw_conf.dc_port = LCD1_DC_GPIO_Port;
interface_lcd.def_scr = lcd5110_def_scr;
LCD5110_init(&interface_lcd.hw_conf, LCD5110_NORMAL_MODE, 0x40, 2, 3);
keyboard new_board = {
GPIOD, GPIOD, input_pins, output_pins, *on_number, *on_choice
};
board = new_board;
keyboard_init(&board);
}
static void on_number(int number) {
// Called when a number is pressed
if (current_menu == input_menu){
// Add a new least-significant digit
current_number_input = current_number_input * 10 + number;
}
interface_display();
}
static void on_choice(button choice) {
// Called when a non-number button is pressed
if (current_menu == start_menu) {
if (choice == button_a) {
// Input temperature
current_menu = input_menu;
}
if (choice == button_c) {
// Feed the fish
dispenser_feed();
}
}
else if (current_menu == input_menu) {
if (choice == button_a) {
// A - Accept - get inputed required temperature menu, return to main menu
set_required_temperature(current_number_input);
current_menu = start_menu;
current_number_input = 0;
}
else if (choice == button_c) {
// C - Cancel - return to main menu
current_menu = start_menu;
current_number_input = 0;
}
else if (choice == button_d) {
// Delete the least significant digit
current_number_input /= 10;
}
}
if (choice == button_b) {
// B - Bright - toggle light
light_state = !light_state;
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_15, light_state);
}
interface_display();
}
void interface_on_input() {
// pass the interrupt to keyboard
keyboard_on_input(&board);
}