-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDigitalTubeDisplay.cpp
136 lines (116 loc) · 2.82 KB
/
DigitalTubeDisplay.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
/*
* DigitalTubeDisplay.cpp
*
* Created on: 2012-2-12
* Author: Agu
*/
#include "DigitalTubeDisplay.h"
DigitalTubeDisplay::DigitalTubeDisplay(uint8_t * pin_digit,
uint8_t digit_length, uint8_t pin_A, uint8_t pin_B, uint8_t pin_C,
uint8_t pin_D, uint8_t pin_E, uint8_t pin_F, uint8_t pin_G,
uint8_t pin_DP) :
_digit_length(digit_length), _pin_digit(pin_digit)
{
_pin_segment[0] = pin_A;
_pin_segment[1] = pin_B;
_pin_segment[2] = pin_C;
_pin_segment[3] = pin_D;
_pin_segment[4] = pin_E;
_pin_segment[5] = pin_F;
_pin_segment[6] = pin_G;
_pin_segment[7] = pin_DP;
for (byte i = 0; i < _digit_length; i++)
pinMode(_pin_digit[i], OUTPUT);
for (byte i = 0; i < 8; i++)
pinMode(_pin_segment[i], OUTPUT);
this->turnOff();
this->setDelaySpan();
_cache = (byte*) malloc(sizeof(byte) * _digit_length);
}
void DigitalTubeDisplay::turnOff()
{
for (byte i = 0; i < _digit_length; i++)
digitalWrite(_pin_digit[i], LOW);
}
void DigitalTubeDisplay::turnOn()
{
for (byte i = 0; i < 8; i++)
digitalWrite(_pin_segment[i], LOW);
for (byte i = 0; i < _digit_length; i++)
digitalWrite(_pin_digit[i], HIGH);
}
DigitalTubeDisplay::~DigitalTubeDisplay()
{
free(_cache);
}
void DigitalTubeDisplay::putPattern(byte pattern)
{
for (byte i = 0; i < 8; i++)
{
digitalWrite(_pin_segment[i], bit_is_clear(pattern, i));
}
}
void DigitalTubeDisplay::setDelaySpan(uint16_t span)
{
_delaySpan = span;
}
void DigitalTubeDisplay::clear(byte val)
{
memset(_cache, val, _digit_length);
}
void DigitalTubeDisplay::display(word times)
{
while (times--)
for (byte i = 0; i < _digit_length; i++)
{
this->putPattern(_cache[i]);
digitalWrite(_pin_digit[i], HIGH);
delayMicroseconds(_delaySpan);
digitalWrite(_pin_digit[i], LOW);
}
}
void DigitalTubeDisplay::setPattern(byte index, byte pattern)
{
if (index >= _digit_length)
return;
_cache[index] = pattern;
}
void DigitalTubeDisplay::printf(const char *__fmt, ...)
{
char * p = (char*) malloc(sizeof(char) * (_digit_length + 1));
va_list ap;
va_start(ap, __fmt);
vsnprintf(p, 9, __fmt, ap);
va_end(ap);
this->clear();
byte index = 0;
byte ptr = 0;
while (p[index] && ptr < _digit_length)
{
if (p[index] >= '0' && p[index] <= '9')
{
_cache[ptr] = pgm_read_byte_near(
DIGITAL_TUBE_FONT + p[index] - 0x30);
ptr++;
}
else if (p[index] >= 'A' && p[index] <= 'Z')
{
_cache[ptr] = pgm_read_byte_near(DIGITAL_TUBE_FONT + p[index] - 55);
ptr++;
}
else if (p[index] >= 'a' && p[index] <= 'z')
{
_cache[ptr] = pgm_read_byte_near(DIGITAL_TUBE_FONT + p[index] - 87);
ptr++;
}
else if (p[index] == '.')
_cache[ptr ? ptr - 1 : 0] |= 0x80;
else
{
_cache[ptr] = 0x00;
ptr++;
}
index++;
}
free(p);
}