Statistics
| Revision:

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

History | View | Annotate | Download (1.58 KB)

1
#include "Forklift.h"
2
extern "C"
3
{
4
#include "i2c.h"
5
#include <util/delay.h>
6
}
7

    
8
#define SCOUT_AVR_ADDR 0x01
9

    
10
#define FORKLIFT_ADDR 0x41
11

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

    
21
#define FORKLIFT_DATA_LEN          7
22

    
23
using namespace Forklift;
24

    
25
char volatile response;
26
char volatile received;
27

    
28
int mrecv(char data)
29
{
30
  response = data;
31
  received = 1;
32
}
33

    
34
void srecv(char) {}
35
char ssend() {}
36

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

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

    
64
void forklift_init()
65
{
66
  i2c_init(SCOUT_AVR_ADDR, mrecv, srecv, ssend);
67
}
68

    
69
int get_tracking_id()
70
{
71
  return read_addr(FORKLIFT_TRACKING_ID);
72
}
73

    
74
int get_serial_number()
75
{
76
  return read_addr(FORKLIFT_SERIAL_NUMBER);
77
}
78

    
79
int get_height()
80
{
81
  return read_addr(FORKLIFT_HEIGHT);
82
}
83

    
84
int get_height_setpoint()
85
{
86
  return read_addr(FORKLIFT_HEIGHT_SETPOINT);
87
}
88

    
89
int set_height_setpoint(char setpoint)
90
{
91
  return write_addr(FORKLIFT_HEIGHT_SETPOINT, setpoint);
92
}