1
0
mirror of synced 2024-11-14 19:57:36 +01:00
Arduino-Aime-Reader/Arduino-Aime-Reader.ino

165 lines
3.7 KiB
Arduino
Raw Normal View History

#if defined(__AVR_ATmega32U4__) || defined(ARDUINO_SAMD_ZERO)
#pragma message "当前的开发板是 ATmega32U4 或 SAMD_ZERO"
#define SerialDevice SerialUSB
#define DATA_PIN A3
#elif defined(ARDUINO_ESP8266_NODEMCU_ESP12E)
#pragma message "当前的开发板是 NODEMCU_ESP12E"
#define SerialDevice Serial
#define DATA_PIN D5
#else
#error "未经测试的开发板,请检查串口和阵脚定义"
#endif
2021-10-09 19:16:03 +02:00
//#define high_baudrate
#define M2F //取消注释此行将默认密钥的mifare模拟为felica
#include "cmd.h"
2021-03-15 15:48:12 +01:00
void setup() {
2021-10-09 19:16:03 +02:00
#ifdef high_baudrate
SerialDevice.begin(115200);//high_baudrate=true
#else
SerialDevice.begin(38400);
2021-10-09 19:16:03 +02:00
#endif
SerialDevice.setTimeout(0);
2021-03-15 15:48:12 +01:00
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
2021-08-15 13:56:27 +02:00
FastLED.setBrightness(50);
2021-10-09 19:16:03 +02:00
FastLED.clear();
FastLED.show();
2021-03-15 15:48:12 +01:00
nfc.begin();
2021-08-15 13:56:27 +02:00
while (!nfc.getFirmwareVersion()) {
fill_solid(leds, NUM_LEDS, 0xFF0000);
FastLED.show();
delay(500);
fill_solid(leds, NUM_LEDS, 0x000000);
FastLED.show();
delay(500);
}
2021-04-24 14:44:05 +02:00
nfc.setPassiveActivationRetries(0x10);//设定等待次数
2021-03-15 15:48:12 +01:00
nfc.SAMConfig();
memset(&req, 0, sizeof(req.bytes));
memset(&res, 0, sizeof(res.bytes));
2021-10-09 19:16:03 +02:00
fill_solid(leds, NUM_LEDS, 0xFFD700);
2021-08-15 13:56:27 +02:00
FastLED.show();
2021-03-15 15:48:12 +01:00
}
void loop() {
SerialCheck();
packet_write();
}
static uint8_t len, r, checksum;
static bool escape = false;
2021-03-15 15:48:12 +01:00
static uint8_t packet_read() {
while (SerialDevice.available()) {
r = SerialDevice.read();
2021-03-15 15:48:12 +01:00
if (r == 0xE0) {
req.frame_len = 0xFF;
len = 0;
2021-03-15 15:48:12 +01:00
continue;
}
if (req.frame_len == 0xFF) {
req.frame_len = r;
len = 1;
checksum = r;
continue;
}
if (r == 0xD0) {
escape = true;
continue;
}
if (escape) {
r++;
escape = false;
}
if (len == req.frame_len && checksum == r) {
return req.cmd;
}
req.bytes[len++] = r;
checksum += r;
}
return 0;
}
static void packet_write() {
uint8_t checksum = 0, len = 0;
if (res.cmd == 0) {
2021-03-15 15:48:12 +01:00
return;
}
SerialDevice.write(0xE0);
while (len <= res.frame_len) {
uint8_t w;
if (len == res.frame_len) {
w = checksum;
} else {
w = res.bytes[len];
checksum += w;
}
2021-03-15 15:48:12 +01:00
if (w == 0xE0 || w == 0xD0) {
SerialDevice.write(0xD0);
SerialDevice.write(--w);
2021-03-15 15:48:12 +01:00
} else {
SerialDevice.write(w);
2021-03-15 15:48:12 +01:00
}
len++;
2021-03-15 15:48:12 +01:00
}
res.cmd = 0;
}
2021-10-09 19:16:03 +02:00
void SerialCheck() {
switch (packet_read()) {
case SG_NFC_CMD_RESET:
sg_nfc_cmd_reset();
break;
case SG_NFC_CMD_GET_FW_VERSION:
sg_nfc_cmd_get_fw_version();
break;
case SG_NFC_CMD_GET_HW_VERSION:
sg_nfc_cmd_get_hw_version();
break;
case SG_NFC_CMD_POLL:
sg_nfc_cmd_poll();
break;
case SG_NFC_CMD_MIFARE_READ_BLOCK:
sg_nfc_cmd_mifare_read_block();
break;
case SG_NFC_CMD_FELICA_ENCAP:
sg_nfc_cmd_felica_encap();
break;
case SG_NFC_CMD_MIFARE_AUTHENTICATE:
sg_res_init();
break;
case SG_NFC_CMD_MIFARE_SELECT_TAG:
sg_nfc_cmd_mifare_select_tag();
break;
case SG_NFC_CMD_MIFARE_SET_KEY_AIME:
sg_nfc_cmd_mifare_set_key();
break;
case SG_NFC_CMD_MIFARE_SET_KEY_BANA:
sg_nfc_cmd_mifare_set_key();
break;
case SG_NFC_CMD_RADIO_ON:
sg_nfc_cmd_radio_on();
break;
case SG_NFC_CMD_RADIO_OFF:
sg_nfc_cmd_radio_off();
break;
case SG_RGB_CMD_RESET:
sg_led_cmd_reset();
break;
case SG_RGB_CMD_GET_INFO:
sg_led_cmd_get_info();
break;
case SG_RGB_CMD_SET_COLOR:
sg_led_cmd_set_color();
break;
case 0:
break;
default:
sg_res_init();
}
}