/********************************************************************************
Transistor used for piezo buzzer: 
https://media.digikey.com/pdf/Data%20Sheets/ON%20Semiconductor%20PDFs/PN2222.pdf
********************************************************************************/

/*******************************************
Level shifter used for LCD display:
https:www.ti.com/lit/ds/symlink/txs0108e.pdf
********************************************/

/*****************************
Time API
https://www.worldtimeapi.org/
*****************************/

/***********************
Weather API
https://open-meteo.com/
***********************/

#include <Keypad.h>
#include "Clock.h"
#include "Weather.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27
#define LCD_COLUMNS 16
#define LCD_LINES   2

uint8_t state;
unsigned long clockTimer;
unsigned long weatherAPItimer;
unsigned long weatherDisplayTimer;

const char* password = "";
const char* ssid = "Wokwi-GUEST";

uint8_t valIndex;
uint8_t cursorPos;
char entered_value [6];
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 1, 0, 3, 2 };
uint8_t rowPins[ROWS] = { 4, 5, 6, 7 };

LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
Clock rtc(&lcd);
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void enterTime()
{
  state = 1;
  memset(&entered_value[0], 0, sizeof(entered_value));
  cursorPos = 0;
  valIndex = 0;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Set clock then  ");
  lcd.setCursor(0, 1);
  lcd.print("press # to save.");
  delay(3000);
  lcd.clear();
}

void enterAlarm()
{
  state = 2;
  memset(&entered_value[0], 0, sizeof(entered_value));
  cursorPos = 0;
  valIndex = 0;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Enter alarm time");
  lcd.setCursor(0, 1);
  lcd.print("press # to save.");
  delay(3000);
  lcd.clear();
}

void nextChar(char key)
{
  if (valIndex < 6)
  {
    entered_value[valIndex] = key;
    lcd.setCursor(0, 0);
    lcd.print(entered_value);
    cursorPos++;
    valIndex++;
  }
}

void eraseChar()
{
  if (valIndex > 0 )
  {
    valIndex--;
    cursorPos--;
    entered_value[valIndex] = '\0';
    lcd.setCursor(cursorPos, 0);
    lcd.print(' ');
    lcd.setCursor(cursorPos, 0);
  }
}

void keyPadState0()
{
  char key = keypad.getKey();
  switch(key)
  {
    case 'A':
      enterAlarm();
      break;
    case 'C':
      enterTime();
      break;
    case '#':
      rtc.silence();
      break;
    case '*':
      rtc.addToSnooze();
      break;
  }
}

void keyPadState1()
{
  char key = keypad.getKey();
  switch(key)
  {
    case '#':
      rtc.setTime(entered_value);
      state = 0;
      break;
    case '*':
      eraseChar();
      break;
    default:
      if (isDigit(key))
      {
        nextChar(key);
      }
      break;
  }
}

void keyPadState2()
{
  char key = keypad.getKey();
  switch(key)
  {
    case '#':
      rtc.setAlarm(entered_value);
      state = 0;
      break;
    case '*':
      eraseChar();
      break;
    default:
      if (isDigit(key))
      {
        nextChar(key);
      }
      break;
  }
}

void getInput()
{
  switch (state)
  {
    case 0:
      keyPadState0();
      break;
    case 1:
      keyPadState1();
      break;
    case 2:
      keyPadState2();
      break;
  }
}

void setup() 
{
  pinMode(8, OUTPUT);
  pinMode(10, OUTPUT);
  Wire.begin(18, 19);
  lcd.init();
  lcd.backlight();
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) 
  {
    lcd.print(".");
    delay(1000);
  }
  lcd.clear();
  if (!rtc.setTimeFromAPI())
  {
    char t [] = __TIME__;
    char compileTime [] = { t[0], t[1], t[3], t[4], t[6], t[7] };
    rtc.setTime(compileTime);
  }
  getWeather(lcd);
  printWeather(lcd);

  Serial.begin(115200);
}

