// C++ code - Έκδοση χωρίς εξωτερικές βιβλιοθήκες για Waveshare LCD1602
#include <Wire.h>

#define LCD_ADDRESS 0x3E // Η εργοστασιακή διεύθυνση I2C του chip AIP31068

int raw_temp = 0;
int temp_C = 0;
int temp_F = 0;
int light_percent = 0;
int temp_C1 = 0;
int temp_F1 = 0;

// Συναρτήσεις χαμηλού επιπέδου για τον έλεγχο της οθόνης Waveshare
void lcd_sendCommand(byte cmd) {
  Wire.beginTransmission(LCD_ADDRESS);
  Wire.write(0x00); 
  Wire.write(cmd);
  Wire.endTransmission();
}
void lcd_writeChar(byte data) {
  Wire.beginTransmission(LCD_ADDRESS);
  Wire.write(0x40); 
  Wire.write(data);
  Wire.endTransmission();
}

void lcd_print(const char* str) {
  while (*str) {
    lcd_writeChar(*str++);
  }
}

void lcd_printInt(int num) {
  char buf[12];
  itoa(num, buf, 10);
  lcd_print(buf);
}
void lcd_clear() {
  lcd_sendCommand(0x01);
  delay(2);
}

void lcd_setCursor(byte col, byte row) {
  byte val = (row == 0) ? (0x80 + col) : (0xC0 + col);
  lcd_sendCommand(val);
}

void setup() {
  Wire.begin();
  delay(50);
  
  // Αρχικοποίηση της οθόνης Waveshare (AIP31068)
  lcd_sendCommand(0x38); // Ρύθμιση λειτουργίας 8-bit, 2 γραμμές
  delay(1);
  lcd_sendCommand(0x0C); // Display ON, Κέρσορας OFF
  delay(1);
  lcd_clear();
  
  pinMode(A1, INPUT);
  pinMode(A0, INPUT);
}

void loop() {
  raw_temp = analogRead(A1);
  temp_C1 = (raw_temp * 0.488);
  temp_C = (temp_C1 - 50);
  temp_F1 = (temp_C * 1.8);
  temp_F = (temp_F1 + 32);
  light_percent = (analogRead(A0) / 10.23);
   lcd_clear();
  lcd_setCursor(0, 0);
  lcd_print("Temp C:");
  lcd_printInt(temp_C);
  lcd_setCursor(0, 1);
  lcd_print("Temp F:");
  lcd_printInt(temp_F);
  delay(3000); // Αναμονή 3 δευτερόλεπτα
  
  lcd_clear();
  lcd_setCursor(0, 0);
  lcd_print("Light %:");
  lcd_printInt(light_percent);
  delay(3000); // Αναμονή 3 δευτερόλεπτα
}