-
Notifications
You must be signed in to change notification settings - Fork 2
/
lcd.cpp
148 lines (131 loc) · 3.03 KB
/
lcd.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
137
138
139
140
141
142
143
144
145
146
147
148
#include <Arduino.h>
#include <Wire.h>
#include <Time.h>
#include "vivarium.h"
#include "lcd.h"
struct window scrollWindow;
void cmd(byte code) {
Wire.beginTransmission(I2C_LCD_ADDR);
Wire.write(LCD_REG_CMD);
Wire.write(code);
Wire.endTransmission();
}
void cls() {
cmd(LCD_CMD_CLEAR_SCR);
}
void cursorOff() {
cmd(LCD_CMD_CURSOR_OFF);
}
void cursorOn() {
cmd(LCD_CMD_CURSOR_ON);
}
char decodeKey(int code)
{
switch(code) {
case 1:
return '1';
case 2:
return '2';
case 4:
return '3';
case 8:
return '4';
case 16:
return '5';
case 32:
return '6';
case 64:
return '7';
case 128:
return '8';
case 256:
return '9';
case 512:
return '*';
case 1024:
return '0';
case 2048:
return '#';
default:
return 0;
}
}
char readKey() {
Wire.beginTransmission(I2C_LCD_ADDR);
Wire.write(LCD_REG_KEY_LO);
Wire.endTransmission();
Wire.requestFrom(I2C_LCD_ADDR,1);
byte loByte=(byte)Wire.read();
Wire.beginTransmission(I2C_LCD_ADDR);
Wire.write(LCD_REG_KEY_HI);
Wire.endTransmission();
Wire.requestFrom(I2C_LCD_ADDR,1);
byte hiByte=(byte)Wire.read();
return decodeKey(hiByte << 8 | loByte);
}
void at(byte x,byte y) {
Wire.beginTransmission(I2C_LCD_ADDR);
Wire.write(LCD_REG_CMD);
Wire.write(LCD_CMD_CURSOR_LOC);
Wire.write(y);
Wire.write(x);
Wire.endTransmission();
}
void writeAt(byte x,byte y,const char *msg) {
at(x,y);
write(msg);
}
void writeAt_P(byte x,byte y,PGM_P msg) {
at(x,y);
write_P(msg);
}
void write(const char *buf) {
Wire.beginTransmission(I2C_LCD_ADDR);
Wire.write(LCD_REG_CMD);
Wire.write(buf);
Wire.endTransmission();
}
void write_P(PGM_P msg) {
Wire.beginTransmission(I2C_LCD_ADDR);
Wire.write(LCD_REG_CMD);
char c;
while((c=pgm_read_byte(msg++))!=0) {
Wire.write(c);
}
Wire.endTransmission();
}
void fmt2DigitsR(char *buf,int num,int radix) {
char ibuf[3];
ibuf[2]=0;
itoa(num,ibuf,radix);
if(ibuf[1]==0) {
ibuf[0]='0';
itoa(num,ibuf+1,radix);
}
memcpy(buf,ibuf,2);
}
void fmt2XDigits(char *buf,int num) {
fmt2DigitsR(buf,num,RADIX_HEX);
}
void fmt2Digits(char *buf,int num) {
fmt2DigitsR(buf,num,RADIX_DEC);
}
int scrollCountDown=0;
void scroll() {
if(scrollCountDown-->0) return;
char visible[scrollWindow.length+1];
memset(visible,' ',scrollWindow.length);
visible[scrollWindow.length]=0;
int textLen=strlen(scrollWindow.text);
int textLeft=textLen-scrollWindow.start;
memcpy(visible,&scrollWindow.text[scrollWindow.start],min(textLeft,scrollWindow.length));
if(textLeft<scrollWindow.length-1) {
memcpy(&visible[textLeft+1],scrollWindow.text,scrollWindow.length-textLeft-1);
}
writeAt(scrollWindow.offset,scrollWindow.line,visible);
scrollWindow.start++;
if(scrollWindow.start==textLen) {
scrollWindow.start=0;
}
scrollCountDown=6;
}