-
Notifications
You must be signed in to change notification settings - Fork 4
/
tbchat.c
195 lines (168 loc) · 4.45 KB
/
tbchat.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
#include <sysinit.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "basic/basic.h"
#include "basic/config.h"
#include "filesystem/ff.h"
#include "funk/nrf24l01p.h"
#include "funk/rftransfer.h"
#include "lcd/render.h"
#include "lcd/print.h"
#include "usetable.h"
/*Global Communication Config*/
uint8_t mac[5] = {1,2,3,5,5};
struct NRF_CFG config =
{
.channel= 81,
.txmac= "\x1\x2\x3\x5\x5",
.nrmacs=1,
.mac0= "\x1\x2\x3\x5\x5",
.maclen ="\x20",
};
unsigned char recvbuf[32];
unsigned char sendbuf[32];
struct Question {
char* text;
char* up;
char* down;
};
uint8_t recv_msg(unsigned char **msg);
void send_msg(unsigned char *msg);
int readTextFile(char * filename, char * data, int len);
void initQuestions(struct Question* const questions);
void showQuestion(const struct Question const * q, const uint8_t idx);
void printWrap(const char const * s, uint8_t len);
uint8_t ceil(const uint8_t n, const uint8_t d);
void ram(void)
{
lcdClear();
lcdPrintln("OK r0ket ready.");
lcdPrintln("Press any button.");
lcdRefresh();
uint8_t key = getInputWait();
getInputWaitRelease();
uint8_t nQuestions = 3;
struct Question q[nQuestions];
unsigned char answers[nQuestions];
memset(answers, nQuestions, 0); // size in bytes
initQuestions(q);
for (uint8_t i = 0; i<nQuestions; ++i) {
showQuestion(q,i);
key = getInputWait();
getInputWaitRelease();
if(key == BTN_UP) {
answers[i] = 1;
} else if(key == BTN_DOWN) {
answers[i] = 0;
}
}
unsigned char *msg;
while(key != BTN_ENTER) {
if(recv_msg(&msg)) {
lcdPrint("<-");
lcdPrintln((char*)msg);
}
key = getInput();
if(key == BTN_RIGHT) {
send_msg((unsigned char*)"right!");
lcdPrintln("->right");
} else if(key == BTN_LEFT) {
send_msg((unsigned char*)"left!");
lcdPrintln("->left");
} else if(key == BTN_UP) {
send_msg((unsigned char*)"up!");
lcdPrintln("->up");
} else if(key == BTN_DOWN) {
send_msg((unsigned char*)"down!");
lcdPrintln("->down");
}
lcdRefresh();
}
}
// a1 and a2 are answer arrays
uint8_t match(const unsigned char const * a1, const unsigned char const * a2, const uint8_t n)
{
uint8_t score = 0;
for (uint8_t i=0; i<n; ++i) {
if (a1[i]==a2[i])
score++;
}
return score;
}
// Copy from filesystem/util.c since the function is not exported
int readTextFile(char * filename, char * data, int len){
UINT readbytes;
readbytes=readFile(filename,data,len-1);
if(len>=0)
data[readbytes]=0;
return readbytes;
};
uint8_t ceil(const uint8_t n, const uint8_t d)
{
uint8_t res = n/d;
if (n%d==0)
return res;
return res + 1;
}
void printWrap(const char const * s, uint8_t len)
{
uint8_t slen = strlen(s);
for (uint8_t i=0; i < ceil(slen, len); ++i) {
char substr[len+1];
for (uint8_t j=0; j < len; ++j) {
uint8_t sidx = i*len+j;
if (sidx < slen)
substr[j] = s[sidx];
else
substr[j] = ' ';
}
substr[len] = 0;
lcdPrintln(substr);
}
}
uint8_t recv_msg(unsigned char **msg)
{
nrf_config_set(&config);
int n = nrf_rcv_pkt_time(10,32,recvbuf);
if(n != 32)
return 0;
*msg = recvbuf;
return 1;
}
void send_msg(unsigned char *msg)
{
memset(sendbuf, 0, 32);
for(int i=0; i<32 && msg[i] != 0; ++i) {
sendbuf[i] = msg[i];
}
nrf_config_set(&config);
nrf_snd_pkt_crc(32, sendbuf);
}
void initQuestions(struct Question* const questions)
{
questions[0].text="Do you like men?";
questionThank you very much for your feedback.s[0].up="Yes";
questions[0].down="No";
questions[1].text="Favourite Editor?";
questions[1].up="vi";
questions[1].down="emacs";
questions[2].text="Schoedingers Cat?";
questions[2].up="dead";
questions[2].down="alive";
// Long Hair
// > 3 Freunde
// > Grammar Nazi
}
void showQuestion(const struct Question const * q, const uint8_t idx)
{
lcdClear();
lcdPrintln("Q: ");
uint8_t lineLength = 12;
printWrap(q[idx].text, lineLength);
lcdPrint("UP: ");
printWrap(q[idx].up, lineLength);
lcdPrint("DOWN: ");
printWrap(q[idx].down, lineLength);
lcdRefresh();
}