forked from arachnidlabs/minishift-arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minishift.cpp
73 lines (63 loc) · 1.66 KB
/
minishift.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
#include "minishift.h"
#include "glcdfont.h"
Minishift::Minishift(int data_pin, int clock_pin, int latch_pin) {
this->data_pin = data_pin;
this->clock_pin = clock_pin;
this->latch_pin = latch_pin;
this->latch_state = HIGH;
pinMode(data_pin, OUTPUT);
pinMode(clock_pin, OUTPUT);
digitalWrite(latch_pin, HIGH);
pinMode(latch_pin, OUTPUT);
}
void Minishift::startTransaction() {
if(this->latch_state != LOW) {
this->latch_state = LOW;
digitalWrite(this->latch_pin, this->latch_state);
}
}
void Minishift::writeColumns(const uint8_t *buf, int len) {
this->writeColumns(buf, len, -1);
}
void Minishift::writeColumns(const uint8_t *buf, int len, int ms) {
for(int i = 0; i < len; i++) {
this->startTransaction();
shiftOut(this->data_pin, this->clock_pin, LSBFIRST, buf[i]);
this->update();
if(ms != -1) {
delay(ms);
}
}
}
void Minishift::writeString(const char *str) {
this->writeString(str, -1);
}
void Minishift::writeString(const char *str, int ms) {
this->writeString(str, ms, 0);
}
void Minishift::writeString(const char *str, int ms, int trailing) {
for(const char *c = str; *c != '\0'; c++) {
for(int col = 0; col < 5; col++) {
this->startTransaction();
shiftOut(this->data_pin, this->clock_pin, LSBFIRST, pgm_read_byte(font + (*c * 5) + col));
this->update();
if(ms != -1) {
delay(ms);
}
}
for(int col = 0; col < trailing; col++) {
this->startTransaction();
shiftOut(this->data_pin, this->clock_pin, LSBFIRST, 0);
this->update();
if(ms != -1) {
delay(ms);
}
}
}
}
void Minishift::update() {
if(this->latch_state != HIGH) {
this->latch_state = HIGH;
digitalWrite(this->latch_pin, this->latch_state);
}
}