forked from andygock/glcd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglcd.h
357 lines (279 loc) · 9.29 KB
/
glcd.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**
\file glcd.h
\brief GLCD Library main header file. This file must be included into project.
\author Andy Gock
*/
/*
Copyright (c) 2012, Andy Gock
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Andy Gock nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ANDY GOCK BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _GLCD_H
#define _GLCD_H
#define GLCD_DEVICE_STM32F4XX_CHIBIOS
#define GLCD_CONTROLLER_SHARP_LS013B7DH03
#define GLCD_USE_SPI_UART FALSE
#define CHIBIOS_SPI_PEREPHERIAL &SPID5
#define CHIBIOS_UART_SPI_PEREPHERIAL &UARTD3
#if defined(GLCD_DEVICE_AVR8)
#include <avr/pgmspace.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "devices/AVR8.h"
#if !defined(GLCD_USE_AVR_DELAY)
extern void delay_ms(uint32_t ms);
#else
/* Use AVR __delay_ms() function */
#include <util/delay.h>
#define delay_ms(t) _delay_ms(t)
#endif
#elif defined(GLCD_DEVICE_LPC111X)
#include <LPC11xx.h>
#include "devices/LPC111x.h"
extern void delay_ms(uint32_t ms);
#define PROGMEM
#elif defined(GLCD_DEVICE_LPC11UXX)
#include <LPC11Uxx.h>
#include "devices/LPC11Uxx.h"
extern void delay_ms(uint32_t ms);
#define PROGMEM
#elif defined(GLCD_DEVICE_STM32F0XX)
#include <stm32f0xx.h>
#include <stm32f0xx_gpio.h>
#include "devices/inc/STM32F0xx.h"
extern void delay_ms(uint32_t ms);
#define PROGMEM
#elif defined(GLCD_DEVICE_STM32F4XX_CHIBIOS)
//Allows you to use a UART peripheral as a SPI bus (STM32 can do this).
#define GLCD_USE_CORTEX_M3_INSTRUCTIONS TRUE
#if GLCD_USE_SPI_UART
#ifndef CHIBIOS_UART_SPI_PEREPHERIAL
#error "You must define CHIBIOS_UART_SPI_PEREPHERIAL somewhere, E.G. #define CHIBIOS_UART_SPI_PEREPHERIAL &UARTD3"
#endif
#else
#ifndef CHIBIOS_SPI_PEREPHERIAL
#error "You must define CHIBIOS_SPI_PEREPHERIAL somewhere, E.G. #define CHIBIOS_SPI_PEREPHERIAL &SPID1"
#endif
#endif
#define delay_ms(t) chThdSleepMilliseconds(t)
#include "devices/STM32F4_ChibiOS.h"
#define PROGMEM
#else
#error "Device not supported"
#endif
#if defined(GLCD_CONTROLLER_PCD8544)
#include "controllers/PCD8544.h"
#elif defined(GLCD_CONTROLLER_ST7565R)
#include "controllers/ST7565R.h"
#elif defined(GLCD_CONTROLLER_NT75451)
#include "controllers/NT75451.h"
#elif defined(GLCD_CONTROLLER_SHARP_LS013B7DH03)
/* Note: you must either externally toggle the VCOM pin at at least 1hz, or configure the chip
* and toggle the vcom bit in the SPI payload. This code assumes external toggling.
*/
#include "controllers/sharp_LS013B7DH03.h"
#define USE_SPI_MULTIBYTE
#define BLACK 0
#define WHITE 1
#if defined(GLCD_USE_PARALLEL)
#error "The SHARP_LS013B7DH03 must use SPI"
#endif
void glcd_write_bounded(const int ymin, const int ymax);
#else
#error "Controller not supported"
#endif
#define swap(a, b) { uint8_t t = a; a = b; b = t; }
#include <stdint.h>
#include "glcd_devices.h"
#include "glcd_controllers.h"
#include "glcd_graphics.h"
#include "glcd_graphs.h"
#include "glcd_text_tiny.h"
#include "glcd_text.h"
#include "unit_tests.h"
/**
* \name Colour Constants
* @{
*/
#ifndef BLACK
#define BLACK 1
#endif
#ifndef WHITE
#define WHITE 0
#endif
//#define FONT_COLOR_TEXT WHITE
//#define FONT_COLOR_BACKGROUND BLACK
//#define GRAPH_COLOR_LINE WHITE
//#define GRAPH_COLOR_BACKGROUND BLACK
/**@}*/
/**
* \name LCD Dimensions
* @{
*/
/* Set to custom value, or leave at 0 for automatic assignment. */
/**
* User specified GLCD width in pixels
* Set to 0 for automatic assignment based on controller
*/
#define GLCD_LCD_WIDTH 0
/**
* User specified GLCD height in pixels
* Set to 0 for automatic assignment based on controller
*/
#define GLCD_LCD_HEIGHT 0
/* Automatic assignment of width and height, if required. */
#if !GLCD_LCD_WIDTH && !GLCD_LCD_HEIGHT
#undef GLCD_LCD_WIDTH
#undef GLCD_LCD_HEIGHT
#if defined(GLCD_CONTROLLER_PCD8544)
#define GLCD_LCD_WIDTH 84
#define GLCD_LCD_HEIGHT 48
#elif defined(GLCD_CONTROLLER_ST7565R) || defined(GLCD_CONTROLLER_NT75451)
#define GLCD_LCD_WIDTH 128
#define GLCD_LCD_HEIGHT 64
#elif defined(GLCD_CONTROLLER_SHARP_LS013B7DH03)
#define GLCD_LCD_WIDTH 128
#define GLCD_LCD_HEIGHT 128
#else
#define GLCD_LCD_WIDTH 128
#define GLCD_LCD_HEIGHT 64
#endif
#endif
/*
* GLCD_NUMBER_OF_BANKS is typically GLCD_LCD_HEIGHT/8
* Don't adjust these below unless required.
*/
#define GLCD_NUMBER_OF_BANKS (GLCD_LCD_WIDTH / 8)
#define GLCD_NUMBER_OF_COLS GLCD_LCD_WIDTH
/**@}*/
#if !defined(GLCD_RESET_TIME)
/** Reset duration by glcd_reset(), in milliseconds */
#define GLCD_RESET_TIME 1
#endif
/**
* Bounding box for pixels that need to be updated
*/
typedef struct {
uint8_t x_min;
uint8_t y_min;
uint8_t x_max;
uint8_t y_max;
} glcd_BoundingBox_t;
typedef enum {
GLCD_SCREEN_ROTATION_0_DEGREES = 0,
GLCD_SCREEN_ROTATION_90_DEGREES = 90,
GLCD_SCREEN_ROTATION_180_DEGREES = 180,
GLCD_SCREEN_ROTATION_270_DEGREES = 270
} glcd_screen_rotation_mode_t;
/* Global variables used for GLCD library */
extern uint8_t glcd_buffer[GLCD_LCD_WIDTH * GLCD_LCD_HEIGHT / 8];
extern glcd_BoundingBox_t glcd_bbox;
extern uint8_t *glcd_buffer_selected;
extern glcd_BoundingBox_t *glcd_bbox_selected;
extern glcd_screen_rotation_mode_t glcd_screen_rotation;
/** \name Base Functions
* @{
*/
uint8_t glcd_get_foreground_color(void);
uint8_t glcd_get_background_color(void);
void glcd_set_foreground_color(uint8_t color);
/**
* Update bounding box.
*
* The bounding box defines a rectangle in which needs to be refreshed next time
* glcd_write() is called. glcd_write() only writes to those pixels inside the bounding box plus any
* surrounding pixels which are required according to the bank/column write method of the controller.
*
* Define a rectangle here, and it will be <em>added</em> to the existing bounding box.
*
* \param xmin Minimum x value of rectangle
* \param ymin Minimum y value of rectangle
* \param xmax Maximum x value of rectangle
* \param ymax Maximum y value of rectangle
* \see glcd_bbox
* \see glcd_bbox_selected
*/
void glcd_update_bbox(uint8_t xmin, uint8_t ymin, uint8_t xmax, uint8_t ymax);
/**
* Reset the bounding box.
* After resetting the bounding box, no pixels are marked as needing refreshing.
*/
void glcd_reset_bbox(void);
/**
* Same as glcd_reset_bbox()
*/
void glcd_bbox_reset(void);
/**
* Marks the entire display for re-writing.
*/
void glcd_bbox_refresh(void);
/**
* Clear the display. This will clear the buffer and physically write and commit it to the LCD
*/
void glcd_clear(void);
/**
* Clear the display buffer only. This does not physically write the changes to the LCD
*/
void glcd_clear_buffer(void);
/**
* Select screen buffer and bounding box structure.
* This should be selected at initialisation. There are future plans to support multiple screen buffers
* but this not yet available.
* \param buffer Pointer to screen buffer
* \param bbox Pointer to bounding box object.
* \see glcd_BoundingBox_t
*/
void glcd_select_screen(uint8_t *buffer, glcd_BoundingBox_t *bbox);
/**
* Scroll entire screne buffer by x and y pixels. (not implemented yet)
* \note Items scrolled off the extents of the display dimensions will be lost.
*
* \param x X distance to scroll
* \param y Y distance to scroll
*/
void glcd_scroll(int8_t x, int8_t y);
/**
* Scroll screen buffer up by 8 pixels.
* This is designed to be used in conjunciton with tiny text functions which are 8 bits high.
* \see Tiny Text
*/
void glcd_scroll_line(void);
void glcd_set_screen_rotation(const glcd_screen_rotation_mode_t mode);
glcd_screen_rotation_mode_t glcd_get_screen_rotation(void);
/**
* FIXME document this
*/
uint8_t glcd_reverse_significant_bits(uint32_t value);
/** @}*/
enum font_table_type { STANG, MIKRO };
typedef struct {
const char *font_table;
uint8_t width;
uint8_t height;
char start_char;
char end_char;
enum font_table_type table_type;
} glcd_FontConfig_t;
extern uint8_t *glcd_buffer_selected;
extern glcd_BoundingBox_t *glcd_bbox_selected;
extern glcd_FontConfig_t font_current;
#endif