-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenedettelli-nxt2wifi-test2.c
193 lines (169 loc) · 4.78 KB
/
benedettelli-nxt2wifi-test2.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
#pragma config(Sensor, S4, NXT2WIFI, sensorHighSpeed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/*
* $Id: benedettelli-nxt2wifi-test2.c 133 2013-03-10 15:15:38Z xander $
*/
/**
* benedettelli-nxt2wifi.h provides an API for Daniele's NXT2WiFi Sensor. This program
* demonstrates how to use that API. This program is opens a connection to a BOFH server
* and fetches an excuse and displays it on the screen. It is a demo for opening a TCP socket
* to a remote host.
*
* Changelog:
* - 0.1: Initial release
*
* Credits:
* - Big thanks to Daniele Benedettelli for providing me with the hardware necessary to write and test this.
*
* License: You may use this code as you wish, provided you give credit where it's due.
*
* THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER.
* Xander Soldaat (xander_at_botbench.com)
* 22 July 2012
* version 0.1
*/
#include "drivers/common.h"
#include "drivers/benedettelli-nxt2wifi.h"
long txbytes = 0;
long rxbytes = 0;
string IPaddress = "0.0.0.0";
string connStatus = "disconnected";
string dataStrings[4];
tHugeByteArray data;
int sizeOfReceivedData;
task updateScreen()
{
while(true)
{
nxtDisplayTextLine(0, "Stat: %s", connStatus);
nxtDisplayTextLine(1, "%s",IPaddress);
nxtDisplayTextLine(2, "-------------------");
nxtDisplayTextLine(3, "%s", dataStrings[0]);
nxtDisplayTextLine(4, "%s", dataStrings[1]);
nxtDisplayTextLine(5, "%s", dataStrings[2]);
nxtDisplayTextLine(6, "%s", dataStrings[3]);
nxtDisplayTextLine(7, "RX/TX: %d/%d", rxbytes, txbytes);
wait1Msec(100);
}
}
void processData()
{
// Used for going through the current line so we
// can print it
string line = "";
string singleword = "";
// What line are we current printing on?
int screenLine = 0;
dataStrings[0] = "";
dataStrings[1] = "";
dataStrings[2] = "";
dataStrings[3] = "";
for (int i = 0; i < sizeOfReceivedData; i++)
{
// It's a printable character
if (RS485rxbuffer[i] > 0x20)
{
// Append to the current word
strncat(singleword, &RS485rxbuffer[i], 1);
}
// we've reached a word or line boundary
else if ((RS485rxbuffer[i] == 0x20) || (RS485rxbuffer[i] == 0x0A))
{
// is the current word and the current line more than we can print?
if ((strlen(line) + strlen(singleword)) > 16)
{
// print out the current line
dataStrings[screenLine++] = line;
// current word becomes new line
memcpy(line, singleword, sizeof(singleword));
// blank out the current word
singleword = "";
}
else
{
// Append a space to the current line before sticking
// the new word to it
if (strlen(line) > 0)
strcat(line, " ");
strcat(line, singleword);
singleword = "";
}
// Are we at the end of the line and need to print it out regardless?
if (RS485rxbuffer[i] == 0x0A)
{
dataStrings[screenLine++] = line;
// Reset the current line
line = "";
}
}
}
}
task main ()
{
string BOFHserver = "192.168.0.100"; // Linux VM
int BOFHport = 6666;
string dataString;
getFriendlyName(dataString);
int avail = 0;
nNxtButtonTask = -2;
StartTask(updateScreen);
// initialise the port, etc
RS485initLib();
memset(RS485rxbuffer, 0, sizeof(RS485rxbuffer));
memset(RS485txbuffer, 0, sizeof(RS485txbuffer));
// Disconnect if already connected
N2WDisconnect();
N2WchillOut();
if (!N2WCustomExist())
{
StopTask(updateScreen);
wait1Msec(50);
eraseDisplay();
PlaySound(soundException);
nxtDisplayCenteredBigTextLine(1, "ERROR");
nxtDisplayTextLine(3, "No custom profile");
nxtDisplayTextLine(4, "configured!!");
while(true) EndTimeSlice();
}
N2WLoad();
wait1Msec(100);
N2WConnect(true);
connStatus = "connecting";
while (!N2WConnected())
wait1Msec(1000);
connStatus = "connected";
PlaySound(soundBeepBeep);
wait1Msec(3000);
N2WgetIP(IPaddress);
wait1Msec(1000);
while (true)
{
while (nNxtButtonPressed != kEnterButton) EndTimeSlice();
while (nNxtButtonPressed != kNoButton) EndTimeSlice();
if (N2WTCPOpenClient(1, BOFHserver, BOFHport)) {
data[0] = 0x0A;
N2WTCPWrite(1, data, 1);
txbytes++;
// How many bytes are available in the buffers?
avail = N2WTCPAvail(1);
if (avail > 0)
{
sizeOfReceivedData = avail;
rxbytes += avail;
N2WchillOut();
// read the current buffer
N2WTCPRead(1, avail);
processData();
for (int i = 0; i < avail; i++)
{
writeDebugStream("%c", RS485rxbuffer[i]);
}
writeDebugStreamLine("");
}
// Wait a bit
N2WchillOut();
// Disconnect
N2WTCPClose(1);
}
}
}