//-----------------------------------------------------------------
// Author: RSP @KMUTNB
// Date: 2023-02-10
//-----------------------------------------------------------------

// TODO:
//  1) Add a Rotary Encoder module to change the light-level indicator
//     if the ADC value at A0 pin below 16.
//  2) Rewrite code to use FreeRTOS tasks and inter-task synchronization/communication services

// Don't forget to include libraries
#include <Adafruit_NeoPixel.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <Arduino_FreeRTOS.h>

#define BTN_PIN       (6)
#define NEOPIXEL_PIN  (5)
// I2C pins SDA/SCL = A4/A5 pins
#define LCD_I2C_ADDR  (0x27)

Adafruit_NeoPixel pixels( 8, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
LiquidCrystal_I2C lcd( LCD_I2C_ADDR, 16, 2 );
RTC_DS1307 rtc; // Adafruit RTClib uses 0x68 as the default address.

static uint8_t disp_mode = 0;

void setup() {
  Serial.begin(115200);
  Serial.println( F("Arduino Nano Demo...") );
  Serial.println( F("- Analog input reading (potentiometer)") );
  Serial.println( F("- PWM LED dimming") );
  Serial.println( F("- Push button") );
  Serial.println( F("- LCD16x2 I2C display module") );
  Serial.println( F("- 8-pixel NeoPixel RGB ring") );
  Serial.println( F("- DHT22 temperature & humidity sensor") );
  Serial.println( F("- RTC I2C DS1307") );
  pinMode( BTN_PIN, INPUT_PULLUP );

  create_tasks();

  pixels.begin();  // Initialize Neopixel ring
  pixels.clear();  // Clear Neopixel ring
  lcd.init();      // Initialize LCD display
  lcd.backlight(); // Turn on LCD backlight
  Wire.setClock(400000);
}

void create_tasks (void) {
  xTaskCreate(
    task1, "Task1", 128, NULL, 1, NULL
  );
  xTaskCreate(
    task2, "Task2", 128, NULL, 1, NULL
  );
}

void task1 (void *pvParameters) {
  while (1) {
    // 1) Detect a button press to toggle LCD display mode
    if (digitalRead(BTN_PIN) == LOW) {
      while (!digitalRead(BTN_PIN)) {
        delay(5);
      }
      disp_mode = (disp_mode + 1) % 4; // up to 4 modes
    }
  }
}

void task2 (void *pvParameters) {
  while (1) {
    // 7) Update the LCD display
    String str1="", str2="";
    switch (disp_mode) {
      case 0:
        break;
      case 1: // show pulse width and distance
        str1 = "   PW[us]: ";
        str2 = "Dist.[cm]: ";
        break;
      case 2: // show temperature and relative humidity
        str1 = " Temp.[C]: ";
        str2 = "Humid.[%]: ";
        break;
      case 3:
        str1 = String("Date: ");
        str2 = String("Time: ");
        break;  
      default:
        disp_mode = 0;
        break;
    }
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print( str1.c_str() );
    lcd.setCursor(0, 1);
    lcd.print( str2.c_str() );
    delay(250);
  }
}

void loop() {}
//-----------------------------------------------------------------
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
pot1:GND
pot1:SIG
pot1:VCC
ring1:GND
ring1:VCC
ring1:DIN
ring1:DOUT
led1:A
led1:C
r1:1
r1:2
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
ultrasonic1:VCC
ultrasonic1:TRIG
ultrasonic1:ECHO
ultrasonic1:GND
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND
GND5VSDASCLSQWRTCDS1307+
rtc1:GND
rtc1:5V
rtc1:SDA
rtc1:SCL
rtc1:SQW