#include <main.h>
#include "graphics.h"
#include <PCF8574.h>
#include <Wire.h>

PCF8574 expander_control(0x27);
PCF8574 expander_buttons(0x20);

TFT_eSPI tft = TFT_eSPI();

extern void dds_screen_render();

void draw_right_menu(const char** right_menu_entries) {
  tft.setCursor(0, 0, 4);
  tft.setTextColor(TFT_WHITE,TFT_BLACK);
  tft.setTextSize(1);
  tft.setTextDatum(MR_DATUM);
  for (uint8_t i = 0; i < 8; i++) {
    if (right_menu_entries[i] == NULL) break;
    uint16_t x = tft.width();
    uint16_t y = 160 + (85 * i);
    tft.drawRect(x - 75, y - 20, 75 + 1, 40, TFT_WHITE);
    tft.drawString(right_menu_entries[i], x, y);
  }
}

void draw_bottom_mode(const char* mode)
{
  tft.setCursor(0, 0, 4);
  tft.setTextColor(TFT_WHITE,TFT_BLACK);
  tft.setTextSize(1);
  tft.setTextDatum(ML_DATUM);
  tft.drawString(mode, 10, tft.height() - 20);
  tft.drawFastHLine(0, tft.height() - 40, tft.width(), TFT_WHITE);
}

void setup() {
  Wire.setSDA(0);
  Wire.setSDA(1);
  expander_control.begin();
  expander_buttons.begin();

  pinMode(5, OUTPUT);
  digitalWrite(5, HIGH);

  tft.init();
  tft.setRotation(2);

  graphics_sinusoide_sprite.setColorDepth(4);
  graphics_sinusoide_sprite.createSprite(90, 50);
  graphics_sinusoide_sprite.pushImage(0, 0, 90, 50, (uint16_t *)graphics_sinusoide);
  graphics_sinusoide_sprite.createPalette(graphics_sinusoide_palette, 16);

  dds_screen_render();

}

void loop() {
  if (expander_buttons.readButton(2)) {
    Serial.println("Up button pressed");
    delay(100); // simple de-bounce
  }
}
