-
Notifications
You must be signed in to change notification settings - Fork 2
/
display.hpp
149 lines (134 loc) · 3.65 KB
/
display.hpp
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
149
/*
The MIT License (MIT)
Copyright (c) 2016 Madis Kaal <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef __display_hpp__
#define __display_hpp__
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
// display class for dual bubble display, with scrolling text support
//
class Display
{
char buf[65]; // this is string currently displayed, so that scrolling can happen
uint8_t cp; // next character address in buf
uint8_t fofs; // visible frame offset
// write single character at adr, starting from left
void write(uint8_t adr,uint8_t data)
{
if (adr>3)
adr=(adr&3)|4; // two lowest bits are adr, next two are chipsels
else
adr=(adr&3)|8;
adr=adr^3; // the display addresses characters from right to left
PORTB=(PORTB&0xf0)|adr|0x0c; // apply address, both CS high
PORTD=data|0x80; // apply data, WR high
PORTB=(PORTB&0xf0)|adr; // appropriate CS low
_delay_us(1);
PORTD=data&0x7f; // WR low
_delay_us(1);
PORTD=data|0x80; // WR high
PORTB|=0x0c; // both CS high
}
public:
Display()
{
clear();
}
// show a single frame from current offset
void refresh(void)
{
int8_t i;
for (i=0;(i+fofs)<cp && i<8;i++)
write(i,buf[i+fofs]);
while (i<8) {
write(i,' ');
i++;
}
}
// advance visible frame by one character and update display
// returns 1 if frame wrapped back to beginning, 0 if more
// text to show
int8_t scroll(void)
{
if (fofs>=cp) {
fofs=0;
refresh();
return 1;
}
fofs++;
refresh();
return fofs>=cp;
}
// clear display, and string buffer
void clear(void)
{
memset(buf,'\0',sizeof(buf));
cp=0;
fofs=0;
refresh();
}
// write a character to display buffer at current
// cursor position. the display is not refreshed
void putc(char c)
{
uint8_t i;
if (cp>=sizeof(buf)-1)
{
for (i=0;i<sizeof(buf)-1;i++)
buf[i]=buf[i+1];
cp=i;
}
if (c>='a' && c<='z')
c=c-('a'-'A');
buf[cp++]=c;
buf[cp]='\0';
}
// put a string to display buffer, visible frame to beginning,
// and refresh display
void puts(const char* s)
{
char c;
cp=0;
while (s && *s && cp<sizeof(buf)-1)
{
c=*s++;
if (c>='a' && c<='z')
c=c-('a'-'A');
buf[cp++]=c;
}
buf[cp]='\0';
fofs=0;
refresh();
}
// print 32 bit decimal number at cursor position
// and refresh display
void putn(int32_t n)
{
if (n<0) {
putc('-');
n=0-n;
}
if (n>9)
putn(n/10);
putc((n%10)+'0');
refresh();
}
};
#endif