-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay7seg.cpp
58 lines (52 loc) · 1.54 KB
/
display7seg.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
#include <display7seg.hpp>
#include <avr/pgmspace.h>
namespace display7seg{
const uint8_t LUT_converter[] PROGMEM = {0b11111100, 0b01100000, 0b11011010, 0b11110010, 0b01100110, 0b10110110, 0b10111110, 0b11100000,
0b11111110, 0b11100110};
Display& Display::init(){
for(auto i : this->_segments){
i.output().set_low();
}
for(auto i : this->_digits){
i.output().set_high();
}
return *this;
}
Display& Display::write(uint8_t value, uint8_t posi){
if(value > 17 || posi > 3) return *this;
this->_number[posi] = value;
return *this;
}
Display& Display::write(uint16_t value){
if(value > 9999u) return *this;
uint8_t M = (value/1000)%10;
uint8_t C = (value/100)%10;
uint8_t D = (value/10)%10;
uint8_t U = (value/1)%10;
this->write(M, 0);
this->write(C, 1);
this->write(D, 2);
this->write(U, 3);
return *this;
}
Display& Display::update(){
for(auto dig : this->_digits){
dig.set_low();
}
this->_digits[disp].set_high();
this->send(pgm_read_byte_near(LUT_converter + this->_number[disp]));
this->disp++;
if(this->disp > 3) this->disp = 0;
return *this;
}
void Display::send(uint8_t value){
for(auto seg : this->_segments){
if(value & 0x80){
seg.set_low();
}else{
seg.set_high();
}
value <<= 1;
}
}
}