Statistics
| Branch: | Revision:

root / paintboard / code / main.c @ c874b753

History | View | Annotate | Download (2.26 KB)

1
#include "twi.h"
2
#include "motor.h"
3
#include "servo.h"
4
#include "sol.h"
5
#include <avr/io.h>
6
#include <avr/interrupt.h>
7

    
8
#define TRACKING_ID 0x43
9
#define SERIAL_NUMBER 0x12
10

    
11
// indicies for paintboard internal data
12
#define PAINT_TRACKING_ID       0 // ro
13
#define PAINT_SERIAL_NUMBER     1 // ro
14
#define PAINT_MOTOR_A           2 // r/w
15
#define PAINT_MOTOR_B           3 // r/w
16
#define PAINT_SERVO_A           4 // r/w
17
#define PAINT_SERVO_B           5 // r/w
18
#define PAINT_12V_1             6 // r/w
19
#define PAINT_12V_2             7 // r/w
20
#define PAINT_12V_3             8 // r/w
21
#define PAINT_12V_4             9 // r/w
22
#define PAINT_INPUT_1           10 //ro
23
#define PAINT_INPUT_2           11 //ro
24
#define PAINT_INPUT_3           12 //ro
25

    
26

    
27
#define PAINT_DATA_LEN         13
28

    
29
#define METAL_DETECT           PD4
30

    
31
uint8_t internal_index = 0;
32
uint8_t internal_data[PAINT_DATA_LEN] = {
33
  TRACKING_ID,
34
  SERIAL_NUMBER,
35
  0,
36
  0,
37
  0,
38
  0,
39
  0,
40
  0,
41
  0,
42
  0,
43
  0,
44
  0,
45
  0
46
};
47

    
48
void slave_rx(uint8_t* data, int len)
49
{
50
  uint8_t value;
51
  if (len > 0 && data[0] < PAINT_DATA_LEN)
52
  {
53
    internal_index = data[0];
54
    if (len > 1 && internal_index > 1)
55
    {
56
      value = data[1];
57
      internal_data[internal_index] = value;
58
      switch (internal_index) {
59
        case PAINT_MOTOR_A:
60
          set_motor1(value);
61
          break;
62
        case PAINT_MOTOR_B:
63
          set_motor2(value);
64
          break;
65
        case PAINT_SERVO_A:
66
          set_servo1(value);
67
          break;
68
        case PAINT_SERVO_B:
69
          set_servo2(value);
70
          break;
71
        case PAINT_12V_1:
72
          set_sol1(value);
73
          break;
74
        case PAINT_12V_2:
75
          set_sol2(value);
76
          break;
77
        case PAINT_12V_3:
78
          set_sol3(value);
79
          break;
80
        case PAINT_12V_4:
81
          set_sol4(value);
82
          break;
83
      }
84
    }
85
  }
86
}
87

    
88
void slave_tx(void)
89
{
90
  twi_transmit(&internal_data[internal_index], 1);
91
  internal_index++;
92
  if (internal_index >= PAINT_DATA_LEN)
93
    internal_index = 0;
94
}
95

    
96
int main()
97
{
98
  sei();
99
  twi_attachSlaveRxEvent(slave_rx);
100
  twi_attachSlaveTxEvent(slave_tx);
101
  twi_setAddress(TRACKING_ID);
102
  twi_init();
103
  motor_init();
104
  servo_init();
105
  while (1)
106
  {
107
    internal_data[PAINT_INPUT_1] = !(!(_BV(METAL_DETECT) & PIND));
108
    /* TODO geiger counter */
109
  }
110
  return 0;
111
}