forked from xhbang/Xadow-digital-watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devices_oled.cpp
executable file
·273 lines (228 loc) · 6.92 KB
/
devices_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
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
/*
* Project: Digital Wristwatch
* Author: Zak Kemble, contact@zakkemble.co.uk
* Copyright: (C) 2013 by Zak Kemble
* License: GNU GPL v3 (see License.txt)
* Web: http://blog.zakkemble.co.uk/diy-digital-wristwatch/
*/
// OLED control
#include <avr/io.h>
#include <util/delay.h>
#include "common.h"
#include "devices_oled.h"
#include "devices_oled_defs.h"
//#include "drivers_spi.h"
#include "watchconfig.h"
#include <Wire.h>
/*shao #define CSN B2
#define DC B0
#define RST D7
#define spiSelect() (pinWrite(CSN, LOW))
#define spiDeselect() (pinWrite(CSN, HIGH))
#define cmdMode() (pinWrite(DC, LOW))
#define dataMode() (pinWrite(DC, HIGH))
#define rstHigh() (pinWrite(RST, HIGH))
#define rstLow() (pinWrite(RST, LOW))
static inline bool oled_select(void)
{
spi_enable();
spiSelect();
return true;
}
static inline bool oled_deselect(void)
{
spiDeselect();
spi_disable();
return false;
}
#define MODE_CMD (cmdMode())
#define MODE_DATA (dataMode())
#define CHIPSELECT(mode) mode; \
for(bool cs = oled_select(); cs; cs = oled_deselect())
*/
byte oledBuffer[FRAME_BUFFER_SIZE];
static void resetPosition(void);
static void sendCommand(unsigned char command);
static void sendData(unsigned char Data);
void oled_init()
{
/*shao pinMode(CSN, OUTPUT);
pinMode(DC, OUTPUT);
rstHigh();
pinMode(RST, OUTPUT);
delay(50);
rstLow();
delay(50);
rstHigh();
delay(50);
// sendCmd(OLED_OFF);
resetPosition();
sendCmd2(OLED_SETMUX, 63);
sendCmd2(OLED_DISPLAY_OFFSET, 0);
sendCmd(OLED_DISPLAY_START | 0);
oled_set180(watchConfig.display180);
sendCmd2(OLED_COMCFG, 0x12);
oled_setBrightness(watchConfig.brightness * 85);
sendCmd(OLED_DISPLAY_SHOWRAM);
oled_setInvert(watchConfig.invert);
sendCmd2(OLED_CLOCK_FREQ, 0x80);
sendCmd2(OLED_CHARGEPUMP, OLED_CHARGEPUMP_EN);
sendCmd(OLED_ON);
// sendCmd2(OLED_PRECHARGE_PERIOD, 0b11110001);//0xF1); // pre-charge period
// sendCmd2(OLED_VCOM_LEVEL, 0x20); // 0x40 // V-COM deselect level
sendCmd2(OLED_MEMMODE, OLED_MEM_HORIZ);
delay(20);
oled_flush();
delay(10);*/
sendCommand(SeeedOLED_Display_Off_Cmd); //display off
delay(5);
sendCommand(SeeedOLED_Display_On_Cmd); //display on
delay(5);
sendCommand(SeeedOLED_Normal_Display_Cmd); //Set Normal Display (default)
}
static void sendData(byte Data)
{
Wire.beginTransmission(SeeedOLED_Address); // begin I2C transmission
#if defined(ARDUINO) && ARDUINO >= 100
Wire.write(SeeedOLED_Data_Mode); // data mode
Wire.write(Data);
#else
Wire.send(SeeedOLED_Data_Mode); // data mode
Wire.send(Data);
#endif
Wire.endTransmission(); // stop I2C transmission
}
static void sendCommand(byte command)
{
Wire.beginTransmission(SeeedOLED_Address); // begin I2C communication
#if defined(ARDUINO) && ARDUINO >= 100
Wire.write(SeeedOLED_Command_Mode); // Set OLED Command mode
Wire.write(command);
#else
Wire.send(SeeedOLED_Command_Mode); // Set OLED Command mode
Wire.send(command);
#endif
Wire.endTransmission(); // End I2C communication
}
void setHorizontalMode()
{
sendCommand(0x20); //set addressing mode
sendCommand(0x00); //set horizontal addressing mode
}
void setPageMode()
{
sendCommand(0x20); //set addressing mode
sendCommand(0x02); //set page addressing mode
}
void setTextXY(unsigned char Row, unsigned char Column)
{
sendCommand(0xB0 + Row); //set page address
sendCommand(0x00 + (8*Column & 0x0F)); //set column lower address
sendCommand(0x10 + ((8*Column>>4)&0x0F)); //set column higher address
}
static void resetPosition()
{
/*shao CHIPSELECT(MODE_CMD)
{
spi_transfer_nr(0x22);
spi_transfer_nr(0x00);
spi_transfer_nr(0x07);
spi_transfer_nr(0x21);
spi_transfer_nr(0x00);
spi_transfer_nr(0x7F);
}*/
setPageMode();
setTextXY(0,0);
setHorizontalMode();
delay(5);
}
void oled_flush()
{
resetPosition();
/*shao CHIPSELECT(MODE_DATA)
{
debugPin_spi(HIGH);
// Here we load a byte from the global variable buffer into a fast local variable.
// This is done while we're waiting for the SPI transfer to complete.
// Delay cycles is also used to replace loop_until_bit_is_set().
// In total, we go from ~3.84ms transfer time of the basic loop to ~2.31ms using local variables and delay cycles. (4MHz SPI, 1KB frame buffer)
// As an added bonus we clear the frame buffer with the extra clock cycles which means we don't have to wait for memset() to clear it later on.
// The minimum possible transfer time for 4MHz SPI and 1KB frame buffer is 2.048ms. 12.5% overhead (1 cycle to read SPSR, 1 cycle to set SPDR)... not bad :3
for(uint i=0;i<FRAME_BUFFER_SIZE;i++)
{
byte next = oledBuffer[i]; // Load next byte
oledBuffer[i] = 0x00; // Clear buffer byte
// At max speed SPI takes 16 cycles to send a byte
// 8 cycles were used to increment the loop counter, compare the counter, loading next byte and clearing buffer byte
// 8 cycles are left
//shao __builtin_avr_delay_cycles(8);
SPSR; // Need to read register to clear SPIF
SPDR = next; // Send byte
}
// After sending the last byte the for loop exits, so we need to wait for the transfer to finish here
//shao __builtin_avr_delay_cycles(14);
SPSR;
//loop_until_bit_is_set(SPSR, SPIF);
// Original method
//for(uint i=0;i<FRAME_BUFFER_SIZE;i++)
// spi_transfer_nr(oledBuffer[i]);
debugPin_spi(LOW);
}*/
/*shao if (watchConfig.display180)
{
for (uint i = (FRAME_BUFFER_SIZE-1); i >=0; i--)
{
byte next = oledBuffer[i]; // Load next byte
oledBuffer[i] = 0x00; // Clear buffer byte
sendData(next);
}
}else
{*/
for (uint i = 0; i < FRAME_BUFFER_SIZE; i++)
{
//Serial.print(oledBuffer[i]);
byte next = oledBuffer[i]; // Load next byte
oledBuffer[i] = 0x00; // Clear buffer byte
sendData(next);
}
//shao }
}
void oled_power(bool on)
{
if (on) {
sendCommand(SeeedOLED_Display_On_Cmd); //display on
delay(5);
sendCommand(SeeedOLED_Normal_Display_Cmd); //Set Normal Display (default)
}else
{
sendCommand(SeeedOLED_Display_Off_Cmd); //display off
delay(5);
}
}
void oled_setBrightness(byte brightness)
{
sendCommand(SeeedOLED_Set_Brightness_Cmd);
sendCommand(brightness);
}
void oled_setInvert(bool invert)
{
if (invert) {
sendCommand(SeeedOLED_Inverse_Display_Cmd);
}else
{
sendCommand(SeeedOLED_Normal_Display_Cmd);
}
}
void oled_set180(bool rotate)
{
/*shao if(rotate)
{
sendCmd(OLED_SEG_REMAP);
sendCmd(OLED_SCANDIR_REMAP);
}
else
{
sendCmd(OLED_SEG_NML);
sendCmd(OLED_SCANDIR_NML);
}*/
}