Statistics
| Branch: | Revision:

root / paintboard / code / main.c @ 1af81271

History | View | Annotate | Download (2.23 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
};
44

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

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

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