Revision 2011
Some fixes to forklift demo, and added most recent forklift code to warehouse project.
Forklift.cpp | ||
---|---|---|
1 |
#define USE_ALT_TWI // an alternative to libdragonfly's i2c code, in twi.cpp |
|
2 |
|
|
1 | 3 |
#include "Forklift.h" |
2 |
#include "libdragonfly/i2c.h" |
|
4 |
extern "C" |
|
5 |
{ |
|
3 | 6 |
#include <util/delay.h> |
7 |
} |
|
4 | 8 |
|
9 |
#ifdef USE_ALT_TWI |
|
10 |
# include "twi.h" |
|
11 |
#else |
|
12 |
extern "C" |
|
13 |
{ |
|
14 |
# include "i2c.h" |
|
15 |
} |
|
16 |
#endif |
|
17 |
|
|
5 | 18 |
#define SCOUT_AVR_ADDR 0x01 |
6 | 19 |
|
7 | 20 |
#define FORKLIFT_ADDR 0x41 |
... | ... | |
17 | 30 |
|
18 | 31 |
#define FORKLIFT_DATA_LEN 7 |
19 | 32 |
|
20 |
using namespace Forklift; |
|
21 |
|
|
22 | 33 |
char volatile response; |
23 | 34 |
char volatile received; |
24 | 35 |
|
25 |
int mrecv(char data) |
|
36 |
static int mrecv(char data)
|
|
26 | 37 |
{ |
27 | 38 |
response = data; |
28 | 39 |
received = 1; |
40 |
return 0; |
|
29 | 41 |
} |
30 | 42 |
|
31 |
void srecv(char) {} |
|
32 |
char ssend() {}
|
|
43 |
static void srecv(char) {}
|
|
44 |
static char ssend() {return 0;}
|
|
33 | 45 |
|
34 |
int read_addr(char addr) |
|
46 |
static int read_addr(char addr)
|
|
35 | 47 |
{ |
48 |
#ifdef USE_ALT_TWI |
|
49 |
return FORKLIFT_ERROR; |
|
50 |
#else |
|
36 | 51 |
if (i2c_send(FORKLIFT_ADDR, &addr, 1)) |
37 | 52 |
return FORKLIFT_ERROR; |
38 | 53 |
received = 0; |
... | ... | |
45 | 60 |
return response; |
46 | 61 |
} |
47 | 62 |
return FORKLIFT_ERROR; |
63 |
#endif |
|
48 | 64 |
} |
49 | 65 |
|
50 |
int write_addr(char addr, char data) |
|
66 |
static int write_addr(char addr, char data)
|
|
51 | 67 |
{ |
52 | 68 |
char buf[2]; |
53 | 69 |
buf[0] = addr; |
54 | 70 |
buf[1] = data; |
71 |
#ifdef USE_ALT_TWI |
|
72 |
if (twi_writeTo(FORKLIFT_ADDR, (uint8_t*) buf, 2, 1)) |
|
73 |
return FORKLIFT_ERROR; |
|
74 |
#else |
|
55 | 75 |
if (i2c_send(FORKLIFT_ADDR, buf, 2)) |
56 | 76 |
return FORKLIFT_ERROR; |
57 |
else
|
|
58 |
return 0;
|
|
77 |
#endif
|
|
78 |
return 0; |
|
59 | 79 |
} |
60 | 80 |
|
61 |
void forklift_init() |
|
81 |
void Forklift::forklift_init()
|
|
62 | 82 |
{ |
83 |
#ifdef USE_ALT_TWI |
|
84 |
twi_init(); |
|
85 |
twi_setAddress(SCOUT_AVR_ADDR); |
|
86 |
#else |
|
63 | 87 |
i2c_init(SCOUT_AVR_ADDR, mrecv, srecv, ssend); |
88 |
#endif |
|
64 | 89 |
} |
65 | 90 |
|
66 |
int get_tracking_id() |
|
91 |
int Forklift::get_tracking_id()
|
|
67 | 92 |
{ |
68 | 93 |
return read_addr(FORKLIFT_TRACKING_ID); |
69 | 94 |
} |
70 | 95 |
|
71 |
int get_serial_number() |
|
96 |
int Forklift::get_serial_number()
|
|
72 | 97 |
{ |
73 | 98 |
return read_addr(FORKLIFT_SERIAL_NUMBER); |
74 | 99 |
} |
75 | 100 |
|
76 |
int get_height() |
|
101 |
int Forklift::get_height()
|
|
77 | 102 |
{ |
78 | 103 |
return read_addr(FORKLIFT_HEIGHT); |
79 | 104 |
} |
80 | 105 |
|
81 |
int get_height_setpoint() |
|
106 |
int Forklift::get_height_setpoint()
|
|
82 | 107 |
{ |
83 | 108 |
return read_addr(FORKLIFT_HEIGHT_SETPOINT); |
84 | 109 |
} |
85 | 110 |
|
86 |
int set_height_setpoint(char setpoint) |
|
111 |
int Forklift::set_height_setpoint(char setpoint)
|
|
87 | 112 |
{ |
88 | 113 |
return write_addr(FORKLIFT_HEIGHT_SETPOINT, setpoint); |
89 | 114 |
} |
Also available in: Unified diff