-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssd1306.h
127 lines (94 loc) · 3.29 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
#ifndef __SSD1306_H__
#define __SSD1306_H__
#include "hal.h"
/*===========================================================================*/
/* Derived constants and error checks. */
/*===========================================================================*/
#if !HAL_USE_I2C
#error "SSD1306 requires HAL_USE_I2C"
#endif
#define SSD1306_WIDTH 128
#define SSD1306_HEIGHT 64
#define SSD1306_WIDTH_FIXED (SSD1306_WIDTH + 1)
/*===========================================================================*/
/* Driver data structures and types. */
/*===========================================================================*/
typedef enum {
SSD1306_COLOR_BLACK = 0x00,
SSD1306_COLOR_WHITE = 0x01
} ssd1306_color_t;
typedef struct {
uint8_t fw;
uint8_t fh;
const uint16_t *dt;
} ssd1306_font_t;
typedef enum {
SSD1306_SAD_0X78 = (0x78 >> 1),
SSD1306_SAD_0X7A = (0x7A >> 1)
} ssd1306_sad_t;
typedef enum {
SSD1306_UNINIT = 0,
SSD1306_STOP = 1,
SSD1306_READY = 2,
} ssd1306_state_t;
typedef struct {
I2CDriver *i2cp;
const I2CConfig *i2ccfg;
ssd1306_sad_t sad;
} SSD1306Config;
#define _ssd1306_methods \
void (*updateScreen)(void *ip); \
void (*toggleInvert)(void *ip); \
void (*fillScreen)(void *ip, ssd1306_color_t color); \
void (*drawPixel)(void *ip, uint8_t x, uint8_t y, ssd1306_color_t color); \
void (*gotoXy)(void *ip, uint8_t x, uint8_t y); \
char (*putc)(void *ip, char ch, const ssd1306_font_t *font, ssd1306_color_t color); \
char (*puts)(void *ip, char *str, const ssd1306_font_t *font, ssd1306_color_t color); \
void (*setDisplay)(void *ip, uint8_t on);
struct SSD1306VMT {
_ssd1306_methods
};
#define _ssd1306_data \
ssd1306_state_t state; \
const SSD1306Config *config; \
typedef struct SSD1306Driver {
const struct SSD1306VMT *vmt;
_ssd1306_data;
uint8_t x;
uint8_t y;
uint8_t inv;
uint8_t fb[SSD1306_WIDTH_FIXED * SSD1306_HEIGHT / 8];
} SSD1306Driver;
/*===========================================================================*/
/* Driver macros. */
/*===========================================================================*/
#define ssd1306UpdateScreen(ip) \
(ip)->vmt->updateScreen(ip)
#define ssd1306ToggleInvert(ip) \
(ip)->vmt->toggleInvert(ip)
#define ssd1306FillScreen(ip, color) \
(ip)->vmt->fillScreen(ip, color)
#define ssd1306DrawPixel(ip, x, y, color) \
(ip)->vmt->drawPixel(ip, x, y, color)
#define ssd1306GotoXy(ip, x, y) \
(ip)->vmt->gotoXy(ip, x, y)
#define ssd1306Putc(ip, ch, font, color) \
(ip)->vmt->putc(ip, ch, font, color)
#define ssd1306Puts(ip, str, font, color) \
(ip)->vmt->puts(ip, str, font, color)
#define ssd1306SetDisplay(ip, on) \
(ip)->vmt->setDisplay(ip, on)
/*===========================================================================*/
/* External declarations. */
/*===========================================================================*/
#ifdef __cplusplus
extern "C" {
#endif
extern const ssd1306_font_t ssd1306_font_11x18;
void ssd1306ObjectInit(SSD1306Driver *devp);
void ssd1306Start(SSD1306Driver *devp, const SSD1306Config *config);
void ssd1306Stop(SSD1306Driver *devp);
#ifdef __cplusplus
}
#endif
#endif /* __SSD1306_H__ */