Statistics
| Branch: | Revision:

root / paintboard / code / main.c @ 351481ed

History | View | Annotate | Download (2.03 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

    
23
#define PAINT_DATA_LEN         10
24

    
25
uint8_t internal_index = 0;
26
uint8_t internal_data[PAINT_DATA_LEN] = {
27
  TRACKING_ID,
28
  SERIAL_NUMBER,
29
  0,
30
  0,
31
  0,
32
  0,
33
  0,
34
  0,
35
  0,
36
  0
37
};
38

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

    
79
void slave_tx(void)
80
{
81
  twi_transmit(&internal_data[internal_index], 1);
82
  internal_index++;
83
  if (internal_index >= PAINT_DATA_LEN)
84
    internal_index = 0;
85
}
86

    
87
int main()
88
{
89
  sei();
90
  twi_attachSlaveRxEvent(slave_rx);
91
  twi_attachSlaveTxEvent(slave_tx);
92
  twi_setAddress(TRACKING_ID);
93
  twi_init();
94
  motor_init();
95
  while (1)
96
  {
97
    /* TODO geiger counter and metal detector */
98
  }
99
  return 0;
100
}