1 |
hedin |
91 |
#define THIS_IS_STACK_APPLICATION
|
2 |
|
|
|
3 |
|
|
#include "stacktsk.h"
|
4 |
|
|
#include "tick.h"
|
5 |
|
|
#include "delay.h"
|
6 |
hedin |
106 |
#include "network.h"
|
7 |
hedin |
109 |
#include "barcode.h"
|
8 |
hedin |
111 |
#include "keyboard.h"
|
9 |
|
|
#include "MyI2C.h"
|
10 |
|
|
#include "xlcd.h"
|
11 |
hedin |
105 |
#include <string.h>
|
12 |
|
|
|
13 |
hedin |
91 |
/*
|
14 |
|
|
* This is used by other stack elements.
|
15 |
|
|
* Main application must define this and initialize it with
|
16 |
|
|
* proper values.
|
17 |
|
|
*/
|
18 |
|
|
APP_CONFIG AppConfig;
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
BYTE myDHCPBindCount = 0;
|
22 |
|
|
#if defined(STACK_USE_DHCP)
|
23 |
|
|
extern BYTE DHCPBindCount;
|
24 |
|
|
#else
|
25 |
|
|
/*
|
26 |
|
|
* If DHCP is not enabled, force DHCP update.
|
27 |
|
|
*/
|
28 |
|
|
BYTE DHCPBindCount = 1;
|
29 |
|
|
#endif
|
30 |
|
|
|
31 |
|
|
/*
|
32 |
|
|
* Set configuration fuses for HITECH compiler.
|
33 |
|
|
* For MCC18 compiler, separately linked config.asm file
|
34 |
|
|
* will set the correct fuses.
|
35 |
|
|
*/
|
36 |
|
|
#if defined(HITECH_C18)
|
37 |
|
|
__CONFIG(1, UNPROTECT & HS);
|
38 |
|
|
__CONFIG(2, PWRTEN & BORDIS & WDTDIS);
|
39 |
|
|
#endif
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
/*
|
43 |
|
|
* Private helper functions.
|
44 |
|
|
* These may or may not be present in all applications.
|
45 |
|
|
*/
|
46 |
|
|
static void InitAppConfig(void);
|
47 |
|
|
static void InitializeBoard(void);
|
48 |
|
|
static void SetConfig(void);
|
49 |
|
|
|
50 |
hedin |
111 |
// Skal fjernes
|
51 |
|
|
#define LCD_IC 0x74
|
52 |
|
|
void mylcd_write_byte(unsigned char data)
|
53 |
|
|
{
|
54 |
|
|
StartI2C(LCD_IC);
|
55 |
|
|
WriteI2CByte( data );
|
56 |
|
|
StopI2C();
|
57 |
|
|
Delay10us(1); //I2C spec's siger min 4.7 uSec mellem stop og næste start
|
58 |
|
|
}
|
59 |
hedin |
96 |
|
60 |
|
|
|
61 |
hedin |
111 |
|
62 |
hedin |
91 |
void main(void)
|
63 |
|
|
{
|
64 |
|
|
// Tick to blink SYSTEM led.
|
65 |
|
|
static TICK t = 0;
|
66 |
hedin |
111 |
char i;
|
67 |
|
|
unsigned char numbers[20];
|
68 |
|
|
unsigned long amount;
|
69 |
|
|
unsigned char lcd_buffer[21];
|
70 |
|
|
short number_count = 0;
|
71 |
|
|
short produkt_amount = 0;
|
72 |
|
|
static bit hello = 0;
|
73 |
|
|
short varetal = 1;
|
74 |
hedin |
91 |
|
75 |
hedin |
93 |
TICK tick;
|
76 |
|
|
TICK lastTick;
|
77 |
|
|
TICK diffTicks;
|
78 |
hedin |
91 |
|
79 |
|
|
/*
|
80 |
|
|
* Initialize any application specific hardware.
|
81 |
|
|
*/
|
82 |
|
|
InitializeBoard();
|
83 |
|
|
|
84 |
|
|
/*
|
85 |
|
|
* Initialize all stack related components.
|
86 |
|
|
* Following steps must be performed for all applications using
|
87 |
|
|
* PICmicro TCP/IP Stack.
|
88 |
|
|
*/
|
89 |
|
|
TickInit();
|
90 |
|
|
|
91 |
|
|
/*
|
92 |
|
|
* Initialize Stack and application related NV variables.
|
93 |
|
|
*/
|
94 |
|
|
InitAppConfig();
|
95 |
|
|
|
96 |
|
|
StackInit();
|
97 |
hedin |
96 |
|
98 |
|
|
network_init();
|
99 |
hedin |
111 |
barcode_init();
|
100 |
|
|
InitI2C();
|
101 |
|
|
XLCDInit();
|
102 |
|
|
|
103 |
hedin |
91 |
|
104 |
|
|
|
105 |
|
|
/*
|
106 |
|
|
* Once all items are initialized, go into infinite loop and let
|
107 |
|
|
* stack items execute their tasks.
|
108 |
|
|
* If application needs to perform its own task, it should be
|
109 |
|
|
* done at the end of while loop.
|
110 |
|
|
* Note that this is a "co-operative mult-tasking" mechanism
|
111 |
|
|
* where every task performs its tasks (whether all in one shot
|
112 |
|
|
* or part of it) and returns so that other tasks can do their
|
113 |
|
|
* job.
|
114 |
|
|
* If a task needs very long time to do its job, it must broken
|
115 |
|
|
* down into smaller pieces so that other tasks can have CPU time.
|
116 |
|
|
*/
|
117 |
|
|
|
118 |
|
|
|
119 |
hedin |
108 |
// Defines the IP add. on the server machine (rnode is defined in network.h)
|
120 |
hedin |
91 |
rnode.IPAddr.v[0] = 192;
|
121 |
|
|
rnode.IPAddr.v[1] = 168;
|
122 |
|
|
rnode.IPAddr.v[2] = 1;
|
123 |
|
|
rnode.IPAddr.v[3] = 20;
|
124 |
|
|
|
125 |
|
|
while(1)
|
126 |
|
|
{
|
127 |
hedin |
111 |
char keyboard_entered;
|
128 |
|
|
char network_error;
|
129 |
|
|
|
130 |
|
|
|
131 |
hedin |
91 |
/*
|
132 |
|
|
* Blink SYSTEM LED every second.
|
133 |
|
|
*/
|
134 |
|
|
if ( TickGetDiff(TickGet(), t) >= TICK_SECOND/2 )
|
135 |
|
|
{
|
136 |
|
|
t = TickGet();
|
137 |
|
|
LATA4 ^= 1;
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
/*
|
141 |
|
|
* This task performs normal stack task including checking
|
142 |
|
|
* for incoming packet, type of packet and calling
|
143 |
|
|
* appropriate stack entity to process it.
|
144 |
|
|
*/
|
145 |
|
|
StackTask();
|
146 |
|
|
|
147 |
|
|
/*
|
148 |
|
|
* For DHCP information, display how many times we have renewed the IP
|
149 |
|
|
* configuration since last reset.
|
150 |
|
|
*/
|
151 |
hedin |
111 |
|
152 |
|
|
//mylcd_write_byte(0b10100101);
|
153 |
|
|
|
154 |
|
|
if( (barcode_is_ready() || keyboard_entered) && !hello){
|
155 |
|
|
network_send_hello(0x01);
|
156 |
|
|
hello = 1;
|
157 |
|
|
}
|
158 |
|
|
if (barcode_is_ready())
|
159 |
|
|
{
|
160 |
|
|
for (i=0;i< barcode_get_length(); i++)
|
161 |
|
|
{
|
162 |
|
|
global_network_char[i] = global_barcode_buffer[i];
|
163 |
|
|
global_network_charlen = barcode_get_length();
|
164 |
|
|
}
|
165 |
|
|
barcode_reset();
|
166 |
|
|
network_send_scan_frame( varetal, global_network_charlen );
|
167 |
|
|
varetal = 1;
|
168 |
|
|
/*
|
169 |
|
|
XLCDClear();
|
170 |
|
|
XLCDGoto(1,0);
|
171 |
|
|
for (i=0;i< barcode_get_length(); i++)
|
172 |
|
|
{
|
173 |
|
|
XLCDPut(global_barcode_buffer[i]);
|
174 |
|
|
}
|
175 |
|
|
*/
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
keyboard_entered = keyboard_read_safe();
|
179 |
|
|
if( keyboard_entered )
|
180 |
|
|
{
|
181 |
|
|
switch( keyboard_entered )
|
182 |
|
|
{
|
183 |
|
|
case 'A':
|
184 |
|
|
network_send_calc_total();
|
185 |
|
|
XLCDClear();
|
186 |
|
|
XLCDGoto(0,0);
|
187 |
|
|
for( i=0; i<global_network_charlen; i++ )
|
188 |
|
|
{
|
189 |
|
|
XLCDPut(global_network_char[i]);
|
190 |
|
|
}
|
191 |
|
|
break;
|
192 |
|
|
|
193 |
|
|
case 'B':
|
194 |
hedin |
113 |
numbers[number_count] = 0; //nulterminering inden brug af atol
|
195 |
|
|
amount = atol(numbers);
|
196 |
hedin |
111 |
number_count = 0;
|
197 |
|
|
|
198 |
|
|
network_send_cash_payed(amount);
|
199 |
|
|
network_send_goodbye();
|
200 |
|
|
hello = 0;
|
201 |
|
|
|
202 |
hedin |
113 |
sprintf(lcd_buffer,"%5d", amount);
|
203 |
hedin |
111 |
XLCDGoto(0,0);
|
204 |
|
|
XLCDPutString(lcd_buffer);
|
205 |
|
|
break;
|
206 |
|
|
|
207 |
|
|
case 'C':
|
208 |
|
|
network_send_cancel_last();
|
209 |
|
|
break;
|
210 |
|
|
|
211 |
|
|
case 'D':
|
212 |
hedin |
113 |
network_send_cancel_all();
|
213 |
|
|
network_send_goodbye();
|
214 |
|
|
hello = 0;
|
215 |
hedin |
111 |
break;
|
216 |
|
|
|
217 |
|
|
case 'E':
|
218 |
|
|
for( i=0; i<number_count;i++ )
|
219 |
|
|
{
|
220 |
|
|
global_network_char[i] = numbers[i];
|
221 |
|
|
}
|
222 |
|
|
network_send_scan_frame( varetal, number_count );
|
223 |
|
|
varetal = 1;
|
224 |
|
|
number_count = 0;
|
225 |
|
|
break;
|
226 |
|
|
|
227 |
|
|
case 'F':
|
228 |
hedin |
113 |
numbers[number_count] = 0; //nulterminering inden brug af atol
|
229 |
|
|
varetal = atol(numbers);
|
230 |
hedin |
111 |
number_count = 0;
|
231 |
|
|
break;
|
232 |
|
|
|
233 |
|
|
default:
|
234 |
|
|
numbers[number_count] = keyboard_entered;
|
235 |
|
|
|
236 |
|
|
//numbers; // got to catch 0-9; 20 byte array.
|
237 |
|
|
number_count++; // amount of used bytes in numbers.
|
238 |
|
|
}
|
239 |
|
|
|
240 |
|
|
}
|
241 |
|
|
/*
|
242 |
|
|
network_send_hello( 0x01 );
|
243 |
|
|
|
244 |
|
|
strncpy(global_network_char, "12345678", 8);
|
245 |
hedin |
105 |
network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8
|
246 |
|
|
|
247 |
|
|
strncpy(global_network_char, "87654444", 8);
|
248 |
|
|
network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8
|
249 |
hedin |
111 |
|
250 |
hedin |
105 |
network_send_calc_total();
|
251 |
|
|
|
252 |
|
|
network_send_cash_payed(50000);
|
253 |
|
|
|
254 |
|
|
network_send_cancel_last();
|
255 |
|
|
|
256 |
|
|
network_send_cancel_all();
|
257 |
|
|
|
258 |
|
|
network_send_goodbye();
|
259 |
hedin |
111 |
*/
|
260 |
hedin |
91 |
}
|
261 |
|
|
}
|
262 |
|
|
|
263 |
hedin |
102 |
void interrupt HighISR(void)
|
264 |
hedin |
91 |
{
|
265 |
hedin |
109 |
if (RCIF == 1)
|
266 |
|
|
barcode_interrupt();
|
267 |
|
|
else
|
268 |
|
|
TickUpdate();
|
269 |
hedin |
91 |
}
|
270 |
|
|
|
271 |
|
|
|
272 |
hedin |
106 |
|
273 |
hedin |
91 |
/*********************************************************************
|
274 |
|
|
* Function: void InitializeBoard(void)
|
275 |
|
|
*
|
276 |
|
|
* PreCondition: None
|
277 |
|
|
*
|
278 |
|
|
* Input: None
|
279 |
|
|
*
|
280 |
|
|
* Output: None
|
281 |
|
|
*
|
282 |
|
|
* Side Effects: None
|
283 |
|
|
*
|
284 |
|
|
* Overview: Initialize board specific hardware.
|
285 |
|
|
*
|
286 |
|
|
* Note: None
|
287 |
|
|
********************************************************************/
|
288 |
|
|
static void InitializeBoard(void)
|
289 |
|
|
{
|
290 |
|
|
/*
|
291 |
|
|
* Setup for PORTA.RA0 as analog input while rests
|
292 |
|
|
* as digital i/o lines.
|
293 |
|
|
*/
|
294 |
|
|
ADCON1 = 0b10001110; // RA0 as analog input, Right justified
|
295 |
|
|
TRISA = 0x03;
|
296 |
|
|
|
297 |
|
|
/*
|
298 |
|
|
* LCD is enabled using RA5.
|
299 |
|
|
*/
|
300 |
hedin |
96 |
// PORTA_RA5 = 0; // Disable LCD.
|
301 |
hedin |
91 |
|
302 |
|
|
/*
|
303 |
|
|
* Turn off the LED's.
|
304 |
|
|
*/
|
305 |
|
|
LATA2 = 1;
|
306 |
|
|
LATA3 = 1;
|
307 |
|
|
|
308 |
|
|
/*
|
309 |
|
|
* External data EEPROM needs pull-ups, so enable internal
|
310 |
|
|
* pull-ups.
|
311 |
|
|
*/
|
312 |
|
|
INTCON2_RBPU = 0;
|
313 |
|
|
|
314 |
|
|
T0CON = 0;
|
315 |
|
|
INTCON_GIEH = 1;
|
316 |
|
|
INTCON_GIEL = 1;
|
317 |
|
|
|
318 |
|
|
}
|
319 |
|
|
|
320 |
|
|
/*********************************************************************
|
321 |
|
|
* Function: void InitAppConfig(void)
|
322 |
|
|
*
|
323 |
|
|
* PreCondition: MPFSInit() is already called.
|
324 |
|
|
*
|
325 |
|
|
* Input: None
|
326 |
|
|
*
|
327 |
|
|
* Output: Write/Read non-volatile config variables.
|
328 |
|
|
*
|
329 |
|
|
* Side Effects: None
|
330 |
|
|
*
|
331 |
|
|
* Overview: None
|
332 |
|
|
*
|
333 |
|
|
* Note: None
|
334 |
|
|
********************************************************************/
|
335 |
|
|
static void InitAppConfig(void)
|
336 |
|
|
{
|
337 |
|
|
/*
|
338 |
|
|
* Load default configuration into RAM.
|
339 |
|
|
*/
|
340 |
|
|
AppConfig.MyIPAddr.v[0] = MY_DEFAULT_IP_ADDR_BYTE1;
|
341 |
|
|
AppConfig.MyIPAddr.v[1] = MY_DEFAULT_IP_ADDR_BYTE2;
|
342 |
|
|
AppConfig.MyIPAddr.v[2] = MY_DEFAULT_IP_ADDR_BYTE3;
|
343 |
|
|
AppConfig.MyIPAddr.v[3] = MY_DEFAULT_IP_ADDR_BYTE4;
|
344 |
|
|
|
345 |
|
|
AppConfig.MyMask.v[0] = MY_DEFAULT_MASK_BYTE1;
|
346 |
|
|
AppConfig.MyMask.v[1] = MY_DEFAULT_MASK_BYTE2;
|
347 |
|
|
AppConfig.MyMask.v[2] = MY_DEFAULT_MASK_BYTE3;
|
348 |
|
|
AppConfig.MyMask.v[3] = MY_DEFAULT_MASK_BYTE4;
|
349 |
|
|
|
350 |
|
|
AppConfig.MyGateway.v[0] = MY_DEFAULT_GATE_BYTE1;
|
351 |
|
|
AppConfig.MyGateway.v[1] = MY_DEFAULT_GATE_BYTE2;
|
352 |
|
|
AppConfig.MyGateway.v[2] = MY_DEFAULT_GATE_BYTE3;
|
353 |
|
|
AppConfig.MyGateway.v[3] = MY_DEFAULT_GATE_BYTE4;
|
354 |
|
|
|
355 |
|
|
AppConfig.MyMACAddr.v[0] = MY_DEFAULT_MAC_BYTE1;
|
356 |
|
|
AppConfig.MyMACAddr.v[1] = MY_DEFAULT_MAC_BYTE2;
|
357 |
|
|
AppConfig.MyMACAddr.v[2] = MY_DEFAULT_MAC_BYTE3;
|
358 |
|
|
AppConfig.MyMACAddr.v[3] = MY_DEFAULT_MAC_BYTE4;
|
359 |
|
|
AppConfig.MyMACAddr.v[4] = MY_DEFAULT_MAC_BYTE5;
|
360 |
|
|
AppConfig.MyMACAddr.v[5] = MY_DEFAULT_MAC_BYTE6;
|
361 |
|
|
|
362 |
|
|
#if defined(STACK_USE_DHCP)
|
363 |
|
|
AppConfig.Flags.bIsDHCPEnabled = TRUE;
|
364 |
|
|
#else
|
365 |
|
|
AppConfig.Flags.bIsDHCPEnabled = FALSE;
|
366 |
|
|
#endif
|
367 |
|
|
}
|
368 |
|
|
|
369 |
|
|
BOOL StringToIPAddress(char *str, IP_ADDR *buffer)
|
370 |
|
|
{
|
371 |
|
|
BYTE v;
|
372 |
|
|
char *temp;
|
373 |
|
|
BYTE byteIndex;
|
374 |
|
|
|
375 |
|
|
temp = str;
|
376 |
|
|
byteIndex = 0;
|
377 |
|
|
|
378 |
|
|
while( v = *str )
|
379 |
|
|
{
|
380 |
|
|
if ( v == '.' )
|
381 |
|
|
{
|
382 |
|
|
*str++ = '\0';
|
383 |
|
|
buffer->v[byteIndex++] = atoi(temp);
|
384 |
|
|
temp = str;
|
385 |
|
|
}
|
386 |
|
|
else if ( v < '0' || v > '9' )
|
387 |
|
|
return FALSE;
|
388 |
|
|
|
389 |
|
|
str++;
|
390 |
|
|
}
|
391 |
|
|
|
392 |
|
|
buffer->v[byteIndex] = atoi(temp);
|
393 |
|
|
|
394 |
|
|
return (byteIndex == 3);
|
395 |
|
|
}
|
396 |
|
|
|
397 |
hedin |
111 |
void XLCDDelay15ms(void)
|
398 |
|
|
{
|
399 |
|
|
DelayMs(15);
|
400 |
|
|
}
|
401 |
|
|
void XLCDDelay4ms(void)
|
402 |
|
|
{
|
403 |
|
|
DelayMs(4);
|
404 |
|
|
}
|
405 |
|
|
|
406 |
|
|
void XLCDDelay100us(void)
|
407 |
|
|
{
|
408 |
|
|
INTCON_GIEH = 0;
|
409 |
|
|
Delay10us(1);
|
410 |
|
|
INTCON_GIEH = 1;
|
411 |
|
|
}
|