You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.8 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>
#include <FS.h>
#include <EEPROM.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include "GyverButton.h"
#include <TFT_eSPI.h> // Hardware-specific library
#include <SPI.h>
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
#define BTN_PIN 4
GButton butt1(BTN_PIN);
ADC_MODE(ADC_VCC);
#define RF_PIN 5
#define ACTIVITY_LED 2
static inline int32_t asm_ccount(void) {
int32_t r; asm volatile ("rsr %0, ccount" : "=r"(r)); return r; }
static inline void asm_nop() {
asm volatile ("nop"); }
// Variables used for temporary storage
static uint8_t data[64];
static uint8_t value[10];
uint32_t uid;
uint32_t vendor;
int dash = 1;
void prepare_data(uint32_t ID, uint32_t VENDOR){
value[0] = (VENDOR>>4) & 0XF;
value[1] = VENDOR & 0XF;
for (int i=1; i<8; i++){
value[i+2] = (ID>>(28-i*4)) &0xF;
}
for (int i=0; i<9; i++) data[i]=1; //header
for (int i=0; i<10; i++) { //data
for (int j=0; j<4; j++) {
data[9 + i*5 +j] = value[i] >> (3-j) & 1;
}
data[9 + i*5 + 4] = ( data[9 + i*5 + 0]
+ data[9 + i*5 + 1]
+ data[9 + i*5 + 2]
+ data[9 + i*5 + 3]) % 2;
}
for (int i=0; i<4; i++) { //checksum
int checksum=0;
for (int j=0; j<10; j++) {
checksum += data[9 + i + j*5];
}
data[i+59] = checksum%2;
}
data[63] = 0; //footer
/*
delay(10);
Serial.println();
for (int i=0; i<64; i++) {
Serial.printf("%d", data[i]);
if (i>=8 && (i+2)%5==0) Serial.printf("\r\n ");
}
Serial.println();
delay(10);
*/
}
void sendkey(){
int i=0, j=0;
for (i=0; i<15; i++){
for (j=0; j<64; j++){
data[j]? (GPE |= (1<<RF_PIN)):(GPE &= ~(1<<RF_PIN));
delayMicroseconds(255);
for (int k=0; k<14; k++) asm_nop(); //fine tuning
data[j]? (GPE &= ~(1<<RF_PIN)):(GPE |= (1<<RF_PIN));
delayMicroseconds(255);
for (int k=0; k<13; k++) asm_nop(); //fine tuning
}
}
}
void IRAM_ATTR isr() {
butt1.tick(); // опрашиваем в прерывании, чтобы поймать нажатие в любом случае
}
void setup() {
Serial.begin(9600);
attachInterrupt(1, isr, CHANGE);
butt1.setDebounce(80); // настройка антидребезга (по умолчанию 80 мс)
butt1.setTimeout(300); // настройка таймаута на удержание (по умолчанию 500 мс)
butt1.setType(HIGH_PULL);
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
pinMode(RF_PIN, INPUT);
pinMode(ACTIVITY_LED, OUTPUT);
digitalWrite(ACTIVITY_LED, LOW);
}
void loop() {
butt1.tick(); // опрашиваем в скетче, иначе не будут работать проверки по времени!
if (butt1.isClick()) Serial.println("Click"); // проверка на один клик
if (butt1.isClick()) { // если кнопка была удержана (это для инкремента)
dash++; // увеличивать/уменьшать переменную value с шагом и интервалом
Serial.println(dash); // для примера выведем в порт
}
tft.setTextSize(3);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.drawString("123", 120, 120, 2);
uid=1964417;
vendor=0;
prepare_data(uid, vendor);
digitalWrite (ACTIVITY_LED, LOW);
delay(10);
sendkey();
digitalWrite (ACTIVITY_LED, HIGH);
}