-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcd.h
More file actions
172 lines (154 loc) · 3.53 KB
/
lcd.h
File metadata and controls
172 lines (154 loc) · 3.53 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* @file lcd.h
* @author Chris Jerrett
* @date 9/9/2017
* @brief LCD wrapper functions and macros
**/
#ifndef _LCD_H_
#define _LCD_H_
#include <API.h>
#include <string.h>
#include <stdarg.h>
/**
* @brief The top row on the lcd screen
* @author Chris Jerrett
* @date 9/9/2017
**/
#define TOP_ROW 1
/**
* @brief The bottom row on the lcd screen
* @author Chris Jerrett
* @date 9/9/2017
**/
#define BOTTOM_ROW 2
/**
* @brief Repressents the state of a button
*
* A button can be pressed of RELEASED. Release is false which is also 0.
* PRESSED is true or 1.
*
* @author Chris Jerrett
* @date 9/9/2017
**/
typedef enum {
/** A released button **/
RELEASED = false,
/** A pressed button **/
PRESSED = true,
} button_state;
/**
* @brief repreents the state of the lcd buttons
* @author Chris Jerrett
* @date 9/9/2017
**/
typedef struct {
button_state left;
button_state middle;
button_state right;
} lcd_buttons;
/** The port of the initialized lcd **/
static FILE *lcd_port = NULL;
void lcd_set_backlight(bool state);
/**
* @brief Asserts the lcd is initialized
* Works by chekcing is the File *lcd_port is the default NULL
* value and thus not set.
* @author Chris Jerrett
* @date 9/9/2017
**/
static void lcd_assert() {
if(lcd_port != NULL) {
printf("LCD NULL!");
exit(1);
}
}
/**
* @brief Returns the pressed buttons
* @return a struct containing the states of all three buttons.
* @author Chris Jerrett
* @date 9/9/2017
* @see lcd_buttons
**/
lcd_buttons lcd_get_pressed_buttons(){
lcd_assert();
unsigned int btn_binary = lcdReadButtons(lcd_port);
bool left = btn_binary & 0x1;
bool middle = btn_binary & 0x2;
bool right = btn_binary & 0x4;
lcd_buttons btns;
btns.left = left ? PRESSED : RELEASED;
btns.middle = middle ? PRESSED : RELEASED;
btns.right = right ? PRESSED : RELEASED;
return btns;
}
/**
* @brief Clears the lcd
* @author Chris Jerrett
* @date 9/9/2017
**/
void lcd_clear() {
lcd_assert();
lcdClear(lcd_port);
}
/**
* @brief Initializes the lcd screen.
* Also will initialize the lcd_port var. Must be called before any lcd function can be called.
* @param lcd the urart port of the lcd scrren
* @see uart1
* @see uart2
* @author Chris Jerrett
* @date 9/9/2017
**/
void init_lcd(FILE *lcd) {
lcdInit(lcd);
lcd_port = lcd;
}
/**
* @brief prints a string to a line on the lcd
* @param line the line to print on
* @param str string to print
* @author Chris Jerrett
* @date 9/9/2017
**/
void lcd_print(unsigned int line, const char *str) {
lcd_assert();
lcdSetText(lcd_port, line, str);
}
/**
* @brief prints a formated string to a line on the lcd. Smilar to printf
* @param line the line to print on
* @param format_str format string string to print
* @author Chris Jerrett
* @date 9/9/2017
**/
void lcd_printf(unsigned int line, const char *format_str, ...) {
lcd_assert();
lcdPrint(lcd_port, line, format_str);
}
/**
* @brief sets the backlight of the lcd
* @param state a boolean representing the state of the backlight. true = on, false = off.
* @author Chris Jerrett
* @date 9/9/2017
**/
void lcd_set_backlight(bool state) {
lcd_assert();
lcdSetBacklight(lcd_port, state);
}
/**
* @brief Prompts the user to confirm a string.
* User must press middle button to confirm.
* Function is not thread safe and will stall a thread.
*
* @param confirm_text the text for the user to confirm.
* @author Chris Jerrett
* @date 9/9/2017
**/
void promt_confirmation(const char *confirm_text) {
lcd_assert();
lcd_print(1, confirm_text);
while(lcd_get_pressed_buttons().middle != PRESSED){
delay(200);
}
}
#endif