-
Notifications
You must be signed in to change notification settings - Fork 1
/
HUB08.cpp
166 lines (132 loc) · 4.33 KB
/
HUB08.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
/*********************************************************************
This is a library for our Monochrome OLEDs based on SSD1306 drivers
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/category/63_98
These displays use SPI to communicate, 4 or 5 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, check license.txt for more information
All text above, and the splash screen below must be included in any redistribution
*********************************************************************/
#ifdef __AVR__
#include <avr/pgmspace.h>
#elif defined(ESP8266)
#include <pgmspace.h>
#else
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
#endif
#if !defined(__ARM_ARCH) && !defined(ENERGIA) && !defined(ESP8266)
#include <util/delay.h>
#endif
#include <stdlib.h>
#include <Wire.h>
#include <SPI.h>
#include "Adafruit_GFX.h"
#include "HUB08.h"
// the memory buffer for the LCD
static uint8_t buffer[HUB08_LCDHEIGHT * HUB08_LCDWIDTH / 8] = {
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x01, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60,
0x01, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0xE0, 0x07, 0x8F, 0xC7, 0xC7, 0xC7, 0xE0,
0x00, 0x40, 0x0C, 0xCE, 0x6C, 0x6C, 0x6C, 0xE0, 0x00, 0xE0, 0x0C, 0x0C, 0x6C, 0x6C, 0x6C, 0x60,
0x01, 0xF0, 0x07, 0x8C, 0x6F, 0xEF, 0xEC, 0x60, 0x23, 0xF8, 0x00, 0xCC, 0x6C, 0x0C, 0x0C, 0x60,
0x33, 0xF8, 0x0C, 0xCE, 0x6C, 0x6C, 0x6C, 0xE0, 0x3B, 0xF8, 0x07, 0x8F, 0xC7, 0xC7, 0xC7, 0xE0,
0x3B, 0xF8, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x1B, 0xF8, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00,
0x0B, 0xF8, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// the most basic function, set a single pixel
void HUB08::drawPixel(int16_t x, int16_t y, uint16_t color) {
if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
return;
uint8_t *byte = buffer + x / 8 + y * width() / 8;
uint8_t bit = x % 8;
if (color) {
*byte |= 0x80 >> bit;
} else {
*byte &= ~(0x80 >> bit);
}
/*
// x is which column
switch (color)
{
case WHITE: buffer[x+ (y/8)*HUB08_LCDWIDTH] |= (1 << (y&7)); break;
case BLACK: buffer[x+ (y/8)*HUB08_LCDWIDTH] &= ~(1 << (y&7)); break;
case INVERSE: buffer[x+ (y/8)*HUB08_LCDWIDTH] ^= (1 << (y&7)); break;
}
*/
}
HUB08::HUB08(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t oe, uint8_t r1, uint8_t stb, uint8_t clk) :
Adafruit_GFX(HUB08_LCDWIDTH, HUB08_LCDHEIGHT) {
this->clk = clk;
this->r1 = r1;
this->stb = stb;
this->oe = oe;
this->a = a;
this->b = b;
this->c = c;
this->d = d;
mask = 0xff;
state = 0;
}
void HUB08::begin()
{
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(oe, OUTPUT);
pinMode(r1, OUTPUT);
pinMode(clk, OUTPUT);
pinMode(stb, OUTPUT);
state = 1;
}
void HUB08::scan()
{
static uint8_t row = 0; // from 0 to 15
if (!state) {
return;
}
uint8_t *head = buffer + row * (width() / 8);
for (uint8_t line = 0; line < (height() / 16); line++) {
uint8_t *ptr = head;
head += width() * 2; // width * 16 / 8
for (uint8_t byte = 0; byte < (width() / 8); byte++) {
uint8_t pixels = *ptr;
ptr++;
pixels = pixels ^ mask; // reverse: mask = 0xff, normal: mask =0x00
for (uint8_t bit = 0; bit < 8; bit++) {
digitalWrite(clk, LOW);
digitalWrite(r1, pixels & (0x80 >> bit));
digitalWrite(clk, HIGH);
}
}
}
digitalWrite(oe, HIGH); // disable display
// select row
digitalWrite(a, (row & 0x01));
digitalWrite(b, (row & 0x02));
digitalWrite(c, (row & 0x04));
digitalWrite(d, (row & 0x08));
// latch data
digitalWrite(stb, LOW);
digitalWrite(stb, HIGH);
digitalWrite(stb, LOW);
digitalWrite(oe, LOW); // enable display
row = (row + 1) & 0x0F;
}
void HUB08::on()
{
state = 1;
}
void HUB08::off()
{
state = 0;
digitalWrite(oe, HIGH);
}
// clear everything
void HUB08::clearDisplay(void) {
memset(buffer, 0, (HUB08_LCDWIDTH*HUB08_LCDHEIGHT/8));
}