Project

General

Profile

Revision 1b054655

ID1b05465522469f6561928140dd6d106e9733f6a6
Parent dc472500
Child 1085ef77

Added by Thomas Mullins over 11 years ago

Fixes. It tooltrons at a basic level now.

View differences:

mainbox/tool.c
85 85
    return;
86 86
  }
87 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]);
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 90

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

  
toolbox/Makefile
2 2
MCU=attiny1634
3 3
#MCU=atmega2560
4 4
F_CPU=8000000UL
5
SRC=main.c serial.c rfid.c \
5
SRC=*.c \
6 6
		freemodbus/port/*.c \
7 7
		freemodbus/modbus/mb.c \
8 8
		freemodbus/modbus/rtu/mbrtu.c \
9 9
		freemodbus/modbus/ascii/mbascii.c \
10 10
		freemodbus/modbus/functions/*.c
11
HDR=../tooltron_mb.h serial.h rfid.h freemodbus/port/*.h
11
HDR=../tooltron_mb.h *.h freemodbus/port/*.h
12 12

  
13 13
FLAGS=-Os -Wall -mmcu=$(MCU) -DF_CPU=$(F_CPU) -I.. \
14 14
			-DRTS_ENABLE \
toolbox/led.c
1 1
#include "led.h"
2 2
#include <avr/io.h>
3
#include <avr/interrupt.h>
3 4

  
4 5
#define PRESCALE 64
5 6
#define CLOCK_SEL 3
......
31 32
    ms++;
32 33
    if (ms == period) {
33 34
      blink();
34
      if (count = 0) {
35
      if (count == 0) {
35 36
        TCCR0B = 0;
36 37
      }
37 38
      ms = 0;
......
44 45
  ms = 0;
45 46
  error = 0;
46 47
  count = n_times*2-1;
47
  period = period_ms;
48
  period = period_ms/2;
48 49
  OCR0A = OCR;
50
  TIMSK = _BV(OCIE0A);
49 51
  TCCR0A = _BV(WGM01);
50 52
  TCCR0B = CLOCK_SEL;
51 53
}
toolbox/main.c
19 19

  
20 20
static enum toolstate_t toolstate = TS_INIT;
21 21
static uint8_t coils;
22
static uint8_t latest_reading[RFID_SERNO_SIZE];
22 23
static uint8_t current_user[RFID_SERNO_SIZE];
23 24

  
24 25
static inline void set_coil(char coil, char bit) {
......
32 33
static inline void tool_enable() {PORTA |= _BV(PA1);}
33 34
static inline void tool_disable() {PORTA &= ~ _BV(PA1);}
34 35

  
36
static char serno_zero(uint8_t *serno) {
37
  memset(serno, 0, RFID_SERNO_SIZE);
38
}
39

  
40
static char serno_is_nonzero(uint8_t *serno) {
41
  int i;
42
  for (i = 0; i < RFID_SERNO_SIZE; i++) {
43
    if (serno[i]) {
44
      return 1;
45
    }
46
  }
47
  return 0;
48
}
49

  
50
static char serno_equal(uint8_t *a, uint8_t *b) {
51
  return memcmp(a, b, RFID_SERNO_SIZE) == 0;
52
}
53

  
54
static void serno_cpy(uint8_t *dest, uint8_t *src) {
55
  memcpy(dest, src, RFID_SERNO_SIZE);
56
}
57

  
35 58
static void tool_main() {
36 59

  
37 60
  switch (toolstate) {
......
46 69
      break;
47 70

  
48 71
    case TS_OFF:
49
      if (rfid_nonzero()) {
50
        rfid_get_serno(current_user);
72
      led_red();
73
      if (serno_is_nonzero(latest_reading)) {
74
        serno_cpy(current_user, latest_reading);
51 75
        set_coil(MB_COIL_NEW, 1);
52 76
        toolstate = TS_WAIT_ACCESS;
53 77
      }
54 78
      break;
55 79

  
56 80
    case TS_WAIT_ACCESS:
81
      led_yellow();
57 82
      if (get_coil(MB_COIL_EN)) {
58 83
        tool_enable();
59 84
        toolstate = TS_ON;
......
73 98
        set_coil(MB_COIL_EN, 0);
74 99
        set_coil(MB_COIL_REQ_DIS, 0);
75 100
        tool_disable();
101
        serno_zero(current_user);
76 102
        toolstate = TS_OFF;
77 103
      }
78 104
      break;
......
83 109
        toolstate = TS_OFF;
84 110
      } else if (get_coil(MB_COIL_REQ_DIS)) {
85 111
        toolstate = TS_REQ_DIS;
86
      } else if (rfid_check_serno(current_user)) {
112
      } else if (serno_equal(current_user, latest_reading)) {
87 113
        toolstate = TS_ON;
88 114
      } else {
89 115
        if (led_blink_done()) {
90 116
          set_coil(MB_COIL_EN, 0);
91 117
          tool_disable();
118
          serno_zero(current_user);
92 119
          toolstate = TS_OFF;
93 120
        }
94 121
      }
95 122
      break;
96 123

  
97 124
    case TS_ON:
125
      led_green();
98 126
      if (!get_coil(MB_COIL_EN)) {
99 127
        tool_disable();
128
        serno_zero(current_user);
100 129
        toolstate = TS_OFF;
101 130
      } else if(get_coil(MB_COIL_REQ_DIS)) {
102 131
        toolstate = TS_REQ_DIS;
103
      } else if (!rfid_check_serno(current_user)) {
132
      } else if (!serno_equal(current_user, latest_reading)) {
104 133
        toolstate = TS_MISSING_ID;
105
        led_blink_start();
134
        led_blink_start(666, 6);
106 135
      }
107 136
      break;
108 137

  
......
231 260
  rfid_start_read();
232 261
  while (1) {
233 262
    if (rfid_poll()) {
234
      rfid_get_serno(current_user);
263
      rfid_get_serno(latest_reading);
235 264
      rfid_start_read();
236 265
    }
237 266
    tool_main();
toolbox/rfid.c
16 16
  }
17 17
}
18 18

  
19
static void read_serno() {
20
  int i;
21
  for (i = 0; i < RFID_SERNO_SIZE; i++) {
22
    serno[i] = serial_read_blocking();
23
  }
24
}
25

  
26
static char check_serno() {
27
  int i;
28
  for (i = 0; i < RFID_SERNO_SIZE; i++) {
29
    if (serno[i] != serial_read_blocking()) {
30
      return 0;
31
    }
32
  }
33
  return 1;
34
}
35

  
36 19
void rfid_init() {
37 20
  serial_init();
38 21
}
39 22

  
40
void rfid_read_safe() {
41
  char n_err = 0, n_ok = 0;
42
  while (1) {
43
    serial_write(read_cmd, sizeof(read_cmd));
44
    if (serial_read_blocking() == RFID_OK) {
45
      if (n_ok == 0) {
46
        read_serno();
47
      } else if (!check_serno()) {
48
        zero_serno();
49
        return;
50
      }
51
      n_ok++;
52
      if (n_ok >= RFID_MIN_OK) {
53
        return;
54
      }
55
    } else {
56
      n_err++;
57
      if (n_err >= RFID_MAX_ERRS) {
58
        zero_serno();
59
        return;
60
      }
61
    }
62
    _delay_ms(10);
63
  }
64
}
65

  
66 23
void rfid_start_read() {
67 24
  serno_idx = -1;
68
  zero_serno(); // TODO temporary
69 25
  serial_flush();
70 26
  serial_write(read_cmd, sizeof(read_cmd));
71 27
}
......
97 53
void rfid_get_serno(uint8_t *buf) {
98 54
  memcpy(buf, serno, sizeof(serno));
99 55
}
100

  
101
char rfid_check_serno(uint8_t *buf) {
102
  return memcmp(buf, serno, sizeof(serno)) == 0;
103
}
104

  
105
char rfid_nonzero() {
106
  int i;
107
  for (i = 0; i < sizeof(serno); i++) {
108
    if (serno[i]) {
109
      return 1;
110
    }
111
  }
112
  return 0;
113
}

Also available in: Unified diff