#include <Arduino.h>
#include <U8g2lib.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>
#include "UI.h"
#define SCL_PIN 22 // SCL pin of OLED. Default: D1 (ESP8266) or D22 (ESP32)
#define SDA_PIN 21
#define BUTTON_UP_PIN 35
#define BUTTON_DOWN_PIN 32
#define BUTTON_SELECT_PIN 33
#define BUTTON_BACK_PIN 0
#define RECV_PIN 27
IRrecv irrecv(RECV_PIN);
decode_results results;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE, SCL_PIN, SDA_PIN);
const int NUM_ITEMS = 4;
const int MAX_ITEM_LENGTH = 20;
char menu_items [NUM_ITEMS] [MAX_ITEM_LENGTH] = {
{ "Infrared" },
{ "NFC" },
{ "Sub1GHz" },
{ "Bluetooth" }
};
const int IR_NUM_ITEMS = 3;
const int IR_MAX_ITEM_LENGTH = 10;
char ir_menu_items [IR_NUM_ITEMS] [IR_MAX_ITEM_LENGTH] = {
{ " < Back" },
{ " Receive" },
{ " Transmit" }
};
int button_up_clicked = 0; // only perform action when button is clicked, and wait until another press
int button_select_clicked = 0; // same as above
int button_back_clicked = 0; // same as above
int button_down_clicked = 0; // same as above
int item_selected = 0; // which item in the menu is selected
int item_back = 0;
int item_sel_previous; // previous item - used in the menu screen to draw the item before the selected one
int item_sel_next; // next item - used in the menu screen to draw next item after the selected one
String mode = "modeMENU";
int ir_record = 0;
void setup() {
Serial.begin(115200);
u8g2.setColorIndex(1); // set the color to white
u8g2.begin();
u8g2.setBitmapMode(1);
pinMode(BUTTON_UP_PIN, INPUT_PULLUP);
pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP);
pinMode(BUTTON_SELECT_PIN, INPUT_PULLUP);
pinMode(BUTTON_BACK_PIN, INPUT_PULLUP);
irrecv.enableIRIn();
}
void ir_rc() {
Serial.println("Halaman rfi receive");
// String irCode = "press remote";
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
String irCode = String(results.value, HEX);
Serial.println(irCode);
// String saveir = irCode;
// appendFile("/ir.txt", irCode.c_str());
// appendFile("/ir.txt", "aurora!\n");
delay(1000);
irrecv.resume(); // Receive the next value
}
}
void loop() {
if ((digitalRead(BUTTON_UP_PIN) == HIGH) && (button_up_clicked == 1)) { // unclick
button_up_clicked = 0;
}
if ((digitalRead(BUTTON_DOWN_PIN) == HIGH) && (button_down_clicked == 1)) { // unclick
button_down_clicked = 0;
}
if ((digitalRead(BUTTON_BACK_PIN) == HIGH) && (button_back_clicked == 1)) {
button_back_clicked = 0;
}
if ((digitalRead(BUTTON_SELECT_PIN) == HIGH) && (button_select_clicked == 1)) { // unclick
button_select_clicked = 0;
}
if (mode == "modeMENU") {
if ((digitalRead(BUTTON_UP_PIN) == LOW) && (button_up_clicked == 0)) { // up button clicked - jump to previous menu item
item_selected = item_selected - 1; // select previous item
button_up_clicked = 1; // set button to clicked to only perform the action once
if (item_selected < 0) { // if first item was selected, jump to last item
item_selected = NUM_ITEMS-1;
}
}
else if ((digitalRead(BUTTON_DOWN_PIN) == LOW) && (button_down_clicked == 0)) { // down button clicked - jump to next menu item
item_selected = item_selected + 1; // select next item
button_down_clicked = 1; // set button to clicked to only perform the action once
if (item_selected >= NUM_ITEMS) { // last item was selected, jump to first menu item
item_selected = 0;
}
}
item_sel_previous = item_selected - 1;
if (item_sel_previous < 0) {item_sel_previous = NUM_ITEMS - 1;} // previous item would be below first = make it the last
item_sel_next = item_selected + 1;
if (item_sel_next >= NUM_ITEMS) {item_sel_next = 0;} // next item would be after last = make it the first
}
else if (mode == "modeIR") {
if ((digitalRead(BUTTON_UP_PIN) == LOW) && (button_up_clicked == 0)) {
item_selected = item_selected - 1;
button_up_clicked = 1;
if (item_selected < 0) {
item_selected = IR_NUM_ITEMS-1;
}
}
else if ((digitalRead(BUTTON_DOWN_PIN) == LOW) && (button_down_clicked == 0)) {
item_selected = item_selected + 1;
button_down_clicked = 1;
if (item_selected >= IR_NUM_ITEMS) {
item_selected = 0;
}
}
item_sel_previous = item_selected - 1;
if (item_sel_previous < 0) {item_sel_previous = IR_NUM_ITEMS - 1;} // previous item would be below first = make it the last
item_sel_next = item_selected + 1;
if (item_sel_next >= IR_NUM_ITEMS) {item_sel_next = 0;}
}
else if (mode == "modeIRrc") {
modeIRrc();
}
if ((digitalRead(BUTTON_SELECT_PIN) == LOW) && (button_select_clicked == 0)) {
button_select_clicked = 1;
if (mode == "modeMENU") {
if (item_selected == 0) {
Serial.println("INFRARED");
mode = "modeIR";
}
else if (item_selected == 1) {
Serial.println("NFC");
mode = "modeNFC";
}
}
else if (mode == "modeIR") {
if (item_selected == 0) {
Serial.println("Back");
mode = "modeMENU";
}
else if (item_selected == 1) {
Serial.println("Receive");
mode = "modeIRrc";
}
else if (item_selected == 2) {
Serial.println("Transmit");
mode = "modeIRtr";
}
}
}
if ((digitalRead(BUTTON_BACK_PIN) == LOW) && (button_back_clicked == 0)) {
button_back_clicked = 1;
if (mode == "modeIRrc" || mode == "modeIRtr") {
mode = "modeIR";
}
else if (mode == "modeIR") {
mode = "modeMENU";
}
}
u8g2.clearBuffer(); // clear buffer for storing display content in RAM
if (mode == "modeMENU") { // MENU SCREEN
u8g2.drawXBMP(0, 22, 128, 21, bitmap_item_sel_outline);
u8g2.setFont(u8g_font_7x14);
u8g2.drawStr(25, 15, menu_items[item_sel_previous]);
u8g2.drawXBMP( 4, 2, 16, 16, bitmap_icons[item_sel_previous]);
u8g2.setFont(u8g_font_7x14B);
u8g2.drawStr(25, 15+20+2, menu_items[item_selected]);
u8g2.drawXBMP( 4, 24, 16, 16, bitmap_icons[item_selected]);
u8g2.setFont(u8g_font_7x14);
u8g2.drawStr(25, 15+20+20+2+2, menu_items[item_sel_next]);
u8g2.drawXBMP( 4, 46, 16, 16, bitmap_icons[item_sel_next]);
u8g2.drawXBMP(128-8, 0, 8, 64, bitmap_scrollbar_background);
u8g2.drawBox(125, 64/NUM_ITEMS * item_selected, 3, 64/NUM_ITEMS);
u8g2.drawXBMP(128-16-4, 64-4, 16, 4, upir_logo);
}
else if (mode == "modeIR") { // IR Menu
u8g2.drawXBMP(0, 22, 128, 21, bitmap_item_sel_outline);
u8g2.setFont(u8g_font_7x14);
u8g2.drawStr(5, 15, ir_menu_items[item_sel_previous]);
u8g2.setFont(u8g_font_7x14B);
u8g2.drawStr(5, 15+20+2, ir_menu_items[item_selected]);
u8g2.setFont(u8g_font_7x14);
u8g2.drawStr(5, 15+20+20+2+2, ir_menu_items[item_sel_next]);
u8g2.drawXBMP(128-8, 0, 8, 64, bitmap_scrollbar_background);
u8g2.drawBox(125, 64/IR_NUM_ITEMS * item_selected, 3, 64/IR_NUM_ITEMS);
u8g2.drawXBMP(128-16-4, 64-4, 16, 4, upir_logo);
}
else if (mode == "modeIRrc") { // IR Receive
// Serial.println("Ini Layer 2");
u8g2.setFont(u8g_font_7x14);
u8g2.setCursor(0, 11);
u8g2.print("Recording...");
if (irrecv.decode(&results)) {
// Serial.println(results.value, HEX);
String irCode = String(results.value, HEX);
u8g2.setCursor(3, 11);
u8g2.print(irCode);
Serial.println(irCode);
// String saveir = irCode;
// appendFile("/ir.txt", irCode.c_str());
// appendFile("/ir.txt", "aurora!\n");
delay(1000);
irrecv.resume(); // Receive the next value
}
u8g2.drawXBMP(128-16-4, 64-4, 16, 4, upir_logo);
}
else if (mode == "modeIRtr") { // IR Transmit
// Serial.println("Ini Layer 3");
u8g2.drawXBMP(128-16-4, 64-4, 16, 4, upir_logo);
}
u8g2.sendBuffer();
Serial.println(mode);
}
void modeIRrc(void) {
}