Back to Parent

#include <Adafruit_GFX.h>     // Core graphics library
#include <Adafruit_ST7789.h>  // Hardware-specific library for ST7789
#include <SPI.h>
#include <TimeLib.h>
#include <TimeAlarms.h>
#include <ArduinoBLE.h>

#define TFT_CS D10
#define TFT_RST -1  // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC D8
#define TFT_COPI D11  // Data out
#define TFT_SCLK D13  // Clock out

enum {
  NEUTRAL = 0,
  SHOCKED = 1,
};

const char *deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char *deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";

int shock = 0;
BLEService shockService(deviceServiceUuid);
BLEByteCharacteristic shockCharacteristic(deviceServiceCharacteristicUuid, BLERead | BLEWrite);

Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_COPI, TFT_SCLK, TFT_RST);

const int BUTTON_PIN = D4;  // the number of the pushbutton pin

int corruptCounter = 0;

int lastState = HIGH;  // the previous state from the input pin
int currentState;      // the current reading from the input pin

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);

  tft.init(172, 320);  // Init ST7789 172x320
  tft.setSPISpeed(32000000);
  drawText("init", ST77XX_WHITE);
  tft.fillScreen(ST77XX_BLACK);

  pinMode(BUTTON_PIN, INPUT_PULLUP);
  setTime(10, 30, 0, 4, 20, 23);  // set time to 10:30:00am Apr 20 2023

  if (!BLE.begin()) {
    Serial.println("- Starting Bluetooth® Low Energy module failed!");
    while (1)
      ;
  }

  BLE.setLocalName("Arduino Nano 33 BLE (Peripheral)");
  BLE.setAdvertisedService(shockService);
  shockService.addCharacteristic(shockCharacteristic);
  BLE.addService(shockService);
  shockCharacteristic.writeValue(-1);
  BLE.advertise();
}

void loop() {
  BLEDevice central = BLE.central();
  if (central) {
    while (central.connected()) {
      currentState = digitalRead(BUTTON_PIN);
      digitalClockDisplay();
      Serial.println("- Discovering central device...");

      if (lastState == LOW && currentState == HIGH && corruptCounter < 3) {
        tft.fillScreen(ST77XX_BLACK);
        drawText("14:00:00", ST77XX_WHITE);
        delay(3000);
        tft.fillScreen(ST77XX_BLACK);
        Serial.println("HIGH");
        drawHelp("FREE ME!", ST77XX_RED);
        delay(3000);

        while (1) {
          if (shockCharacteristic.written()) {
            Serial.println("one");
            Serial.println(shock);
            delay(5000);
            shock = shockCharacteristic.value();
            Serial.println("two");
            Serial.println(shock);
            writeShock(shock);
            break;
          }
        }
        tft.fillScreen(ST77XX_BLACK);

      } else {
        digitalWrite(LED_BUILTIN, HIGH);
      }
      // save the last state
      lastState = currentState;
    }
  }
}

void writeShock(int shock) {
  Serial.println("- Characteristic <shock_type> has changed!");

  switch (shock) {
    case SHOCKED:
      glitch();
      Serial.println("SHOCKED");
      break;
    case NEUTRAL:
      Serial.println("NOT SHOCKED");
      break;
    default:
      Serial.println("NEUTRAL");
      break;
  }
}

void glitch() {
  corruptCounter++;
  drawTextMiddle("WR%$%^&*()&^%$vdfdksnjfwvadvbjbva", ST77XX_RED);
  tft.invertDisplay(true);
  delay(100);
  tft.invertDisplay(false);
  delay(50);
  drawTextSmall("WVJasAvJsdncJDN^%&&*&&%$%^&*(AdKDAFJANFKsndlcd", ST77XX_RED);
  tft.invertDisplay(true);
  delay(100);
  tft.invertDisplay(false);
  delay(50);
  drawTextLarge("NJWJFDNSCndaaevbwrlnf^&*()&^%$ECJKE", ST77XX_RED);
  tft.invertDisplay(true);
  delay(100);
  tft.invertDisplay(false);
  delay(50);
  tft.invertDisplay(true);
  tft.fillScreen(ST77XX_BLACK);
}

void digitalClockDisplay() {
  char buffer[10];
  sprintf(buffer, "%d:%d:%d", hour(), minute(), second());
  if (corruptCounter >= 3) {
    drawText(buffer, ST77XX_WHITE);
    drawText(buffer, ST77XX_BLACK);
    glitch();
  } else {
    drawText(buffer, ST77XX_WHITE);
    drawText(buffer, ST77XX_BLACK);
  }
}

void drawText(char *text, uint16_t color) {
  tft.setCursor(0, 65);
  tft.setRotation(3);
  tft.setTextColor(color);
  tft.setTextSize(6);
  tft.setTextWrap(true);
  tft.print(text);
}

void drawHelp(char *text, uint16_t color) {
  tft.setCursor(0, 65);
  tft.setRotation(3);
  tft.setTextColor(color);
  tft.setTextSize(6);
  tft.setTextWrap(true);
  tft.print(text);
}

void drawTextSmall(char *text, uint16_t color) {
  tft.setRotation(3);
  tft.setCursor(0, 55);
  tft.setTextColor(color);
  tft.setTextSize(3);
  tft.setTextWrap(true);
  tft.print(text);
}

void drawTextMiddle(char *text, uint16_t color) {
  tft.setRotation(3);
  tft.setCursor(70, 65);
  tft.setTextColor(color);
  tft.setTextSize(5);
  tft.setTextWrap(false);
  tft.print(text);
}

void drawTextLarge(char *text, uint16_t color) {
  tft.setRotation(3);
  tft.setCursor(-50, 75);
  tft.setTextColor(color);
  tft.setTextSize(7);
  tft.setTextWrap(true);
  tft.print(text);
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0