-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.cpp
129 lines (95 loc) · 2.65 KB
/
display.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
#include "display.h"
const int resetPin = 12; //SRCLR
const int clockPin = 11; //SRCLK //SHCP
const int latchPin = 10; //RCLK //STCP
const int outEnablePin = 9; //OE
const int dataPin = 8; //SER / dsp
const byte bitNumbers[10] =
{
0b11111100, //0
0b01100000, //1
0b11011010, //2
0b11110010, //3
0b01100110, //4
0b10110110, //5
0b10111110, //6
0b11100000, //7
0b11111110, //8
0b11110110 //9
};
const byte displayPointByte = 1; //can be used to add the display point with an OR operation
const byte clearDisplayByte = 0; //this byte when sent to the display clears everything.
void initializeDisplay(void)
{
pinMode(resetPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(outEnablePin, OUTPUT);
pinMode(dataPin, OUTPUT);
digitalWrite(outEnablePin, LOW);
digitalWrite(resetPin, HIGH);
}
void clearDisplay(void){
for(int i = amountOfDisplays - 1; i >= 0; i--){
bool isLast = false;
if(i == 0){
isLast = true;
}
writeByte(clearDisplayByte, isLast);
}
}
void writeByte(uint8_t bits,bool last)
{
digitalWrite(latchPin, LOW);
for(int i = 0; i < 8; i++){
digitalWrite(clockPin, LOW);
//set either 1 or 0 based on the rightmost bit on the bits...
uint8_t shiftedBits = bits;
digitalWrite(dataPin, (shiftedBits >> i) & 1);
digitalWrite(clockPin, HIGH);
digitalWrite(dataPin, LOW);
}
digitalWrite(clockPin, LOW);
if(last) { digitalWrite(latchPin, HIGH); }
}
void writeHighAndLowNumber(int numbers[])
{
for(int i = amountOfDisplays - 1; i >= 0; i--){
bool isLast = false;
if(i == 0){
isLast = true;
}
if(numbers[i] > 9 ) { numbers[i] = 9; }
if(numbers[i] < 0) { numbers[i] = 0; }
byte bits = bitNumbers[numbers[i]];
writeByte(bits, isLast);
}
}
void showResult(byte number)
{
//number has to be between these...
int maxAmount = positivePowerOfTen(amountOfDisplays);
if(number >= maxAmount){
number = maxAmount -1;
}
if(number < 0){
number = 0;
}
int numbers[amountOfDisplays];
for(int i = 0; i < amountOfDisplays; i++){
int thing = positivePowerOfTen(amountOfDisplays - 1 - i);
int currentNumber = number / thing;
number -= currentNumber * thing;
numbers[i] = currentNumber;
}
writeHighAndLowNumber(numbers);
}
int positivePowerOfTen(int n){
int result = 1;
if (n > 0) {
for (int i = 0; i < n; i++) {
result *= 10;
}
}
return result;
}