forked from daschr/pico-ssd1306
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssd1306.h
294 lines (239 loc) · 7.95 KB
/
ssd1306.h
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
MIT License
Copyright (c) 2021 David Schramm
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/**
* @file ssd1306.h
*
* simple driver for ssd1306 displays
*/
#ifndef _inc_ssd1306
#define _inc_ssd1306
#include <pico/stdlib.h>
#include <hardware/i2c.h>
/**
* @brief defines commands used in ssd1306
*/
typedef enum {
SET_CONTRAST = 0x81,
SET_ENTIRE_ON = 0xA4,
SET_NORM_INV = 0xA6,
SET_DISP = 0xAE,
SET_MEM_ADDR = 0x20,
SET_COL_ADDR = 0x21,
SET_PAGE_ADDR = 0x22,
SET_DISP_START_LINE = 0x40,
SET_SEG_REMAP = 0xA0,
SET_MUX_RATIO = 0xA8,
SET_COM_OUT_DIR = 0xC0,
SET_DISP_OFFSET = 0xD3,
SET_COM_PIN_CFG = 0xDA,
SET_DISP_CLK_DIV = 0xD5,
SET_PRECHARGE = 0xD9,
SET_VCOM_DESEL = 0xDB,
SET_CHARGE_PUMP = 0x8D
} ssd1306_command_t;
/**
* @brief defines rotations/orientations
*/
typedef enum {
ROT_0 = 0,
ROT_90 = 1,
ROT_180 = 2,
ROT_270 = 3,
} rotation_t;
/**
* @brief holds the configuration
*/
typedef struct {
uint8_t width; /**< width of display */
uint8_t height; /**< height of display */
uint8_t pages; /**< stores pages of display (calculated on initialization*/
uint8_t address; /**< i2c address of display*/
i2c_inst_t *i2c_i; /**< i2c connection instance */
bool external_vcc; /**< whether display uses external vcc */
uint8_t *buffer; /**< display buffer */
size_t bufsize; /**< buffer size */
rotation_t rotation; /**< rotation of display */
} ssd1306_t;
/**
* @brief initialize display
*
* @param[in] p : pointer to instance of ssd1306_t
* @param[in] width : width of display
* @param[in] height : heigth of display
* @param[in] address : i2c address of display
* @param[in] i2c_instance : instance of i2c connection
*
* @return bool.
* @retval true for Success
* @retval false if initialization failed
*/
bool ssd1306_init(ssd1306_t *p, uint16_t width, uint16_t height, uint8_t address, i2c_inst_t *i2c_instance);
/**
* @brief deinitialize display
*
* @param[in] p : instance of display
*
*/
void ssd1306_deinit(ssd1306_t *p);
/**
* @brief turn off display
*
* @param[in] p : instance of display
*
*/
void ssd1306_poweroff(ssd1306_t *p);
/**
@brief turn on display
@param[in] p : instance of display
*/
void ssd1306_poweron(ssd1306_t *p);
/**
@brief set contrast of display
@param[in] p : instance of display
@param[in] val : contrast
*/
void ssd1306_contrast(ssd1306_t *p, uint8_t val);
/**
@brief set invert display
@param[in] p : instance of display
@param[in] inv : inv==0: disable inverting, inv!=0: invert
*/
void ssd1306_invert(ssd1306_t *p, uint8_t inv);
/**
@brief display buffer, should be called on change
@param[in] p : instance of display
*/
void ssd1306_show(ssd1306_t *p);
/**
@brief clear display buffer
@param[in] p : instance of display
*/
void ssd1306_clear(ssd1306_t *p);
/**
@brief sets the rotation for
@param[in] p : instance of display
@param[in] rotation : rotation angle
*/
void ssd1306_set_rotation(ssd1306_t *p, rotation_t rotation);
/**
@brief clear pixel on buffer
@param[in] p : instance of display
@param[in] x : x position
@param[in] y : y position
*/
void ssd1306_clear_pixel(ssd1306_t *p, uint32_t x, uint32_t y);
/**
@brief draw pixel on buffer
@param[in] p : instance of display
@param[in] x : x position
@param[in] y : y position
*/
void ssd1306_draw_pixel(ssd1306_t *p, uint32_t x, uint32_t y);
/**
@brief draw line on buffer
@param[in] p : instance of display
@param[in] x1 : x position of starting point
@param[in] y1 : y position of starting point
@param[in] x2 : x position of end point
@param[in] y2 : y position of end point
*/
void ssd1306_draw_line(ssd1306_t *p, int32_t x1, int32_t y1, int32_t x2, int32_t y2);
/**
@brief clear square at given position with given size
@param[in] p : instance of display
@param[in] x : x position of starting point
@param[in] y : y position of starting point
@param[in] width : width of square
@param[in] height : height of square
*/
void ssd1306_clear_square(ssd1306_t *p, uint32_t x, uint32_t y, uint32_t width, uint32_t height);
/**
@brief draw filled square at given position with given size
@param[in] p : instance of display
@param[in] x : x position of starting point
@param[in] y : y position of starting point
@param[in] width : width of square
@param[in] height : height of square
*/
void ssd1306_draw_square(ssd1306_t *p, uint32_t x, uint32_t y, uint32_t width, uint32_t height);
/**
@brief draw empty square at given position with given size
@param[in] p : instance of display
@param[in] x : x position of starting point
@param[in] y : y position of starting point
@param[in] width : width of square
@param[in] height : height of square
*/
void ssd1306_draw_empty_square(ssd1306_t *p, uint32_t x, uint32_t y, uint32_t width, uint32_t height);
/**
@brief draw monochrome bitmap with offset
@param[in] p : instance of display
@param[in] data : image data (whole file)
@param[in] size : size of image data in bytes
@param[in] x_offset : offset of horizontal coordinate
@param[in] y_offset : offset of vertical coordinate
*/
void ssd1306_bmp_show_image_with_offset(ssd1306_t *p, const uint8_t *data, const long size, uint32_t x_offset, uint32_t y_offset);
/**
@brief draw monochrome bitmap
@param[in] p : instance of display
@param[in] data : image data (whole file)
@param[in] size : size of image data in bytes
*/
void ssd1306_bmp_show_image(ssd1306_t *p, const uint8_t *data, const long size);
/**
@brief draw char with given font
@param[in] p : instance of display
@param[in] x : x starting position of char
@param[in] y : y starting position of char
@param[in] scale : scale font to n times of original size (default should be 1)
@param[in] font : pointer to font
@param[in] c : character to draw
*/
void ssd1306_draw_char_with_font(ssd1306_t *p, uint32_t x, uint32_t y, uint32_t scale, const uint8_t *font, char c);
/**
@brief draw char with builtin font
@param[in] p : instance of display
@param[in] x : x starting position of char
@param[in] y : y starting position of char
@param[in] scale : scale font to n times of original size (default should be 1)
@param[in] c : character to draw
*/
void ssd1306_draw_char(ssd1306_t *p, uint32_t x, uint32_t y, uint32_t scale, char c);
/**
@brief draw string with given font
@param[in] p : instance of display
@param[in] x : x starting position of text
@param[in] y : y starting position of text
@param[in] scale : scale font to n times of original size (default should be 1)
@param[in] font : pointer to font
@param[in] s : text to draw
*/
void ssd1306_draw_string_with_font(ssd1306_t *p, uint32_t x, uint32_t y, uint32_t scale, const uint8_t *font, const char *s );
/**
@brief draw string with builtin font
@param[in] p : instance of display
@param[in] x : x starting position of text
@param[in] y : y starting position of text
@param[in] scale : scale font to n times of original size (default should be 1)
@param[in] s : text to draw
*/
void ssd1306_draw_string(ssd1306_t *p, uint32_t x, uint32_t y, uint32_t scale, const char *s);
#endif