-
Notifications
You must be signed in to change notification settings - Fork 5
/
RAK1921_oled.cpp
159 lines (137 loc) · 3.38 KB
/
RAK1921_oled.cpp
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
/**
* @file RAK1921_oled.cpp
* @author Bernd Giesecke ([email protected])
* @brief Initialization and usage of RAK1921 OLED
* @version 0.1
* @date 2022-02-18
*
* @copyright Copyright (c) 2022
*
*/
#include "app.h"
#include <nRF_SSD1306Wire.h>
void disp_show(void);
void rak1921_show(void);
/** Width of the display in pixel */
#define OLED_WIDTH 128
/** Height of the display in pixel */
#define OLED_HEIGHT 64
/** Height of the status bar in pixel */
#define STATUS_BAR_HEIGHT 11
/** Height of a single line */
#define LINE_HEIGHT 10
/** Number of message lines */
#define NUM_OF_LINES (OLED_HEIGHT - STATUS_BAR_HEIGHT) / LINE_HEIGHT
/** Line buffer for messages */
char disp_buffer[NUM_OF_LINES + 1][32] = {0};
/** Current line used */
uint8_t current_line = 0;
/** Display class using Wire */
SSD1306Wire oled_display(0x3c, PIN_WIRE_SDA, PIN_WIRE_SCL, GEOMETRY_128_64, &Wire);
/**
* @brief Initialize the display
*
* @return true always
* @return false never
*/
bool init_rak1921(void)
{
Wire.begin();
delay(500); // Give display reset some time
oled_display.setI2cAutoInit(true);
oled_display.init();
oled_display.displayOff();
oled_display.clear();
oled_display.displayOn();
oled_display.setBrightness(200);
// oled_display.flipScreenVertically();
oled_display.setContrast(100, 241, 64);
oled_display.setFont(ArialMT_Plain_10);
oled_display.display();
return true;
}
/**
* @brief Write the top line of the display
*/
void rak1921_write_header(char *header_line)
{
oled_display.setFont(ArialMT_Plain_10);
// clear the status bar
oled_display.setColor(BLACK);
oled_display.fillRect(0, 0, OLED_WIDTH, STATUS_BAR_HEIGHT);
oled_display.setColor(WHITE);
oled_display.setTextAlignment(TEXT_ALIGN_LEFT);
oled_display.drawString(0, 0, header_line);
// draw divider line
oled_display.drawLine(0, 11, 128, 11);
oled_display.display();
}
/**
* @brief Add a line to the display buffer
*
* @param line Pointer to char array with the new line
*/
void rak1921_add_line(char *line)
{
if (current_line == NUM_OF_LINES)
{
// Display is full, shift text one line up
for (int idx = 0; idx < NUM_OF_LINES; idx++)
{
memcpy(disp_buffer[idx], disp_buffer[idx + 1], 32);
}
current_line--;
}
snprintf(disp_buffer[current_line], 32, "%s", line);
if (current_line != NUM_OF_LINES)
{
current_line++;
}
rak1921_show();
}
/**
* @brief Update display messages
*
*/
void rak1921_show(void)
{
oled_display.setColor(BLACK);
oled_display.fillRect(0, STATUS_BAR_HEIGHT + 1, OLED_WIDTH, OLED_HEIGHT);
oled_display.setFont(ArialMT_Plain_10);
oled_display.setColor(WHITE);
oled_display.setTextAlignment(TEXT_ALIGN_LEFT);
for (int line = 0; line < current_line; line++)
{
oled_display.drawString(0, (line * LINE_HEIGHT) + STATUS_BAR_HEIGHT + 1, disp_buffer[line]);
}
oled_display.display();
}
/**
* @brief Clear the display
*
*/
void rak1921_clear(void)
{
oled_display.setColor(BLACK);
oled_display.fillRect(0, STATUS_BAR_HEIGHT + 1, OLED_WIDTH, OLED_HEIGHT);
current_line = 0;
}
/**
* @brief Write a line at given position
*
* @param line line number
* @param y_pos x position to start
* @param text String text
*/
void rak1921_write_line(int16_t line, int16_t y_pos, String text)
{
oled_display.drawString(y_pos, (line * LINE_HEIGHT) + STATUS_BAR_HEIGHT + 1, text);
}
/**
* @brief Display the buffer
*
*/
void rak1921_display(void)
{
oled_display.display();
}