Statistics
| Branch: | Revision:

scoutece / paintboard / code / main.c @ d9b50611

History | View | Annotate | Download (2.42 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
#include <util/delay.h>
8

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

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

    
27

    
28
#define PAINT_DATA_LEN         13
29

    
30
#define METAL_DETECT           PD5
31

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

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

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

    
97
int main()
98
{
99
  sei();
100
  twi_attachSlaveRxEvent(slave_rx);
101
  twi_attachSlaveTxEvent(slave_tx);
102
  twi_setAddress(TRACKING_ID);
103
  twi_init();
104
  motor_init();
105
  servo_init();
106
  while (1)
107
  {
108
    internal_data[PAINT_INPUT_1] = !(!(_BV(METAL_DETECT) & PIND));
109
    if (internal_data[PAINT_INPUT_1]) {
110
      set_servo1(-128);
111
    } else {
112
      set_servo1(127);
113
    }
114
    servo_pulse();
115
    /* TODO geiger counter */
116
    _delay_ms(20);
117
  }
118
  return 0;
119
}