-
Notifications
You must be signed in to change notification settings - Fork 4
/
DualDisplayDriver.cpp
143 lines (122 loc) · 4.02 KB
/
DualDisplayDriver.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
#include "Arduino.h"
#include "DualDisplayDriver.h"
DualDisplayDriver::DualDisplayDriver(uint8_t i2c_address, Snapshot *snapshot)
{
this->i2c_address = i2c_address;
this->snapshot = snapshot;
}
void DualDisplayDriver::init()
{
for(uint8_t digit=0; digit<8; digit++)
{
digits[digit] = 0;
}
// Reset chip (Why? I have no idea.)
Wire.beginTransmission(0x00);
Wire.write(0x0C);
Wire.write(0x01 | 0x01);
Wire.endTransmission();
// Set display driver I2C address
Wire.beginTransmission(0x00); // Default I2C address
Wire.write(0x2D); // Command to set self addressing register
Wire.write(i2c_address); // Value of new I2C address
Wire.endTransmission();
delay(20);
// Decode modes and hex codes
//
// 0x00 = No decode for digits 7:0
// 0x01 = Code-B/HEX decode for digit 0. No decodee for digits 7:1
// 0x07 = Code-B/HEX decode for digit 0:2. No decodee for digits 7:3
// 0x3F = Code-B/HEX decode for digit 0:5. No decodee for digits 7:6
// 0x25 = Code-B/HEX decode for digit 0,2,5. No decodee for digits 1,3,4,6,7
//
// Set decode mode
Wire.beginTransmission(i2c_address);
Wire.write(0x09); // Command to set decode mode
Wire.write(0x00); // No decode for digits 7:0
Wire.endTransmission();
// Set global intensity
Wire.beginTransmission(i2c_address);
Wire.write(0x0A); // Command to set intensity control register (globally)
Wire.write(this->snapshot->display_intensity); // Intensity: range: 0x00, 0x01, 0x02... 0x0F
Wire.endTransmission();
// Set scan limit (which digits should be displayed)
Wire.beginTransmission(i2c_address);
Wire.write(0x0B); // Command to set scan limit
Wire.write(0x07); // Display digits 0:7
Wire.endTransmission();
// Shutdown modes and hex codes:
//
// 0x00 = Shutdown Mode, Reset Feature Register to Default Settings
// 0x80 = Shutdown Mode, Feature Register Unchanged
// 0x01 = Normal Operation, Reset Feature Register To Default Settings
// 0x81 = Normal Operation, Feature Register Unchanged
//
// Set shutdown mode
Wire.beginTransmission(i2c_address);
Wire.write(0x0C); // Command to set Shutdown Register
Wire.write(0x01); // Set to normal operation, reset feature register
Wire.endTransmission();
}
void DualDisplayDriver::setIntensity(uint8_t intensity) // 0 - 16
{
if(this->snapshot->display_intensity != intensity)
{
this->intensity = intensity;
this->snapshot->setDisplayIntensity(intensity);
Wire.beginTransmission(i2c_address);
Wire.write(0x0A); // Command to set intensity control register (globally)
Wire.write(intensity); // Intensity: range: 0x00, 0x01, 0x02... 0x0F
Wire.endTransmission();
}
}
void DualDisplayDriver::write(uint8_t display, uint16_t value)
{
if(display == DISPLAY_A)
{
this->writeDigit(0, value / 1000);
this->writeDigit(1, (value / 100) % 10);
this->writeDigit(2, (value / 10) % 10);
this->writeDigit(3, value % 10);
}
if(display == DISPLAY_B)
{
this->writeDigit(4, value / 1000);
this->writeDigit(5, (value / 100) % 10);
this->writeDigit(6, (value / 10) % 10);
this->writeDigit(7, value % 10);
}
}
void DualDisplayDriver::writeSetting(uint8_t display, uint16_t value, byte byte_code)
{
if(display == DISPLAY_A)
{
this->writeByteCode(0, byte_code);
this->writeDigit(1, (value / 100) % 10);
this->writeDigit(2, (value / 10) % 10);
this->writeDigit(3, value % 10);
}
if(display == DISPLAY_B)
{
this->writeByteCode(4, byte_code);
this->writeDigit(5, (value / 100) % 10);
this->writeDigit(6, (value / 10) % 10);
this->writeDigit(7, value % 10);
}
}
void DualDisplayDriver::writeDigit(byte digit, byte value)
{
byte byte_code = this->byte_codes[value];
this->writeByteCode(digit, byte_code);
}
void DualDisplayDriver::writeByteCode(byte digit, byte byte_code)
{
if(digits[digit] != byte_code)
{
Wire.beginTransmission(i2c_address);
Wire.write(digit + 1);
Wire.write(byte_code);
Wire.endTransmission();
digits[digit] = byte_code;
}
}