-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtinyip.cpp
205 lines (173 loc) · 3.87 KB
/
tinyip.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
#include <kernel.h>
#include <t_syslog.h>
#include <t_stdlib.h>
#include "syssvc/serial.h"
#include "syssvc/syslog.h"
#include "kernel_cfg.h"
#include "syssvc/logtask.h"
#include "arduino_app.h"
#include "mbed.h"
#include "TextLCD.h"
#include "ethernet_api.h"
#include "ethernetext_api.h"
#include "SDFileSystem.h"
#include <time.h>
#include <stdlib.h>
#include "netconf.h"
#include "netlib.h"
#include "sntpclient.h"
#include "util.h"
#include "morse.h"
#include "ethernet.h"
#include "arp.h"
#include "ip.h"
#include "tcp.h"
#include "httpd.h"
#include "nameresolver.h"
#include "dhclient.h"
#include "tweet.h"
#include <wolfssl/ssl.h>
#include <wolfssl/wolfcrypt/logging.h>
#define MIN(x,y) ((x)<(y)?(x):(y))
#define TEXT_LEN 32 //16x2 LCD
static TextLCD lcd(D0, D1, D2, D3, D4, D5);
SDFileSystem sd(P8_5, P8_6, P8_3, P8_4, "sd");
InterruptIn user_button0(USER_BUTTON0);
/*
* Error check of service calls
*/
Inline void
svc_perror(const char *file, int_t line, const char *expr, ER ercd)
{
if (ercd < 0) {
t_perror(LOG_ERROR, file, line, expr, ercd);
}
}
#define SVC_PERROR(expr) svc_perror(__FILE__, __LINE__, #expr, (expr))
void user_button0_fall() {
iset_flg(BUTTON_FLG, PTN_FALL);
return;
}
void user_button0_rise() {
iset_flg(BUTTON_FLG, PTN_RISE);
return;
}
/*
* Main task
*/
void main_task(intptr_t exinf) {
/* configure syslog */
SVC_PERROR(syslog_msk_log(LOG_UPTO(LOG_INFO), LOG_UPTO(LOG_EMERG)));
syslog(LOG_NOTICE, "Sample program starts (exinf = %d).", (int_t) exinf);
//乱数のシード値を未使用のアナログ入力から取得
AnalogIn ain(A0);
srand(ain.read_u16());
lcd.cls(); lcd.printf("Please wait...");
//Ethernetコントローラ割り込みの設定
ethernet_initialize();
while(!ethernet_link()){
dly_tsk(100);
lcd.cls(); lcd.printf("No link...");
}
lcd.cls(); lcd.printf("Please wait...");
LOG("macaddress: %s", macaddr2str(MACADDR));
sta_cyc(CYCHDR1);
register_arptable(IPADDR_TO_UINT32(IPBROADCAST), ETHERBROADCAST, true);
start_ip();
#ifdef USE_DHCP
FLGPTN ptn;
start_dhclient();
if(twai_flg(DHCLIENT_FLG, DHCLIENT_TRANSION_BOUND, TWF_ORW, &ptn, 30000)==E_TMOUT){ //BOUND状態に遷移するまで待つ
lcd.cls(); lcd.printf("DHCP: failed");
}else{
lcd.cls(); lcd.printf("DHCP: OK");
}
#endif //USE_DHCP
#ifdef USE_SNTP
start_sntpclient();
twai_sem(SNTP_SEM, 5000);
#endif //USE_SNTP
start_tcp();
start_httpd();
user_button0.fall(user_button0_fall);
user_button0.rise(user_button0_rise);
start_morse();
act_tsk(USER_TASK);
}
static void index2pos(int index, int *col, int *row){
*col=index%lcd.columns();
*row=index/lcd.columns();
}
static int pos2index(int col, int row){
return col+row*lcd.columns();
}
void user_task(intptr_t exinf){
LOG("usertask start");
lcd.cls();
char text[TEXT_LEN+1];
char utf8text[TEXT_LEN*3+1];
int index=0; text[index]='\0';
int col,row;
char c;
while(true){
switch(c=morse_getc()){
case CHAR_SEND:
//送信
{
mcled_change(COLOR_LIGHTBLUE);
char *w=utf8text;
for(char *r=text; *r!='\0'; r++){
if(is_halfkana(*r)){
memcpy(w, halfkana_sjis_to_utf8(*r), 3);
w+=3;
}else{
*w=*r; w++;
}
}
*w='\0';
if(tweet(utf8text)==0)
mcled_change(COLOR_OFF);
else
mcled_change(COLOR_RED);
}
case CHAR_CLEAR:
//クリア
index=0;
text[0]='\0';
lcd.cls();
break;
case '\b':
while(index>0){
index--;
if(text[index]!=' ')
break;
}
text[index]='\0';
index2pos(index, &col, &row);
lcd.locate(col, row);
lcd.putc(' ');
lcd.locate(col, row);
break;
default:
if(index<TEXT_LEN){
text[index]=c;
lcd.putc(c);
text[++index]='\0';
}
break;
}
}
}
/*
* Cyclic handler
*/
void cyclic_handler(intptr_t exinf)
{
/* add your code here */
static bool led_state = false;
if(led_state)
redled_off();
else
redled_on();
led_state = !led_state;
}