void loop() 
{
  if (state == 0)
  {
    unsigned long millisNow = millis();
    if (millisNow - clockTimer >= 1000)
    {
      clockTimer = millisNow;
      rtc.updateClock();
      // create a variable that toggles every second
      // primarily to be used to simplify the inline assembly code
      unsigned long secondsEvenOdd = millisNow/1000 % 2;
      Serial.println(secondsEvenOdd);
      //digitalWrite(8,secondsEvenOdd); // toggle LED every second
      /*
        playing around with inline assembly to toggle LED
        instead of using digitalWrite
      */
      asm volatile (
        "addi x0, x0, 0     \n"  // nop psuedo instruction (included just to confirm nop equiv)
        // get address of GPIO bit set/clear register
        "lui t1, 0x60004    \n"  // start of peripherals in ESP32-C3 (0x6000_4000) (upper 20 bits)
        "addi t1, t1, 0x008 \n"  // select GPIO output bit set config register (lower 12 bits)
        "add t2, zero, zero \n"
        "addi t2, zero, 1<<8  \n"  // select GPIO 8

        //  Config registers for setting/clearing GPIO bits
        //
        // 0x6000_4008 GPIO_OUT_W1TS - write to this register sets corresponding GPIO bits
        // 0x6000_400C GPIO_OUT_W1TC - write to this register clears corresponding GPIO bits
        //
        // NOTES:
        // 1. The set register is at a lower address than the clear register
        // 2. The value to set the LED GPIO is 0 or 1, passed in local C variable secondsEvenOdd
        //
        // If the set register was at the lower address, it would be straightforward
        // to multiply the set/clear value by 4 and add to the lower config reg address
        // to compute the address of the requisite config register for clear/set
        //
        // However, this can be fixed by inverting address bit 2 *after* the address calculation
        // is done. This post-address calculation fix is only feasible because the set/clear
        // config address pair are on an address boundary that is a multiple of 8 bytes/2 words
        // 
        // This inversion is only needed for options 1 & 2, as option 3 uses a conditional
        // branch to select a store instruction with the proper offset (0 or 4) from the
        // register pair base address, where the branch is taken or not is dependent upon
        // whether a set or clear is requested.
        //
        //   Change the jump targe to select 1 of 3 different ways to set/clear the LED GPIO
        "beq zero, zero, option3 \n"  // jump targest: option1, option2, or option3

        // 1. Hack to avoid using another temp register (not sure one is available)
        //    Add 0 or 4 by adding 0 or 1 four times
        //
        //    Instruction count: 6
        "option1: \n"
        "add t1, t1, %0 \n"   // add 0 or 1
        "add t1, t1, %0 \n"   // add 0 or 1
        "add t1, t1, %0 \n"   // add 0 or 1
        "add t1, t1, %0 \n"   // add 0 or 1
        "xori t1, t1, 0x4 \n" // fix set/clear inversion by swapp
        "sw t2, 0(t1) \n"     // set/clear the GPIO bit
        "beq zero, zero, done \n"  // exit 

        // 2. Use another register to compute the offset of the set/clear register,
        //    then add it to the base address of the set/clear register pair
        //
        //    Instruction count: 4
        "option2: \n"
        "slli t3, %0, 2 \n"
        //"andi t3, t3, 0x4 \n" // this shouldn't be needed if %0 value is 0 or 1
        "add t1, t1, t3 \n"
        "xori t1, t1, 0x4 \n"   // fix set/clear inversion
        "sw t2, 0(t1) \n"       // set/clear the GPIO bit
        "beq zero, zero, done \n"  // exit 

        // 3. Set or clear using different instruction,
        //    using conditional branch to select correct instruction
        //
        //    Instruction count: 5
        "option3: \n"
        "beq zero, %0, set_zero \n"
        "sw t2, 0(t1) \n"         // set the GPIO bit
        "beq zero, zero, done \n" // bit is set, so skip to end
        "set_zero:  \n"
        "sw t2, 4(t1) \n"         // clear the GPIO bit
        "beq zero, zero, done \n" // exit 
        
        "done: \n"
        :                       // output (none here)
        : "r" (secondsEvenOdd)  // input 
        : "t1", "t2", "t3"      // clobbers these regs (compiler needs to know)
      );
    }
    if (millisNow - weatherDisplayTimer >= 10000)
    {
      weatherDisplayTimer = millisNow;
      printWeather(lcd);
    }
    if (millisNow - weatherAPItimer >= 3600000)
    {
      weatherAPItimer = millisNow;
      getWeather(lcd);
    }
  }
  getInput(); 
}
<------- Set Alarm
<------- Set Clock
|
^
------------------------------ Submit / Silence Alarm
Erase / Snooze-----------------
^
|