-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcd_client.c
402 lines (334 loc) · 14.1 KB
/
lcd_client.c
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include <wiringPiI2C.h>
#include <wiringPi.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <pthread.h>
// Define some device parameters
#define I2C_ADDR 0x27 // I2C device address
// Define some device constants
#define LCD_CHR 1 // Mode - Sending data
#define LCD_CMD 0 // Mode - Sending command
#define LINE1 0x80 // 1st line
#define LINE2 0xC0 // 2nd line
#define LCD_BACKLIGHT 0x08 // On
// LCD_BACKLIGHT = 0x00 # Off
#define ENABLE 0b00000100 // Enable bit
#define buzzer_pin 18
#define SERVER_IP "192.168.73.7" // 서버 IP 주소
#define SERVER_PORT 8080 // 서버 포트 번호
pthread_mutex_t lock;
void lcd_init(void);
void lcd_byte(int bits, int mode);
void lcd_toggle_enable(int bits);
// added by Lewis
void typeInt(int i);
void typeFloat(float myFloat);
void lcdLoc(int line); //move cursor
void ClrLcd(void); // clr LCD return home
void typeln(const char *s);
void typeChar(char val);
int fd; // seen by all subroutines
void createCustomChar(int location, unsigned char charmap[]);
void init_korean();
void init_vowel_type1();
void init_multi_type1();
void error_handling(char *message) {
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
// typedef struct korean{
// int top;
// int middle;
// int bottom;
// unsigned char upper_value[8];
// unsigned char lower_value[8];
// } korean;
typedef struct korean{
int key;
unsigned char value[8];
} korean;
korean consonant[14];
korean vowel[9];
korean multi[70];
unsigned char empty[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ,0x00};
int word_cnt = 0;
void loadCharacter(int front, int middle, int back, int cnt_idx) {
if (front > 0) {
createCustomChar(0 + cnt_idx * 4, consonant[front - 1].value);
createCustomChar(1 + cnt_idx * 4, vowel[middle - 1].value);
if (back == 0) {
createCustomChar(2 + cnt_idx * 4, empty);
createCustomChar(3 + cnt_idx * 4, empty);
} else {
createCustomChar(2 + cnt_idx * 4, consonant[back - 1].value);
createCustomChar(3 + cnt_idx * 4, empty);
}
} else {
createCustomChar(0 + cnt_idx * 4, multi[middle - 1].value);
if (back == 0) {
createCustomChar(2 + cnt_idx * 4, empty);
} else {
createCustomChar(2 + cnt_idx * 4, consonant[back - 1].value);
}
createCustomChar(1 + cnt_idx * 4, empty);
createCustomChar(3 + cnt_idx * 4, empty);
}
}
void* input_thread(void* arg) {
int sock;
struct sockaddr_in serv_addr;
char msg[7]; // 서버로부터 받을 메시지
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock == -1) error_handling("socket() error");
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
serv_addr.sin_port = htons(SERVER_PORT);
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1)
error_handling("connect() error");
printf("Connection established\n");
while (1) {
if (read(sock, msg, sizeof(msg) - 1) <= 0) {
error_handling("read() error");
}
else {
printf("Receive message from Server : %s\n", msg);
msg[6] = '\0';
pthread_mutex_lock(&lock);
word_cnt++;
if (word_cnt > 2) {
word_cnt = 1;
}
int front_h = (msg[0] - '0');
int front_l = (msg[1] - '0');
int middle_h = (msg[2] - '0');
int middle_l = (msg[3] - '0');
int back_h = (msg[4] - '0');
int back_l = (msg[5] - '0');
int front = front_h * 10 + front_l;
int middle = middle_h * 10 + middle_l;
int back = back_h * 10 + back_l;
loadCharacter(front, middle, back, word_cnt - 1);
pthread_mutex_unlock(&lock);
wiringPiSetupGpio();
pinMode(buzzer_pin, PWM_OUTPUT);
pwmSetClock(19);
pwmSetMode(PWM_MODE_MS);
pwmSetRange(1000000 / 262);
pwmWrite(buzzer_pin, 1000000 / 262 / 2);
delay(300);
pwmWrite(buzzer_pin, 0);
printf("word_cnt : %d\n", word_cnt);
}
}
return NULL;
}
void* display_thread(void* arg) {
int start_idx = 0;
while (1) {
pthread_mutex_lock(&lock);
int local_word_cnt = word_cnt;
pthread_mutex_unlock(&lock);
if (local_word_cnt > 0) {
ClrLcd();
lcdLoc(LINE1);
for (int i = 0; i < local_word_cnt; i++) {
typeChar(0 + (i * 4));
typeChar(1 + (i * 4));
}
lcdLoc(LINE2);
for (int i = 0; i < local_word_cnt; i++) {
typeChar(2 + (i * 4));
typeChar(3 + (i * 4));
}
}
delay(1000); // 10 seconds delay
start_idx++;
}
return NULL;
}
int main() {
if (wiringPiSetup() == -1) exit(1);
fd = wiringPiI2CSetup(I2C_ADDR);
lcd_init();
init_korean();
init_vowel_type1();
init_multi_type1();
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, input_thread, NULL);
pthread_create(&tid2, NULL, display_thread, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
void init_korean(){
// phoneme.key = (int)'고';
unsigned char word[14][8] = {{0x00, 0x1F, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x10, 0x10, 0x1E, 0x00, 0x00, 0x00, 0x00},
{ 0x00, 0x1E, 0x10, 0x1E, 0x00, 0x00, 0x00, 0x00}, { 0x00, 0x1E, 0x02, 0x1E, 0x10, 0x1E, 0x00, 0x00},
{ 0x00, 0x1E, 0x12, 0x12, 0x1E, 0x00, 0x00, 0x00}, { 0x00, 0x0A, 0x0E, 0x0A, 0x0E, 0x00, 0x00, 0x00},
{ 0x00, 0x04, 0x04, 0x0A, 0x11, 0x00, 0x00, 0x00}, { 0x00, 0x0C, 0x12, 0x12, 0x0C, 0x00, 0x00, 0x00},
{ 0x00, 0x1F, 0x04, 0x0A, 0x11, 0x00, 0x00, 0x00}, { 0x04, 0x1F, 0x04, 0x0A, 0x11, 0x00, 0x00, 0x00},
{ 0x00, 0x1E, 0x02, 0x0E, 0x02, 0x00, 0x00, 0x00}, { 0x00, 0x1E, 0x10, 0x1E, 0x10, 0x1E, 0x00, 0x00},
{ 0x00, 0x1F, 0x0A, 0x1F, 0x00, 0x00, 0x00, 0x00}, { 0x04, 0x1F, 0x00, 0x0E, 0x11, 0x11, 0x0E, 0x00}
};
// unsigned char giyuck[] = { 0x00, 0x1F, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 };
for(int i = 0; i < 14; i++)
{
memcpy(consonant[i].value, word[i], sizeof(word[i]));
consonant[i].key = i+1;
}
}
void init_vowel_type1(){
unsigned char word[9][8] = {{ 0x00, 0x08, 0x08, 0x0C, 0x08, 0x08, 0x00, 0x00}, { 0x00, 0x08, 0x0C, 0x08, 0x0C, 0x08, 0x00, 0x00},
{ 0x00, 0x08, 0x08, 0x18, 0x08, 0x08, 0x00, 0x00}, { 0x00, 0x08, 0x18, 0x08, 0x18, 0x08, 0x00, 0x00},
{ 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00},
{ 0x00, 0x0A, 0x0A, 0x0E, 0x0A, 0x0A, 0x00, 0x00}, { 0x00, 0x0A, 0x0A, 0x1A, 0x0A, 0x0A, 0x00, 0x00},
{ 0x00, 0x0A, 0x0E, 0x0A, 0x0E, 0x0A, 0x00, 0x00}, { 0x00, 0x0A, 0x1A, 0x0A, 0x1A, 0x0A, 0x00, 0x00}
};
// unsigned char giyuck[] = { 0x00, 0x1F, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 };
for(int i = 0; i < 9; i++)
{
memcpy(vowel[i].value, word[i], sizeof(word[i]));
vowel[i].key = i+1;
}
}
void init_multi_type1(){
unsigned char word[70][8] = {
// ㅗ 0x04, 0x1F, 0x00
{ 0x00, 0x1F, 0x01, 0x01, 0x00, 0x04, 0x1F, 0x00}, { 0x00, 0x10, 0x10, 0x1F, 0x00, 0x04, 0x1F, 0x00},
{ 0x00, 0x1F, 0x10, 0x1F, 0x00, 0x04, 0x1F, 0x00}, { 0x1F, 0x01, 0x1F, 0x10, 0x1F, 0x04, 0x1F, 0x00},
{ 0x1F, 0x11, 0x11, 0x1F, 0x00, 0x04, 0x1F, 0x00}, { 0x00, 0x0A, 0x0E, 0x0A, 0x0E, 0x04, 0x1F, 0x00},
{ 0x00, 0x04, 0x04, 0x0A, 0x11, 0x04, 0x1F, 0x00}, { 0x0E, 0x11, 0x11, 0x0E, 0x00, 0x04, 0x1F, 0x00},
{ 0x00, 0x00, 0x1F, 0x0A, 0x11, 0x04, 0x1F, 0x00}, { 0x00, 0x04, 0x1F, 0x0A, 0x11, 0x04, 0x1F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}, { 0x1F, 0x01, 0x1F, 0x10, 0x1F, 0x00, 0x1F, 0x04},
{ 0x1F, 0x11, 0x11, 0x1F, 0x00, 0x00, 0x1F, 0x04}, { 0x00, 0x0A, 0x0E, 0x0A, 0x0E, 0x00, 0x1F, 0x04},
{ 0x00, 0x04, 0x04, 0x0A, 0x11, 0x00, 0x1F, 0x04}, { 0x0E, 0x11, 0x11, 0x0E, 0x00, 0x00, 0x1F, 0x04},
{ 0x00, 0x00, 0x1F, 0x0A, 0x11, 0x00, 0x1F, 0x04}, { 0x00, 0x04, 0x1F, 0x0A, 0x11, 0x00, 0x1F, 0x04},
{ 0x00, 0x1F, 0x01, 0x0F, 0x01, 0x00, 0x1F, 0x04}, { 0x1F, 0x10, 0x1F, 0x10, 0x1F, 0x00, 0x1F, 0x04},
{ 0x00, 0x1F, 0x0A, 0x0A, 0x1F, 0x00, 0x1F, 0x04}, { 0x04, 0x1F, 0x0E, 0x11, 0x0E, 0x00, 0x1F, 0x04},
// ㅠ 0x00, 0x1F, 0x0A
{ 0x00, 0x1F, 0x01, 0x01, 0x00, 0x00, 0x1F, 0x0A}, { 0x00, 0x10, 0x10, 0x1F, 0x00, 0x00, 0x1F, 0x0A},
{ 0x00, 0x1F, 0x10, 0x1F, 0x00, 0x00, 0x1F, 0x0A}, { 0x1F, 0x01, 0x1F, 0x10, 0x1F, 0x00, 0x1F, 0x0A},
{ 0x1F, 0x11, 0x11, 0x1F, 0x00, 0x00, 0x1F, 0x0A}, { 0x00, 0x0A, 0x0E, 0x0A, 0x0E, 0x00, 0x1F, 0x0A},
{ 0x00, 0x04, 0x04, 0x0A, 0x11, 0x00, 0x1F, 0x0A}, { 0x0E, 0x11, 0x11, 0x0E, 0x00, 0x00, 0x1F, 0x0A},
{ 0x00, 0x00, 0x1F, 0x0A, 0x11, 0x00, 0x1F, 0x0A}, { 0x00, 0x04, 0x1F, 0x0A, 0x11, 0x00, 0x1F, 0x0A},
{ 0x00, 0x1F, 0x01, 0x0F, 0x01, 0x00, 0x1F, 0x0A}, { 0x1F, 0x10, 0x1F, 0x10, 0x1F, 0x00, 0x1F, 0x0A},
{ 0x00, 0x1F, 0x0A, 0x0A, 0x1F, 0x00, 0x1F, 0x0A}, { 0x04, 0x1F, 0x0E, 0x11, 0x0E, 0x00, 0x1F, 0x0A},
// ㅡ 0x00, 0x1F, 0x00
{ 0x00, 0x1F, 0x01, 0x01, 0x00, 0x00, 0x1F, 0x00}, { 0x00, 0x10, 0x10, 0x1F, 0x00, 0x00, 0x1F, 0x00},
{ 0x00, 0x1F, 0x10, 0x1F, 0x00, 0x00, 0x1F, 0x00}, { 0x1F, 0x01, 0x1F, 0x10, 0x1F, 0x00, 0x1F, 0x00},
{ 0x1F, 0x11, 0x11, 0x1F, 0x00, 0x00, 0x1F, 0x00}, { 0x00, 0x0A, 0x0E, 0x0A, 0x0E, 0x00, 0x1F, 0x00},
{ 0x00, 0x04, 0x04, 0x0A, 0x11, 0x00, 0x1F, 0x00}, { 0x0E, 0x11, 0x11, 0x0E, 0x00, 0x00, 0x1F, 0x00},
{ 0x00, 0x00, 0x1F, 0x0A, 0x11, 0x00, 0x1F, 0x00}, { 0x00, 0x04, 0x1F, 0x0A, 0x11, 0x00, 0x1F, 0x00},
{ 0x00, 0x1F, 0x01, 0x0F, 0x01, 0x00, 0x1F, 0x00}, { 0x1F, 0x10, 0x1F, 0x10, 0x1F, 0x00, 0x1F, 0x00},
{ 0x00, 0x1F, 0x0A, 0x0A, 0x1F, 0x00, 0x1F, 0x00}, { 0x04, 0x1F, 0x0E, 0x11, 0x0E, 0x00, 0x1F, 0x00},
};
int vow = 0;
for (int i = 0; i < 70; i++)
{
memcpy(multi[i].value, word[i], sizeof(word[i]));
int consonant = (i+1) % 14;
if (consonant == 1){
vow++;
}
multi[i].key = consonant * 100 + vow;
}
}
// void createCustomChar(int location, unsigned char charmap[]) {
// location &= 0x7; // we only have 8 locations 0-7
// lcd_byte(0x40 | (location << 3), LCD_CMD);
// for (int i = 0; i < 8; i++) {
// lcd_byte(charmap[i], LCD_CHR);
// }
// }
void createCustomChar(int location, unsigned char charmap[]) {
location &= 0x3F; // location을 0-39로 제한합니다.
int cgram_location = location % 40; // 0-39 사이의 위치를 사용합니다.
lcd_byte(0x40 | (cgram_location << 3), LCD_CMD);
for (int i = 0; i < 8; i++) {
lcd_byte(charmap[i], LCD_CHR);
}
}
// float to string
void typeFloat(float myFloat) {
char buffer[20];
sprintf(buffer, "%4.2f", myFloat);
typeln(buffer);
}
// int to string
void typeInt(int i) {
char array1[20];
sprintf(array1, "%d", i);
typeln(array1);
}
// clr lcd go home loc 0x80
void ClrLcd(void) {
lcd_byte(0x01, LCD_CMD);
lcd_byte(0x02, LCD_CMD);
}
// go to location on LCD
void lcdLoc(int line) {
lcd_byte(line, LCD_CMD);
}
// out char to LCD at current position
void typeChar(char val) {
lcd_byte(val, LCD_CHR);
}
// this allows use of any size string
void typeln(const char *s) {
while ( *s ) lcd_byte(*(s++), LCD_CHR);
}
void lcd_byte(int bits, int mode) {
//Send byte to data pins
// bits = the data
// mode = 1 for data, 0 for command
int bits_high;
int bits_low;
// uses the two half byte writes to LCD
bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT ;
bits_low = mode | ((bits << 4) & 0xF0) | LCD_BACKLIGHT ;
// High bits
wiringPiI2CReadReg8(fd, bits_high);
lcd_toggle_enable(bits_high);
// Low bits
wiringPiI2CReadReg8(fd, bits_low);
lcd_toggle_enable(bits_low);
}
void lcd_toggle_enable(int bits) {
// Toggle enable pin on LCD display
delayMicroseconds(500);
wiringPiI2CReadReg8(fd, (bits | ENABLE));
delayMicroseconds(500);
wiringPiI2CReadReg8(fd, (bits & ~ENABLE));
delayMicroseconds(500);
}
void lcd_init() {
// Initialise display
lcd_byte(0x33, LCD_CMD); // Initialise
lcd_byte(0x32, LCD_CMD); // Initialise
lcd_byte(0x06, LCD_CMD); // Cursor move direction
lcd_byte(0x0C, LCD_CMD); // 0x0F On, Blink Off
lcd_byte(0x28, LCD_CMD); // Data length, number of lines, font size
lcd_byte(0x01, LCD_CMD); // Clear display
delayMicroseconds(500);
}