-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcd.h
More file actions
200 lines (158 loc) · 5.49 KB
/
lcd.h
File metadata and controls
200 lines (158 loc) · 5.49 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// See JHD1313 datasheet
// Author: Nate Aquino (naquino14@outlook.com)
#ifndef _RGB_LCD_H
#define _RGB_LED_H
#ifndef byte
#define byte uint8_t
#endif // !byte
#pragma region Includes
#ifndef _STDINT_H
#include <stdint.h>
#endif // !_STDINT_H
#ifndef _STDIO_H
#include <stdio.h>
#endif // !_STDIO_H
#ifndef _HARDWARE_I2C_H
#include "hardware/i2c.h"
#endif // !_HARDWARE_I2C_H
#ifndef _PICO_STDIO_H
#include "pico/stdlib.h"
#endif // !_PICO_STDIO_H
#ifndef _STDARG_H
#include <stdarg.h>
#endif // !_STDARG_H
#pragma endregion Includes
#pragma region LCD
/// The address of the LCD I2C device.
#define LCD_ADDR (0x7C >> 1)
/// The delay time in μs between each command sent to the LCD. (0.15 ms)
#define FAST_DELAY 150
/// The delay time in μs between each slow command sent to the LCD. (2 ms)
#define SLOW_DELAY 2000
#pragma region LCD commands
/// Clears the screen, sets the address counter to 0
/// and returns the cursor to the start.
#define LCD_SCREEN_CLEAR 0x1
/// Sets Digital Display RAM address to 0,
/// and returns the cursor to the beginning.
/// Doesnt clear display.
#define LCD_CURSOR_RETURN 0x2
/// Sets Digital Display RAM address to 0,
/// returns the cursor to the beginning, but leaves
/// the content on the LCD unchanged.
#define LCD_ENTRY_MODE_SET 0x4
/// Sets the display on/off,
/// sets the cursor on/off,
/// and sets blink on/off.
#define LCD_DISPLAY_SWITCH 0x8
/// Moves the cursor/whole display,
/// leaving the Digigal Display RAM unchanged.
#define LCD_SHIFT_CURSOR 0x10
/// Sets the display bit mode,
/// wether or not we use 1/2 display lines
/// and the font type to 5x10/5x7.
#define LCD_FUNCTION_SET 0x20
/// Set the Character Generator RAM address
/// and prepares it to recieve data.
#define LCD_SET_CGRAM_ADDR 0x40
/// Set the Digital Display RAM address
/// and prepares it to recieve data.
#define LCD_SET_DDRAM_ADDR 0x80
#pragma endregion LCD commands
#pragma region LCD flags
// Entry mode Set flags
/// Sets the entry moving direction of the cursor
/// to the right.
#define LCD_ENTRY_MODE_INC 0x2
/// Sets the entry moving direction of the cursor.
/// to the left.
#define LCD_ENTRY_MODE_DEC 0x0
// Sets the entry shift to decriment.
#define LCD_ENTRY_MODE_NSHFT 0x1
/// Sets the entry shift to incriment.
#define LCD_ENTRY_MODE_SHIFT 0X0
// Display Switch flags
/// Turn the LCD display on.
#define LCD_DISPLAY_SWITCH_DISP_ON 0x4
/// Turn the LCD display off.
#define LCD_DISPLAY_SWITCH_DISP_OFF 0x0
/// Turn the LCD cursor on.
#define LCD_DISPLAY_SWITCH_CUR_ON 0x2
/// Turn the LCD cursor off.
#define LCD_DISPLAY_SWITCH_CUR_OFF 0x0
/// Turn the LCD cursor blink on.
#define LCD_DISPLAY_SWITCH_BLINK_ON 0x1
/// Turn the LCD cursor blink off.
#define LCD_DISPLAY_SWITCH_BLINK_OFF 0x0
// Shift Cursor flags
/// Shifts the cursor when incrimenting or decrimenting.
#define LCD_SHIFT_CURSOR_DISP_SHIFT 0x8
/// Shifts the display when incrimenting or decrimenting.
#define LCD_SHIFT_CURSOR_CURS_SHIFT 0x0
/// Increments the cursor position.
#define LCD_SHIFT_CURSOR_RIGHT 0x4
/// Decrements the cursor position.
#define LCD_SHIFT_CURSOR_LEFT 0x0
// Function Set flags
/// Sets the LCD to 8 bit mode.
#define LCD_FUNCTION_SET_8BIT 0x10
/// Sets the LCD to 4 bit mode.
#define LCD_FUNCTION_SET_4BIT 0x0
/// Sets the LCD to use 2 display lines.
#define LCD_FUNCTION_SET_2LINE 0x8
/// Sets the LCD to use 1 display line.
#define LCD_FUNCTION_SET_1LINE 0x0
/// Sets the LCD to use 5x10 font.
#define LCD_FUNCTION_SET_5X10 0x4
/// Sets the LCD to use 5x7 font.
#define LCD_FUNCTION_SET_5X7 0x0
#pragma endregion LCD flags
#pragma region Structs
/// @brief A struct containing the default initialization commands for the LCD.
typedef struct lcd_init_def_t {
byte func_set;
byte display_switch;
byte entry_mode_set;
} lcd_init_def;
#pragma endregion Structs
#pragma region Methods
/// @brief Initializes the LCD and the backlight I2C devices.
/// @param i2cport The I2C port to use.
/// @param sdaport The SDA port to use.
/// @param sclport The SCL port to use.
void lcd_init(i2c_inst_t* i2cport, uint sdaport, uint sclport);
/// @brief sets the baudrate of the I2C port. (By default 400kHz)
/// @param baudrate Baudrate. (in Hz)
void setbaudrate(uint baudrate);
/// @brief Combines flags of a command into a single byte. TODO: mark as static when done testing.
/// @param cmd The command to combine flags with.
/// @param n_flags The number of flags to combine.
/// @param ... The flags to combine.
/// @return The combined flags.
byte mkcmd(byte cmd, int n_flags, ...);
/// @brief Makes an lcd initialization defaults struct.
/// @return The lcd initialization defaults struct.
lcd_init_def mkinitdef();
/// @brief Sets the initialization defaults for the LCD.
/// @param def The initialization defaults struct.
void setinitdef(lcd_init_def* def);
/// @brief Moves the cursor position.
/// @param line The line to move the cursor to.
/// @param pos The position to move the cursor to.
void set_cursor(int pos, int line);
/// @brief Sends a command to the LCD.
/// @param cmd The command to send.
/// @returns The number of bytes sent or -2 if the send failed.
int writecmd(byte cmd);
/// @brief Sends data to the LCD.
/// @param data The data to send.
/// @returns The number of bytes sent or -2 if the send failed.
int writedata(byte data);
/// @brief Sets the text of a line on the LCD.
/// @param text The text to set.
/// @param length The length of the text.
/// @param line The line to set the text on.
void set_text(const char* text, int line);
#pragma endregion Methods
#pragma endregion LCD
#endif // !RGB_LCD_H