-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSeg7.cpp
93 lines (81 loc) · 1.67 KB
/
Seg7.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
/*
Seg7.h - A library to control the TPIC6B595 Serial to Parallel Shift Register
Created by Derek Chafin, October 8, 2011
Released into the public domain.
*/
#include "Seg7.h"
#include "WProgram.h"
#define GATE_DELAY 20
Seg7::Seg7(int dataPin, int clockPin, int latchPin, int clearPin)
{
_dataPin = dataPin;
_clockPin = clockPin;
_latchPin = latchPin;
_clearPin = clearPin;
}
void Seg7::init(byte* bin1)
{
digits = 1;
_bin1 = bin1;
}
void Seg7::init(byte* bin1, byte* bin2)
{
digits = 2;
_bin1 = bin1;
_bin2 = bin2;
}
void Seg7::init(byte* bin1, byte* bin2, byte* bin3)
{
digits = 3;
_bin1 = bin1;
_bin2 = bin2;
_bin3 = bin3;
}
void Seg7::init(byte* bin1, byte* bin2, byte* bin3, byte* bin4)
{
digits = 4;
_bin1 = bin1;
_bin2 = bin2;
_bin3 = bin3;
_bin4 = bin4;
}
void Seg7::sendNumber(int number)
{
clear();
switch(digits)
{
case 4:
shiftOut(_dataPin, _clockPin, LSBFIRST, _bin4[getDigit(number, 3)]);
case 3:
shiftOut(_dataPin, _clockPin, LSBFIRST, _bin3[getDigit(number, 2)]);
case 2:
shiftOut(_dataPin, _clockPin, LSBFIRST, _bin2[getDigit(number, 1)]);
case 1:
shiftOut(_dataPin, _clockPin, LSBFIRST, _bin1[getDigit(number, 0)]);
}
pulsePin(_latchPin, HIGH);
}
void Seg7::clear()
{
pulsePin(_clearPin, LOW);
pulsePin(_latchPin, HIGH);
}
int Seg7::getDigit(int number, int pos)
{
return (number / (int)pow(10, pos)) % 10;
}
void Seg7::pulsePin(int pin, int active)
{
if (active == HIGH)
{
digitalWrite(pin, HIGH);
delayMicroseconds(GATE_DELAY);
digitalWrite(pin, LOW);
}
else
{
digitalWrite(pin, LOW);
delayMicroseconds(GATE_DELAY);
digitalWrite(pin, HIGH);
}
}