|
| 1 | +#include "cmd.h" |
| 2 | +bool high_baudrate = false;//high_baudrate=true |
| 3 | + |
| 4 | +void setup() { |
| 5 | + nfc.begin(); |
| 6 | + nfc.setPassiveActivationRetries(0x10);//设定等待次数 |
| 7 | + nfc.SAMConfig(); |
| 8 | + pixels.begin(); |
| 9 | + pixels.setBrightness(LED_brightness); |
| 10 | + while (!nfc.getFirmwareVersion()) { |
| 11 | + // FastLED.showColor(0xFF0000); |
| 12 | + pixels.fill( pixels.Color(255, 0, 0)); |
| 13 | + pixels.show(); |
| 14 | + delay(500); |
| 15 | + pixels.fill( pixels.Color(0, 0, 0)); |
| 16 | + pixels.show(); |
| 17 | + delay(500); |
| 18 | + } |
| 19 | + |
| 20 | + memset(&req, 0, sizeof(req.bytes)); |
| 21 | + memset(&res, 0, sizeof(res.bytes)); |
| 22 | + SerialDevice.begin(115200); |
| 23 | + SerialDevice.dtr(false); |
| 24 | + |
| 25 | + |
| 26 | + pixels.fill( pixels.Color(16, 16, 16)); |
| 27 | + pixels.show(); |
| 28 | +} |
| 29 | + |
| 30 | +void loop() { |
| 31 | + SerialCheck(); |
| 32 | + packet_write(); |
| 33 | +} |
| 34 | + |
| 35 | +static uint8_t len, r, checksum; |
| 36 | +static bool escape = false; |
| 37 | + |
| 38 | +static uint8_t packet_read() { |
| 39 | + |
| 40 | + while (SerialDevice.available()) { |
| 41 | + r = SerialDevice.read(); |
| 42 | + if (r == 0xE0) { |
| 43 | + req.frame_len = 0xFF; |
| 44 | + continue; |
| 45 | + } |
| 46 | + if (req.frame_len == 0xFF) { |
| 47 | + req.frame_len = r; |
| 48 | + len = 0; |
| 49 | + checksum = r; |
| 50 | + continue; |
| 51 | + } |
| 52 | + if (r == 0xD0) { |
| 53 | + escape = true; |
| 54 | + continue; |
| 55 | + } |
| 56 | + if (escape) { |
| 57 | + r++; |
| 58 | + escape = false; |
| 59 | + } |
| 60 | + req.bytes[++len] = r; |
| 61 | + if (len == req.frame_len && checksum == r) { |
| 62 | + return req.cmd; |
| 63 | + } |
| 64 | + checksum += r; |
| 65 | + } |
| 66 | + return 0; |
| 67 | +} |
| 68 | + |
| 69 | +static void packet_write() { |
| 70 | + uint8_t checksum = 0, len = 0; |
| 71 | + if (res.cmd == 0) { |
| 72 | + return; |
| 73 | + } |
| 74 | + SerialDevice.write(0xE0); |
| 75 | + while (len <= res.frame_len) { |
| 76 | + uint8_t w; |
| 77 | + if (len == res.frame_len) { |
| 78 | + w = checksum; |
| 79 | + } else { |
| 80 | + w = res.bytes[len]; |
| 81 | + checksum += w; |
| 82 | + } |
| 83 | + if (w == 0xE0 || w == 0xD0) { |
| 84 | + SerialDevice.write(0xD0); |
| 85 | + SerialDevice.write(--w); |
| 86 | + } else { |
| 87 | + SerialDevice.write(w); |
| 88 | + } |
| 89 | + len++; |
| 90 | + } |
| 91 | + res.cmd = 0; |
| 92 | +} |
| 93 | + |
| 94 | +void SerialCheck() { |
| 95 | + switch (packet_read()) { |
| 96 | + case SG_NFC_CMD_RESET: |
| 97 | + sg_nfc_cmd_reset(); |
| 98 | + break; |
| 99 | + case SG_NFC_CMD_GET_FW_VERSION: |
| 100 | + sg_nfc_cmd_get_fw_version(); |
| 101 | + break; |
| 102 | + case SG_NFC_CMD_GET_HW_VERSION: |
| 103 | + sg_nfc_cmd_get_hw_version(); |
| 104 | + break; |
| 105 | + case SG_NFC_CMD_POLL: |
| 106 | + sg_nfc_cmd_poll(); |
| 107 | + break; |
| 108 | + case SG_NFC_CMD_MIFARE_READ_BLOCK: |
| 109 | + sg_nfc_cmd_mifare_read_block(); |
| 110 | + break; |
| 111 | + case SG_NFC_CMD_FELICA_ENCAP: |
| 112 | + sg_nfc_cmd_felica_encap(); |
| 113 | + break; |
| 114 | + case SG_NFC_CMD_AIME_AUTHENTICATE: |
| 115 | + sg_nfc_cmd_aime_authenticate(); |
| 116 | + break; |
| 117 | + case SG_NFC_CMD_BANA_AUTHENTICATE: |
| 118 | + sg_nfc_cmd_bana_authenticate(); |
| 119 | + break; |
| 120 | + case SG_NFC_CMD_MIFARE_SELECT_TAG: |
| 121 | + sg_nfc_cmd_mifare_select_tag(); |
| 122 | + break; |
| 123 | + case SG_NFC_CMD_MIFARE_SET_KEY_AIME: |
| 124 | + sg_nfc_cmd_mifare_set_key_aime(); |
| 125 | + break; |
| 126 | + case SG_NFC_CMD_MIFARE_SET_KEY_BANA: |
| 127 | + sg_nfc_cmd_mifare_set_key_bana(); |
| 128 | + break; |
| 129 | + case SG_NFC_CMD_RADIO_ON: |
| 130 | + sg_nfc_cmd_radio_on(); |
| 131 | + break; |
| 132 | + case SG_NFC_CMD_RADIO_OFF: |
| 133 | + sg_nfc_cmd_radio_off(); |
| 134 | + break; |
| 135 | + case SG_RGB_CMD_RESET: |
| 136 | + sg_led_cmd_reset(); |
| 137 | + break; |
| 138 | + case SG_RGB_CMD_GET_INFO: |
| 139 | + sg_led_cmd_get_info(); |
| 140 | + break; |
| 141 | + case SG_RGB_CMD_SET_COLOR: |
| 142 | + sg_led_cmd_set_color(); |
| 143 | + break; |
| 144 | + case 0: |
| 145 | + break; |
| 146 | + default: |
| 147 | + sg_res_init(); |
| 148 | + } |
| 149 | +} |
0 commit comments