Project

General

Profile

Revision dc472500

IDdc472500b5d9e2baef332e74a0ff124280767c6d
Parent e1d61b43
Child 1b054655

Added by Thomas Mullins over 11 years ago

Added LED blinking, and other fixes

View differences:

mainbox/Makefile
1 1
SRC=main.c tool.c
2
HDR=tool.h
2
HDR=tool.h ../tooltron_mb.h
3 3

  
4 4
FLAGS=-O2 -g -Wall -I.. `pkg-config --cflags --libs libmodbus`
5 5

  
mainbox/tool.c
58 58
static void tool_grant_access(struct tool_t *tool) {
59 59
  printf("Granting access to %08x on %s\n", tool->user, tool->name);
60 60
  tool_write_coil(MB_COIL_EN, 1);
61
  tool->state = TS_ON;
61 62
}
62 63

  
63 64
static void tool_deny_access(struct tool_t *tool) {
64 65
  printf("Denying access to %08x on %s\n", tool->user, tool->name);
65 66
  tool_write_coil(MB_COIL_EN, 0);
67
  tool->state = TS_OFF;
66 68
}
67 69

  
68 70
void tool_request_disable(struct tool_t *tool) {
69 71
  printf("Requesting disable on %s\n", tool->name);
70 72
  tool_write_coil(MB_COIL_REQ_DIS, 1);
73
  tool->state = TS_REQ_DIS;
71 74
}
72 75

  
73 76
void tool_poll(struct tool_t *tool) {
......
77 80
    fprintf(stderr, "modbus_set_slave: %s\n", modbus_strerror(errno));
78 81
  }
79 82

  
80
  tool_read_user(tool);
81
  printf("%08x\n", tool->user);
82
  return;
83

  
84 83
  if (modbus_read_bits(ctx, 0, N_COILS, status) == -1) {
85 84
    fprintf(stderr, "modbus_read_bits: %s\n", modbus_strerror(errno));
86 85
    return;
87 86
  }
88 87

  
88
  printf("new:%d en:%d req_dis:%d init:%d\n", status[MB_COIL_NEW],
89
      status[MB_COIL_EN], status[MB_COIL_REQ_DIS], status[MB_COIL_INIT]);
90

  
89 91
  if (!status[MB_COIL_INIT]) {
90 92
    tool->state = TS_INIT;
91 93
  }
......
100 102
      if (status[MB_COIL_NEW]) {
101 103
        tool_read_user(tool);
102 104
        // TODO check actual credentials
103
        if (rand() & 1) {
105
        printf("user:%08x\n", tool->user);
106
        //if (rand() & 1) {
104 107
          tool_grant_access(tool);
105
        } else {
108
        /*} else {
106 109
          tool_deny_access(tool);
107
        }
110
        }*/
108 111
      }
109 112
      break;
110 113

  
toolbox/led.c
1
#include "led.h"
2
#include <avr/io.h>
3

  
4
#define PRESCALE 64
5
#define CLOCK_SEL 3
6

  
7
/* F_CPU / PRESCALE = OCR * 1000 + ERROR */
8
#define OCR (F_CPU / PRESCALE / 1000UL)
9
#define ERROR (F_CPU / PRESCALE - OCR * 1000UL)
10

  
11
char count;
12
uint16_t period;
13

  
14
uint16_t ms;
15
uint16_t error;
16

  
17
static void blink() {
18
  count--;
19
  if (count % 2) {
20
    led_yellow();
21
  } else {
22
    led_off();
23
  }
24
}
25

  
26
ISR(TIMER0_COMPA_vect) {
27
  error += ERROR;
28
  if (error >= 1000) {
29
    error -= 1000;
30
  } else {
31
    ms++;
32
    if (ms == period) {
33
      blink();
34
      if (count = 0) {
35
        TCCR0B = 0;
36
      }
37
      ms = 0;
38
    }
39
  }
40
}
41

  
42
void led_blink_start(unsigned int period_ms, char n_times) {
43
  led_yellow();
44
  ms = 0;
45
  error = 0;
46
  count = n_times*2-1;
47
  period = period_ms;
48
  OCR0A = OCR;
49
  TCCR0A = _BV(WGM01);
50
  TCCR0B = CLOCK_SEL;
51
}
52

  
53
char led_blink_done() {
54
  return count == 0;
55
}
toolbox/led.h
9 9
static inline void led_yellow() {PORTC |= _BV(PC1) | _BV(PC0);}
10 10
static inline void led_green() {PORTC = (PORTC & ~_BV(PC1)) | _BV(PC0);}
11 11

  
12
/* Starts LED blinking */ 
13
void led_blink_start(unsigned int period_ms, char n_times);
14

  
15
/* Returns nonzero if blinking has finished */
16
char led_blink_done();
17

  
12 18
#endif
toolbox/main.c
57 57
      if (get_coil(MB_COIL_EN)) {
58 58
        tool_enable();
59 59
        toolstate = TS_ON;
60
      } else if (!get_coil(MB_COIL_NEW)) {
61
        toolstate = TS_OFF;
60 62
      }
61 63
      break;
62 64

  
......
84 86
      } else if (rfid_check_serno(current_user)) {
85 87
        toolstate = TS_ON;
86 88
      } else {
87
        // TODO blink yellow for 10 seconds or something
88
        set_coil(MB_COIL_EN, 0);
89
        tool_disable();
90
        toolstate = TS_OFF;
89
        if (led_blink_done()) {
90
          set_coil(MB_COIL_EN, 0);
91
          tool_disable();
92
          toolstate = TS_OFF;
93
        }
91 94
      }
