forked from khampf/g13
-
Notifications
You must be signed in to change notification settings - Fork 1
/
g13_lcd.cpp
216 lines (190 loc) · 5.71 KB
/
g13_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
pixels are mapped rather strangely for G13 buffer...
byte 0 contains column 0 / row 0 - 7
byte 1 contains column 1 / row 0 - 7
so the masks for each pixel are laid out as below
(ByteOffset.PixelMask)
00.01 01.01 02.01 ...
00.02 01.02 02.02 ...
00.04 01.04 02.04 ...
00.08 01.08 02.08 ...
00.10 01.10 02.10 ...
00.20 01.20 02.20 ...
00.40 01.40 02.40 ...
00.80 01.80 02.80 ...
A0.01 A1.01 A2.01 ...
*/
#include "g13.hpp"
#include "g13_device.hpp"
#include "g13_fonts.hpp"
#include "logo.hpp"
#include <fstream>
#include <iostream>
#include <log4cpp/Category.hh>
namespace G13 {
void G13_Device::LcdInit() {
int error = libusb_control_transfer(handle, 0, 9, 1, 0, nullptr, 0, 1000);
if (error != LIBUSB_SUCCESS) {
G13_ERR("Error when initializing LCD endpoint: "
<< G13_Device::DescribeLibusbErrorCode(error));
} else {
LcdWrite(g13_logo, sizeof(g13_logo));
}
}
void G13_Device::LcdWrite(unsigned char *data, size_t size) {
if (size != G13_LCD_BUFFER_SIZE) {
G13_LOG(log4cpp::Priority::ERROR << "Invalid LCD data size " << size
<< ", should be " << G13_LCD_BUFFER_SIZE);
return;
}
unsigned char buffer[G13_LCD_BUFFER_SIZE + 32];
memset(buffer, 0, G13_LCD_BUFFER_SIZE + 32);
buffer[0] = 0x03;
memcpy(buffer + 32, data, G13_LCD_BUFFER_SIZE);
int bytes_written;
int error = libusb_interrupt_transfer(
handle, LIBUSB_ENDPOINT_OUT | G13_LCD_ENDPOINT, buffer,
G13_LCD_BUFFER_SIZE + 32, &bytes_written, 1000);
if (error) {
G13_LOG(log4cpp::Priority::ERROR << "Error when transferring image: "
<< DescribeLibusbErrorCode(error) << ", "
<< bytes_written << " bytes written");
}
}
void G13_Device::LcdWriteFile(const std::string &filename) {
std::filebuf *pbuf;
std::ifstream filestr;
size_t size;
filestr.open(filename.c_str());
pbuf = filestr.rdbuf();
size = pbuf->pubseekoff(0, std::ios::end, std::ios::in);
pbuf->pubseekpos(0, std::ios::in);
char buffer[size];
pbuf->sgetn(buffer, size);
filestr.close();
LcdWrite((unsigned char *) buffer, size);
}
void G13_LCD::Image(unsigned char *data, int size) {
m_keypad.LcdWrite(data, size);
}
G13_LCD::G13_LCD(G13_Device &keypad) : m_keypad(keypad) {
cursor_col = 0;
cursor_row = 0;
text_mode = 0;
}
/*
void G13_LCD::image_setpixel(unsigned row, unsigned col) {
unsigned offset = image_byte_offset(row, col); // col + (row /8 ) *
BYTES_PER_ROW * 8; unsigned char mask = 1 << ((row)&7);
if (offset >= G13_LCD_BUF_SIZE) {
G13_LOG(log4cpp::Priority::ERROR << "bad offset " << offset << " for "
<< (row) << " x "
<< (col));
return;
}
image_buf[offset] |= mask;
}
*/
/*
void G13_LCD::image_clearpixel(unsigned row, unsigned col) {
unsigned offset = image_byte_offset(row, col); // col + (row /8 ) *
BYTES_PER_ROW * 8; unsigned char mask = 1 << ((row)&7);
if (offset >= G13_LCD_BUF_SIZE) {
G13_LOG(log4cpp::Priority::ERROR << "bad offset " << offset << " for "
<< (row) << " x "
<< (col));
return;
}
image_buf[offset] &= ~mask;
}
*/
void G13_LCD::WritePos(int row, int col) {
cursor_row = row;
cursor_col = col;
if (cursor_col >= G13_LCD_COLUMNS) {
cursor_col = 0;
}
if (cursor_row >= G13_LCD_TEXT_ROWS) {
cursor_row = 0;
}
}
void G13_LCD::WriteChar(char c, unsigned int row, unsigned int col) {
if (row == -1) {
row = cursor_row;
col = cursor_col;
cursor_col += m_keypad.current_font().width();
if (cursor_col >= G13_LCD_COLUMNS) {
cursor_col = 0;
if (++cursor_row >= G13_LCD_TEXT_ROWS) {
cursor_row = 0;
}
}
}
unsigned offset =
image_byte_offset(row * G13_LCD_TEXT_CHEIGHT,
col); //*m_keypad.m_currentFont->m_width );
if (text_mode) {
memcpy(&image_buf[offset],
&m_keypad.current_font().char_data(c).bits_inverted,
m_keypad.current_font().width());
} else {
memcpy(&image_buf[offset],
&m_keypad.current_font().char_data(c).bits_regular,
m_keypad.current_font().width());
}
}
void G13_LCD::WriteString(const char *str) {
G13_OUT("writing \"" << str << "\"");
while (*str) {
if (*str == '\n') {
cursor_col = 0;
if (++cursor_row >= G13_LCD_TEXT_ROWS) {
cursor_row = 0;
}
} else if (*str == '\t') {
cursor_col += 4 - (cursor_col % 4);
if (++cursor_col >= G13_LCD_COLUMNS) {
cursor_col = 0;
if (++cursor_row >= G13_LCD_TEXT_ROWS) {
cursor_row = 0;
}
}
} else {
WriteChar(*str);
}
++str;
}
image_send();
}
/*
void G13_LCD::image_test(int x, int y) {
int row, col;
if (y >= 0) {
image_setpixel(x, y);
} else {
image_clear();
switch (x) {
case 1:
for (row = 0; row < G13_LCD_ROWS; ++row) {
col = row;
image_setpixel(row, col);
image_setpixel(row, G13_LCD_COLUMNS - col);
}
break;
case 2:
default:
for (row = 0; row < G13_LCD_ROWS; ++row) {
col = row;
image_setpixel(row, 8);
image_setpixel(row, G13_LCD_COLUMNS - 8);
image_setpixel(row, G13_LCD_COLUMNS / 2);
image_setpixel(row, col);
image_setpixel(row, G13_LCD_COLUMNS - col);
}
break;
}
}
image_send();
}
*/
} // namespace G13