Project

General

Profile

Revision 12ea39cc

ID12ea39cc64a8ebc9b7f3c176488a210d80684453
Parent 3811693d
Child e53aa5c5

Added by Thomas Mullins over 11 years ago

Added actual control of tool

View differences:

toolbox/main.c
1 1
#include <stdint.h>
2
#include <string.h>
3
#include <avr/io.h>
2 4
#include <util/delay.h>
3 5
#include "mb.h"
4 6
#include "mbport.h"
5 7
#include "tooltron_mb.h"
6 8
#include "rfid.h"
7 9

  
10
enum toolstate_t {
11
  TS_INIT,
12
  TS_OFF,
13
  TS_WAIT_ACCESS,
14
  TS_REQ_DIS,
15
  TS_MISSING_ID,
16
  TS_ON
17
};
18

  
19
static enum toolstate_t toolstate = TS_INIT;
8 20
static char coils;
21
static char current_user[RFID_SERNO_SIZE];
9 22

  
10 23
static inline void set_coil(char coil, char bit) {
11 24
  coils |= (bit << coil);
12 25
}
26
static inline char get_coil(char coil) {
27
  return (coils >> coil) & 1;
28
}
29

  
30
static inline void tool_init() {DDRA |= _BV(DDA1);}
31
static inline void tool_enable() {PORTA |= _BV(PA1);}
32
static inline void tool_disable() {PORTA &= _BV(PA1);}
33

  
34
static void tool_main() {
35

  
36
  switch (toolstate) {
37

  
38
    case TS_INIT:
39
      if (get_coil(MB_COIL_INIT)) {
40
        set_coil(MB_COIL_NEW, 0);
41
        set_coil(MB_COIL_EN, 0);
42
        set_coil(MB_COIL_REQ_DIS, 0);
43
        toolstate = TS_OFF;
44
      }
45
      break;
46

  
47
    case TS_OFF:
48
      if (rfid_nonzero()) {
49
        rfid_get_serno(current_user);
50
        set_coil(MB_COIL_NEW, 1);
51
        toolstate = TS_WAIT_ACCESS;
52
      }
53
      break;
54

  
55
    case TS_WAIT_ACCESS:
56
      if (get_coil(MB_COIL_EN)) {
57
        tool_enable();
58
        toolstate = TS_ON;
59
      }
60
      break;
61

  
62
    case TS_REQ_DIS:
63
      // TODO blink yellow for 10 seconds or something
64
      set_coil(MB_COIL_EN, 0);
65
      tool_disable();
66
      toolstate = TS_OFF;
67
      break;
68

  
69
    case TS_MISSING_ID:
70
      if (rfid_check_serno(current_user)) {
71
        toolstate = TS_ON;
72
      } else {
73
        // TODO blink yellow for 10 seconds or something
74
        set_coil(MB_COIL_EN, 0);
75
        tool_disable();
76
        toolstate = TS_OFF;
77
      }
78
      break;
79

  
80
    case TS_ON:
81
      if (!get_coil(MB_COIL_EN)) {
82
        tool_disable();
83
        toolstate = TS_OFF;
84
      }
85
      if (get_coil(MB_COIL_REQ_DIS)) {
86
        toolstate = TS_REQ_DIS;
87
      }
88
      if (!rfid_check_serno(current_user)) {
89
        toolstate = TS_MISSING_ID;
90
      }
91
      break;
92

  
93
  }
94

  
95
}
13 96

  
14 97
eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils,
15 98
    eMBRegisterMode mode) {
......
40 123
        }
41 124

  
42 125
      case MB_COIL_REQ_DIS:
43
        set_coil(MB_COIL_INIT, reg_buf[0] & 1);
126
        set_coil(MB_COIL_REQ_DIS, reg_buf[0] & 1);
44 127
        reg_buf[0] >>= 1;
45 128
        n_coils--;
46 129
        if (n_coils == 0) {
......
73 156
eMBErrorCode eMBRegInputCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs) {
74 157
  char serno[RFID_SERNO_SIZE];
75 158

  
76
  rfid_get_serno(serno);
159
  memcpy(reg_buf, current_user, sizeof(current_user));
77 160

  
78 161
  switch (addr) {
79 162

  
......
119 202
}
120 203

  
121 204
int main() {
205

  
206
  tool_init();
122 207
  rfid_init();
123 208

  
124 209
  eMBInit(MB_RTU, SLAVE_ADDR, 0, MB_BAUD, MB_PAR_NONE);
125 210
  eMBEnable();
126 211

  
127
  /* Set pin controlling relay to output */
128
  DDRA |= _BV(DDA1);
129

  
130 212
  sei();
131 213

  
132 214
  while (1) {
133 215
    rfid_read();
134
    /* TODO act on rfid input and coil state */
216
    tool_main();
135 217
    eMBPoll();
136 218
    _delay_ms(200);
137 219
  }

Also available in: Unified diff