ATY_LIB V2_102_230218
ATY_LIB for general devices or ALGO
 
Loading...
Searching...
No Matches
ssd1306.c
Go to the documentation of this file.
1// Copyright 2021 IOsetting <iosetting(at)outlook.com>
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "ssd1306.h"
16
17/* Absolute value */
18#define ABS(x) ((x) > 0 ? (x) : -(x))
19
20/* SSD1306 data buffer */
22
23/* Private SSD1306 structure */
24typedef struct {
29} SSD1306_t;
30
31/* Private variable */
33
35{
36 I2C_Write(SSD1306_I2C_ADDR, 0x00, &command, 1);
37}
38
40{
41 I2C_Write(SSD1306_I2C_ADDR, 0x40, &dat, 1);
42}
43
44void SSD1306_Init(void)
45{
46 SYS_Delay(100);
47
48 /* Init LCD */
49 SSD1306_WriteCommand(0xAE); //display off
54 SSD1306_WriteCommand(0x00); //---set low column address
55 SSD1306_WriteCommand(0x10); //---set high column address
56
66 /* 0x20,Set Memory Addressing Mode, 2 bytes,
67 * 0x00,Horizontal Addressing Mode (slide horizontally and goto next page)
68 * 0x01,Vertical Addressing Mode (slide vertically and goto next column)
69 * 0x02,Page Addressing Mode (RESET) (slide horizontally and remain in the same page)
70 * 0x03,Invalid
71 */
84 SSD1306_WriteCommand(0x00); // From Page 0
85 SSD1306_WriteCommand(0x07); // To Page 7
86
91 SSD1306_WriteCommand(0xC8); //Set COM Output Scan Direction
110 SSD1306_WriteCommand(0x00); // offset in vertical
118 SSD1306_WriteCommand(0x12); // A[4]=0, A[5]=1
123 SSD1306_WriteCommand(0xF0); // divide ratio
136
143
147
148 /* Clear screen */
150
151 /* Update screen */
153
154 /* Set default values */
155 SSD1306.CurrentX = 0;
156 SSD1306.CurrentY = 0;
157
158 /* Initialized OK */
160}
161
163{
165}
166
168{
169 uint16_t i;
170
171 /* Toggle invert */
173
174 /* Do memory toggle */
175 for (i = 0; i < sizeof(SSD1306_Buffer_all); i++)
176 {
177 SSD1306_Buffer_all[i] = ~SSD1306_Buffer_all[i];
178 }
179}
180
182{
183 if (SSD1306.Inverted)
184 {
185 color = (uint8_t)!color;
186 }
187 /* Set memory */
188 memset(SSD1306_Buffer_all, (color == SSD1306_COLOR_BLACK) ? 0x00 : 0xFF, SSD1306_WIDTH * SSD1306_HEIGHT / 8);
189}
190
192{
193 if (x >= SSD1306_WIDTH || y >= SSD1306_HEIGHT)
194 {
195 /* Error */
196 return;
197 }
198
199 /* Check if pixels are inverted */
200 if (SSD1306.Inverted)
201 {
202 color = (uint8_t)!color;
203 }
204
205 /* Set color */
206 if (color == SSD1306_COLOR_WHITE)
207 {
208 SSD1306_Buffer_all[x + (y / 8) * SSD1306_WIDTH] |= 1 << (y % 8);
209 }
210 else
211 {
212 SSD1306_Buffer_all[x + (y / 8) * SSD1306_WIDTH] &= ~(1 << (y % 8));
213 }
214}
215
217{
218 /* Set write pointers */
221}
222
224{
225 int16_t dx, dy, sx, sy, err, e2, i, tmp;
226
227 /* Check for overflow */
228 if (x0 >= SSD1306_WIDTH)
229 {
230 x0 = SSD1306_WIDTH - 1;
231 }
232 if (x1 >= SSD1306_WIDTH)
233 {
234 x1 = SSD1306_WIDTH - 1;
235 }
236 if (y0 >= SSD1306_HEIGHT)
237 {
238 y0 = SSD1306_HEIGHT - 1;
239 }
240 if (y1 >= SSD1306_HEIGHT)
241 {
242 y1 = SSD1306_HEIGHT - 1;
243 }
244
245 dx = (x0 < x1) ? (x1 - x0) : (x0 - x1);
246 dy = (y0 < y1) ? (y1 - y0) : (y0 - y1);
247 sx = (x0 < x1) ? 1 : -1;
248 sy = (y0 < y1) ? 1 : -1;
249 err = ((dx > dy) ? dx : -dy) / 2;
250
251 if (dx == 0)
252 {
253 if (y1 < y0)
254 {
255 tmp = y1;
256 y1 = y0;
257 y0 = tmp;
258 }
259
260 if (x1 < x0)
261 {
262 tmp = x1;
263 x1 = x0;
264 x0 = tmp;
265 }
266
267 /* Vertical line */
268 for (i = y0; i <= y1; i++)
269 {
270 SSD1306_DrawPixel(x0, i, c);
271 }
272
273 /* Return from function */
274 return;
275 }
276
277 if (dy == 0)
278 {
279 if (y1 < y0)
280 {
281 tmp = y1;
282 y1 = y0;
283 y0 = tmp;
284 }
285
286 if (x1 < x0)
287 {
288 tmp = x1;
289 x1 = x0;
290 x0 = tmp;
291 }
292
293 /* Horizontal line */
294 for (i = x0; i <= x1; i++)
295 {
296 SSD1306_DrawPixel(i, y0, c);
297 }
298
299 /* Return from function */
300 return;
301 }
302
303 while (1)
304 {
305 SSD1306_DrawPixel(x0, y0, c);
306 if (x0 == x1 && y0 == y1)
307 {
308 break;
309 }
310 e2 = err;
311 if (e2 > -dx)
312 {
313 err -= dy;
314 x0 += sx;
315 }
316 if (e2 < dy)
317 {
318 err += dx;
319 y0 += sy;
320 }
321 }
322}
323
324void SSD1306_ON(void)
325{
329}
330void SSD1306_OFF(void)
331{
335}
__CODE int8_t dat[20]
uint8_t I2C_Write(uint8_t devAddr, uint8_t memAddr, uint8_t *dat, uint16_t size)
Definition: fw_i2c.c:18
void SYS_Delay(uint16_t t)
Definition: fw_sys.c:65
unsigned short uint16_t
Definition: fw_types.h:19
short int16_t
Definition: fw_types.h:24
unsigned char uint8_t
Definition: fw_types.h:18
uint8_t __XDATA i
volatile int16_t y
Definition: main.c:34
volatile int16_t x
Definition: main.c:34
void SSD1306_Fill(uint8_t color)
Fills entire LCD with desired color.
Definition: ssd1306.c:181
void SSD1306_WriteCommand(uint8_t command)
Writes single byte command to slave.
Definition: ssd1306.c:34
void SSD1306_ToggleInvert(void)
Toggles pixels invertion inside internal RAM.
Definition: ssd1306.c:167
void SSD1306_ON(void)
Definition: ssd1306.c:324
void SSD1306_GotoXY(uint16_t x, uint16_t y)
Sets cursor pointer to desired location for strings.
Definition: ssd1306.c:216
void SSD1306_DrawPixel(uint16_t x, uint16_t y, uint8_t color)
Draws pixel at desired location.
Definition: ssd1306.c:191
void SSD1306_OFF(void)
Definition: ssd1306.c:330
void SSD1306_Init(void)
Definition: ssd1306.c:44
void SSD1306_UpdateScreen(void)
Updates buffer from internal RAM to LCD.
Definition: ssd1306.c:162
void SSD1306_DrawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint8_t c)
Draws line on LCD.
Definition: ssd1306.c:223
void SSD1306_WriteData(uint8_t dat)
Writes single byte data to slave.
Definition: ssd1306.c:39
static SSD1306_t SSD1306
Definition: ssd1306.c:32
static __XDATA uint8_t SSD1306_Buffer_all[SSD1306_WIDTH *SSD1306_HEIGHT/8]
Definition: ssd1306.c:21
#define SSD1306_I2C_ADDR
Definition: ssd1306.h:26
#define SSD1306_HEIGHT
Definition: ssd1306.h:37
#define SSD1306_COLOR_BLACK
Definition: ssd1306.h:45
#define SSD1306_WIDTH
Definition: ssd1306.h:33
#define SSD1306_COLOR_WHITE
Definition: ssd1306.h:47
uint8_t Inverted
Definition: ssd1306.c:27
uint16_t CurrentY
Definition: ssd1306.c:26
uint16_t CurrentX
Definition: ssd1306.c:25
uint8_t Initialized
Definition: ssd1306.c:28