forked from luetee/ST7567S_128X64_I2C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathST7567S-IIC-128x64.cpp
144 lines (125 loc) · 3.72 KB
/
ST7567S-IIC-128x64.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
#include "ST7567S-IIC-128x64.h"
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
ST7567S_IIC::ST7567S_IIC(uint16_t w, uint16_t h) : GFXcanvas1(w, h)
{
}
void ST7567S_IIC::render()
{
for (int row = 0; row < HEIGHT / 8; row++)
{
WriteByte_command(0xb0 + row);
WriteByte_command(0x00);
WriteByte_command(0x10);
for (int col = 0; col < WIDTH; col++)
{
WriteByte_dat(buffer[row * WIDTH + col]); // row=bit0--bit7, display off
}
}
}
/******************************************
* Write a command to LCD.
* Define instructions on page 23 of the ST7567A data sheet.
*******************************************/
void ST7567S_IIC::WriteByte_command(int dat)
{
Wire.beginTransmission(addr); // transmit to device 0x3f
Wire.write(0x00); // Co=0,A0=0. data= Co-A0-0-0-0-0-0-0.
Wire.write(dat); // sends restart command.
Wire.endTransmission(); // stop transmitting
}
/******************************************
* Init the LCD.
* This initialization function is called when using LCD.
*******************************************/
void ST7567S_IIC::Init()
{
Wire.begin();
delay(10);
WriteByte_command(0xE2); // Internal reset
delay(10);
WriteByte_command(0xA2); // 0xa2 LCD bias ratio is 1/9; 0xa3 is bias ratio 1/7; 128X64=1/9
//// normal screen
WriteByte_command(0xA0); //(0xa0-0xa1 is ADC direction selection) 0xa0 is segment forward, 0xa1 is segment reverse
WriteByte_command(0xC8); // com output mode drive direction is reverse
//// flip screen
// WriteByte_command(0xA1); // may be bug, loss first 4 bits in each row
// WriteByte_command(0xC0);
// WriteByte_command(0xA7); // 0xA6 Normal, 0xA7 Inverse
//********Adjust display brightness********
WriteByte_command(0x25); // 0x20-0x27 is the internal Rb/Ra resistance adjustment setting of V5 voltage RR=4.5V
// WriteByte_command(0x24); // only 0x25 and 0x24 is ok
WriteByte_command(0x81); // Electricity Mode Settings
WriteByte_command(0x20);
WriteByte_command(0x2C); // Internal Power Supply Control Mode
WriteByte_command(0x2E);
WriteByte_command(0x2F);
// Clear(false); //initalize DDRAM
WriteByte_command(0xAF);
WriteByte_command(0x40);
}
/******************************************
* Write a data to LCD.
* Define instructions on page 23 of the ST7567A data sheet.
*******************************************/
void ST7567S_IIC::WriteByte_dat(int dat)
{
Wire.beginTransmission(addr); // transmit to device 0x7e
Wire.write(0x40); // Co=0,A0=1. data= Co-A0-0-0-0-0-0-0.
Wire.write(dat); // sends data.
Wire.endTransmission(); // stop transmitting
}
void ST7567S_IIC::drawPixel(int16_t x, int16_t y, uint16_t color)
{
if (buffer)
{
if ((x < 0) || (y < 0) || (x >= _width) || (y >= _height))
return;
int16_t t;
switch (rotation)
{
case 1:
t = x;
x = WIDTH - 1 - y;
y = t;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = HEIGHT - 1 - t;
break;
}
int paging = (y / 8);
int ny = y & 7;
int bytePos = paging * WIDTH + x;
if (bytePos < WIDTH * HEIGHT)
{
int b = buffer[bytePos];
int v = (color ? 0 : 1) << (7 - ny);
if (v != 0)
b = b | 1 << (ny);
else
b = b & (0xFF ^ (1 << (ny)));
buffer[bytePos] = b;
}
}
}
void ST7567S_IIC::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color)
{
for (int i = 0; i < w; i++)
{
drawPixel(x + i, y, color);
}
}
void ST7567S_IIC::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color)
{
for (int i = 0; i < h; i++)
{
drawPixel(x, y + i, color);
}
}