forked from andygock/glcd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
glcd.c
228 lines (191 loc) · 6.02 KB
/
glcd.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
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
/**
\file glcd.c
\author Andy Gock
\brief Basic GLCD functions affecting bounding box manipulation,
clearing of screen and buffers, and basic scroll functions.
*/
/*
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.
*/
#include <string.h>
#include <stdio.h>
#include "glcd.h"
/** \addtogroup GlobalVars Global Variables
* @{
*/
/**
* Screen buffer
*
* Requires at least one bit for every pixel (e.g 504 bytes for 48x84 LCD)
*/
uint8_t glcd_buffer[GLCD_LCD_WIDTH * GLCD_LCD_HEIGHT / 8];
/**
* Keeps track of bounding box of area on LCD which need to be
* updated next reresh cycle
*/
glcd_BoundingBox_t glcd_bbox;
/**
* Pointer to screen buffer currently in use.
*/
uint8_t *glcd_buffer_selected = NULL;
/**
* Pointer to bounding box currently in use.
*/
glcd_BoundingBox_t *glcd_bbox_selected = NULL;
/**
* The current screen orientation
*/
glcd_screen_rotation_mode_t glcd_screen_rotation = GLCD_SCREEN_ROTATION_0_DEGREES;
uint8_t current_foreground_color = WHITE;
uint8_t current_background_color = BLACK;
uint8_t glcd_get_foreground_color(void) {
return(current_foreground_color);
}
uint8_t glcd_get_background_color(void) {
return(current_background_color);
}
void glcd_set_foreground_color(uint8_t color) {
glcd_graph_set_foreground_color(color);
glcd_text_set_foreground_color(color);
current_foreground_color = color;
current_background_color = (color == BLACK ? WHITE : BLACK);
}
/** @} */
void glcd_update_bbox(uint8_t xmin, uint8_t ymin, uint8_t xmax, uint8_t ymax)
{
/* Keep and check bounding box within limits of LCD screen dimensions */
if (xmin > (GLCD_LCD_WIDTH-1)) {
xmin = GLCD_LCD_WIDTH-1;
}
if (xmax > (GLCD_LCD_WIDTH-1)) {
xmax = GLCD_LCD_WIDTH-1;
}
if (ymin > (GLCD_LCD_HEIGHT-1)) {
ymin = GLCD_LCD_HEIGHT-1;
}
if (ymax > (GLCD_LCD_HEIGHT-1)) {
ymax = GLCD_LCD_HEIGHT-1;
}
/* Update the bounding box size */
if (xmin < glcd_bbox_selected->x_min) {
glcd_bbox_selected->x_min = xmin;
}
if (xmax > glcd_bbox_selected->x_max) {
glcd_bbox_selected->x_max = xmax;
}
if (ymin < glcd_bbox_selected->y_min) {
glcd_bbox_selected->y_min = ymin;
}
if (ymax > glcd_bbox_selected->y_max) {
glcd_bbox_selected->y_max = ymax;
}
}
void glcd_reset_bbox()
{
/* Used after physically writing to the LCD */
glcd_bbox_selected->x_min = GLCD_LCD_WIDTH - 1;
glcd_bbox_selected->x_max = 0;
glcd_bbox_selected->y_min = GLCD_LCD_HEIGHT -1;
glcd_bbox_selected->y_max = 0;
}
void glcd_bbox_reset() {
glcd_reset_bbox();
}
void glcd_bbox_refresh() {
/* Marks bounding box as entire screen, so on next glcd_write(), it writes the entire buffer to the LCD */
glcd_bbox_selected->x_min = 0;
glcd_bbox_selected->x_max = GLCD_LCD_WIDTH - 1;
glcd_bbox_selected->y_min = 0;
glcd_bbox_selected->y_max = GLCD_LCD_HEIGHT -1;
}
void glcd_clear(void) {
memset(glcd_buffer_selected, 0x00, GLCD_LCD_WIDTH * GLCD_LCD_HEIGHT / 8);
glcd_update_bbox(0,0,GLCD_LCD_WIDTH - 1,GLCD_LCD_HEIGHT - 1);
glcd_write();
}
void glcd_clear_buffer(void) {
memset(glcd_buffer_selected, 0x00, GLCD_LCD_WIDTH * GLCD_LCD_HEIGHT / 8);
glcd_update_bbox(0,0,GLCD_LCD_WIDTH - 1,GLCD_LCD_HEIGHT - 1);
}
void glcd_select_screen(uint8_t *buffer, glcd_BoundingBox_t *bbox)
{
glcd_buffer_selected = buffer;
glcd_bbox_selected = bbox;
}
void glcd_scroll(int8_t x, int8_t y)
{
/** \todo Skeleton */
uint8_t row;
for (row=0; row<(GLCD_LCD_HEIGHT / 8); row++) {
uint8_t x;
for (x=0; x<GLCD_LCD_WIDTH; x++) {
}
}
}
void glcd_scroll_line(void)
{
uint8_t y;
uint8_t number_of_rows = GLCD_LCD_HEIGHT / 8;
for (y=0; y<number_of_rows; y++) {
if (y < (number_of_rows - 1)) {
/* All lines except the last */
memcpy(glcd_buffer_selected + y*GLCD_LCD_WIDTH, glcd_buffer_selected + y*GLCD_LCD_WIDTH + GLCD_LCD_WIDTH, GLCD_LCD_WIDTH);
} else {
/* Last line, clear it */
memset(glcd_buffer_selected + (number_of_rows - 1)*GLCD_LCD_WIDTH, 0x00, GLCD_LCD_WIDTH);
}
}
glcd_update_bbox(0,0,GLCD_LCD_WIDTH - 1,GLCD_LCD_HEIGHT - 1);
}
#ifdef GLCD_USE_CORTEX_M3_INSTRUCTIONS
/*
* 8-bit MSB->LSB or LSB->MSB conversions of bytes.
*/
inline __attribute__((always_inline))
uint8_t glcd_reverse_significant_bits(uint32_t value)
{
//single cycle instruction to reverse the order of the bits
uint32_t d;
asm ("rbit %[Rd], %[Rm] \r\n"
"lsrs %[Rd], %[Rd], #24 \r\n"
: [Rd] "=r" (d)
: [Rm] "r" (value));
return(d & 0xFF);
}
#else
/*
* 8-bit MSB->LSB or LSB->MSB conversions of bytes.
*/
uint8_t glcd_reverse_significant_bits(uint32_t v) {
uint8_t ret = (v & 0x01);
for(int i = 1; i < 8; i++ ) {
ret <<= 1;
v >>= 1;
if( v & 0x01 ) {
ret |= 0x01;
}
}
return(ret);
}
#endif