Statistics
| Revision:

root / trunk / code / projects / forklift_demo / Forklift.cpp @ 2005

History | View | Annotate | Download (1.58 KB)

1
#include "Forklift.h"
2
#include "libdragonfly/i2c.h"
3
#include <util/delay.h>
4

    
5
#define SCOUT_AVR_ADDR 0x01
6

    
7
#define FORKLIFT_ADDR 0x41
8

    
9
// indicies for forklift internal data
10
#define FORKLIFT_TRACKING_ID       0
11
#define FORKLIFT_SERIAL_NUMBER     1
12
#define FORKLIFT_HEIGHT            2
13
#define FORKLIFT_HEIGHT_SETPOINT   3 // r/w
14
#define FORKLIFT_LINE_POS          4
15
#define FORKLIFT_LINE_THRESH_HIGH  5 // r/w
16
#define FORKLIFT_LINE_THRESH_LOW   6 // r/w
17

    
18
#define FORKLIFT_DATA_LEN          7
19

    
20
using namespace Forklift;
21

    
22
char volatile response;
23
char volatile received;
24

    
25
int mrecv(char data)
26
{
27
  response = data;
28
  received = 1;
29
}
30

    
31
void srecv(char) {}
32
char ssend() {}
33

    
34
int read_addr(char addr)
35
{
36
  if (i2c_send(FORKLIFT_ADDR, &addr, 1))
37
    return FORKLIFT_ERROR;
38
  received = 0;
39
  if (i2c_request(FORKLIFT_ADDR))
40
    return FORKLIFT_ERROR;
41
  for (int i = 0; i < 500; i++) // waits up to ~50 ms
42
  {
43
    _delay_ms(.1);
44
    if (received)
45
      return response;
46
  }
47
  return FORKLIFT_ERROR;
48
}
49

    
50
int write_addr(char addr, char data)
51
{
52
  char buf[2];
53
  buf[0] = addr;
54
  buf[1] = data;
55
  if (i2c_send(FORKLIFT_ADDR, buf, 2))
56
    return FORKLIFT_ERROR;
57
  else
58
    return 0;
59
}
60

    
61
void forklift_init()
62
{
63
  i2c_init(SCOUT_AVR_ADDR, mrecv, srecv, ssend);
64
}
65

    
66
int get_tracking_id()
67
{
68
  return read_addr(FORKLIFT_TRACKING_ID);
69
}
70

    
71
int get_serial_number()
72
{
73
  return read_addr(FORKLIFT_SERIAL_NUMBER);
74
}
75

    
76
int get_height()
77
{
78
  return read_addr(FORKLIFT_HEIGHT);
79
}
80

    
81
int get_height_setpoint()
82
{
83
  return read_addr(FORKLIFT_HEIGHT_SETPOINT);
84
}
85

    
86
int set_height_setpoint(char setpoint)
87
{
88
  return write_addr(FORKLIFT_HEIGHT_SETPOINT, setpoint);
89
}