-
Notifications
You must be signed in to change notification settings - Fork 0
/
prog.cpp
76 lines (63 loc) · 1.47 KB
/
prog.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
#include <iostream>
#include <unistd.h>
#include <string>
#include <fstream>
#include "prog.h"
Prog::Prog(int clockPin, int clearPin, int dataPin, int bits, int hold) {
clock = new GPIO(clockPin, holdTime);
clear = new GPIO(clearPin, holdTime);
data = new GPIO(dataPin, holdTime);
bitNum = bits;
holdTime = hold;
initialize();
}
void Prog::write(int data) {
int maskOffset = 16 - bitNum;
unsigned short int mask = 0x8000;
unsigned short int setValue = 0;
mask >>= maskOffset;
for(int i = 0; i < bitNum; i++) {
setValue = mask & data;
if (setValue != 0x0000) {
setBit(HIGH);
}
else {
setBit(LOW);
}
mask >>= 1;
}
}
Prog::~Prog() {
delete(clock);
delete(clear);
delete(data);
}
void Prog::status() {
std::cout << "CLOCK -- ";
clock->status();
std::cout << "CLEAR -- ";
clear->status();
std::cout << "DATA -- ";
data->status();
}
void Prog::initialize() {
clock->setDirection(OUT);
data->setDirection(OUT);
clear->setDirection(OUT);
clock->setValue(LOW);
data->setValue(LOW);
clear->setValue(HIGH); //ACTIVE LOW
}
void Prog::setBit(GPIO_VALUE val) {
data->setValue(val);
clock->setValue(LOW);
usleep(holdTime);
clock->setValue(HIGH);
usleep(holdTime);
clock->setValue(LOW);
}
void Prog::clearReg() {
clear->setValue(LOW);
usleep(holdTime);
clear->setValue(HIGH); //ACTIVE LOW
}