#include "lcdgfx.h"
#include "lcdgfx_gui.h"
#include "buttons.h"

#define MO_PIN 0 // Define MOSI pin
#define CS_PIN 3 // Define CS pin
#define DC_PIN 5 // Define D/C pin

#define BUTTON_PIN 1

DisplayILI9341_240x320x16_SPI display(0, {-1, -1, DC_PIN, MO_PIN, -1, -1});

/* Define menu items of the menu box */
const char *menuItems[] = {
  "menu item 1",
  "menu item 2",
  "menu item 3",
  "menu item 4",
  "menu item 5",
  "menu item 6",
  "menu item 7",
  "menu item 8",
  "menu item 9",
};

/* This variable will hold menu state, processed by ILI9431 API functions */
LcdGfxMenu menu(menuItems, sizeof(menuItems) / sizeof(char *), (NanoRect) { {2, 2}, {83, 65}
});

static Key button;

void setup() {
  pinMode(CS_PIN, OUTPUT); // CS pin config
  pinMode(DC_PIN, OUTPUT); // D/C pin config

  display.begin();
  // display.getInterface().setRotation(1);
  display.setFixedFont(ssd1306xled_font6x8);
  display.clear();

  // Show menu on the display
  menu.show( display );
  button = getPressedButton(BUTTON_PIN);
}

void loop() {
  Key newButton = getPressedButton(BUTTON_PIN);

  if (newButton == button) {
    return;
  }

  button = newButton;

  switch (button) {
    case Key::BT_UP:
      // Move menu cursor up and refresh menu on the display
      menu.up();
      menu.show( display );
      break;
    case Key::BT_DOWN:
      // Move menu cursor down and refresh menu on the display
      menu.down();
      menu.show( display );
      break;
    case Key::BT_SELECT:
      // You always can request current position of menu cursor,
      // by calling display.menuSelection()
      break;
    default:
      break;
  }
}