summaryrefslogtreecommitdiff
path: root/usb-i2c-sl018/tuer-rfid.c
blob: f3cf21bea6584797207412b8d84ff41eed442f4c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 *  spreadspace avr utils
 *
 *
 *  Copyright (C) 2013 Christian Pointner <equinox@spreadspace.org>
 *                     Othmar Gsenger <otti@wirdorange.org>
 *
 *  This file is part of spreadspace avr utils.
 *
 *  spreadspace avr utils is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  spreadspace avr utils is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>.
 */

#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include "LUFA/Drivers/Peripheral/TWI.h"

#include <stdio.h>
#include <string.h>

#include "util.h"
#include "led.h"
#include "anyio.h"

#include "heartbeat.h"
#include "stepper.h"
#include "ledmatrix.h"
#include "sl018.h"
#include "keystore.h"
#include "statemachine.h"
#include "eventqueue.h"
#include "limits.h"
#include "manual.h"
#include "ajar.h"

void handle_cmd(uint8_t cmd)
{  
  switch(cmd) {
    case 'r': 
             reset2bootloader(); 
             break;
    case 'R': 
             if(!sl018_reset())
               printf("ok\n\r");
             break;
    case 'f': {
                unsigned char * firmware_str = sl018_get_firmware_version();
                if(firmware_str)
                  printf("%s\n\r",firmware_str);

                break;
              }
    case 'e': //flash eeprom
             keystore_flash_from_stdio();
             break;
    case 'd': //dump eeprom - this breaks security!
             keystore_dump_to_stdio();
             break;
    case 'o': 
             eventqueue_push(cmd_open);
             break;
    case 'c': 
             eventqueue_push(cmd_close);
             break;
    case 't': 
             eventqueue_push(cmd_toggle);
             break;
    case 's': 
             printf("Status: %s, %s, %s\n\r", limits_to_string(limits_get()), statemachine_get_state_as_string(), ajar_to_string(ajar_get()));
             break;
    /* case '0': ledmatrix_set(off); break; */
    /* case '1': ledmatrix_set(red); break; */
    /* case '2': ledmatrix_set(red_moving); break; */
    /* case '3': ledmatrix_set(red_blink); break; */
    /* case '4': ledmatrix_set(green); break; */
    /* case '5': ledmatrix_set(green_moving); break; */
    /* case '6': ledmatrix_set(green_blink); break; */
    /* case '7': ledmatrix_set(rg_moving); break; */
    /* case '8': ledmatrix_set(rg_blink); break; */
    default: printf("Error(cmd): unknown command %02X '%c'\n\r", cmd, cmd); return;
  }
}

void handle_card(void)
{
  uid_t uid;
  sl018_read_card_uid(&uid);
  if (uid.length)
  {
    printf("Info(card): card(");
    for (uint8_t pos=0; pos<uid.length; pos++) 
      printf("%02X",uid.buffer[uid.length-pos-1]); 
    printf(") ");

    if(keystore_check_card(uid.buffer,uid.length)) {  
      printf("found - opening/closing door\n\r");
      eventqueue_push(card);
    } else {
      printf("not found - ignoring\n\r");
    }
  }
}

int main(void)
{
  MCUSR &= ~(1 << WDRF);
  wdt_disable();

  cpu_init();
  led_init();
  anyio_init(115200, false);
  TWI_Init(TWI_BIT_PRESCALE_1, TWI_BITLENGTH_FROM_FREQ(1, 200000));

  heartbeat_init();
  stepper_init();
  ledmatrix_init();
  eventqueue_init();
  limits_init();
  manual_init();
  ajar_init();
  sei();  

  sl018_reset();

  for(;;) {
    statemachine_task();

    anyio_task();
    manual_task();
    ajar_task();

    int16_t bytes_received = anyio_bytes_received();
    if(bytes_received > 0)
      handle_cmd(fgetc(stdin));

    if (sl018_check_for_new_card())
      handle_card();

    heartbeat_task();
  }
}