Statistics
| Revision:

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

History | View | Annotate | Download (1.7 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
static int mrecv(char data)
29
{
30
  response = data;
31
  received = 1;
32
  return 0;
33
}
34

    
35
static void srecv(char) {}
36
static char ssend() {return 0;}
37

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

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

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

    
70
int Forklift::get_tracking_id()
71
{
72
  return read_addr(FORKLIFT_TRACKING_ID);
73
}
74

    
75
int Forklift::get_serial_number()
76
{
77
  return read_addr(FORKLIFT_SERIAL_NUMBER);
78
}
79

    
80
int Forklift::get_height()
81
{
82
  return read_addr(FORKLIFT_HEIGHT);
83
}
84

    
85
int Forklift::get_height_setpoint()
86
{
87
  return read_addr(FORKLIFT_HEIGHT_SETPOINT);
88
}
89

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