92 95
      break;
93 96

  
......
99 102
        toolstate = TS_REQ_DIS;
100 103
      } else if (!rfid_check_serno(current_user)) {
101 104
        toolstate = TS_MISSING_ID;
105
        led_blink_start();
102 106
      }
103 107
      break;
104 108

  
......
109 113
eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils,
110 114
    eMBRegisterMode mode) {
111 115

  
116
  addr--;
117

  
112 118
  if (addr+n_coils > N_COILS) {
113 119
    return MB_ENOREG;
114 120
  }
......
153 159

  
154 160
  } else if (mode == MB_REG_READ) {
155 161

  
156
    reg_buf[0] = (coils >> (addr-1)) & ((1 << n_coils) - 1);
162
    reg_buf[0] = (coils >> addr) & ((1 << n_coils) - 1);
157 163
    return MB_ENOERR;
158 164

  
159 165
  }
......
167 173

  
168 174
eMBErrorCode eMBRegInputCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs) {
169 175

  
176
  addr--;
177

  
170 178
  switch (addr) {
171 179

  
172 180
    case MB_INP_SERNOL:
......
226 234
      rfid_get_serno(current_user);
227 235
      rfid_start_read();
228 236
    }
229
    //tool_main();
237
    tool_main();
230 238
    eMBPoll();
231 239
    _delay_ms(50);
232 240
  }
tooltron_mb.h
6 6
 */
7 7

  
8 8
/* Serial number from rfid reader, or 0 if there is no rfid tag present */
9
#define MB_INP_SERNOL  1
10
#define MB_INP_SERNOH  2
9
#define MB_INP_SERNOL  0
10
#define MB_INP_SERNOH  1
11 11

  
12 12
/* Value from current sensor */
13
#define MB_INP_CURRENT 3
13
#define MB_INP_CURRENT 2
14 14

  
15 15
/*
16 16
 * Coils, 1 bit read/write
......
18 18

  
19 19
/* When 1, there is a new rfid tag in the tool. Automatically cleared when the
20 20
 * main box writes to MB_COIL_EN */
21
#define MB_COIL_NEW     1
21
#define MB_COIL_NEW     0
22 22

  
23 23
/* When 1, the tool receives power. Write 1 or 0 to grant or deny an access
24 24
 * request, respectively. If the tool is running, write 0 to immediately shut
25 25
 * off the tool */
26
#define MB_COIL_EN      2
26
#define MB_COIL_EN      1
27 27

  
28 28
/* Write 1 to request that the tool is disabled soon (what that means will be
29 29
 * determined later) */
30
#define MB_COIL_REQ_DIS 3
30
#define MB_COIL_REQ_DIS 2
31 31

  
32 32
/* When 1, tool has been properly initialized. When 0, tool has lost power and
33 33
 * the main box should rewrite values for current limits (to be implemented
34 34
 * later) */
35
#define MB_COIL_INIT    4
35
#define MB_COIL_INIT    3
36 36

  
37 37
/* Number of coils */
38 38
#define N_COILS 4

Also available in: Unified diff