/*
 * $Id: menu_6_fn.c 464 2019-08-01 02:15:54Z true $
 * begin 20190527 true
 *
 * about menu functions
 */

#include "ui/menu.h"

#include "driver/efm8ub2_hwid.h"
#include "led/rgbled.h"
#include "sensor/adxl.h"
#include "sensor/system.h"

#include "user_config.h"

#include <stdio.h>
#include <string.h>


uint8_t font_index = 0;
uint8_t font_glyph = 0;


void menu_6_font_index(int8_t dir)
{
	font_index += dir;
	if (font_index >= (sizeof(font_table) / sizeof(font_table[0]))) {
		if (dir > 0) {
			font_index = 0;
		} else {
			font_index = (sizeof(font_table) / sizeof(font_table[0])) - 1;
		}
	}
}

void menu_6_font_glyph(int8_t dir)
{
	font_glyph += dir;
	if (font_glyph > font_table[font_index].font[FONT_CHAR_NUM_POS] - 1) {
		if (dir > 0) {
			font_glyph = 0;
		} else {
			font_glyph = font_table[font_index].font[FONT_CHAR_NUM_POS] - 1;
		}
	}
}

void menu_6_font_next()
{
	if (btn[1].held) {
		menu_6_font_index(1);
	} else {
		menu_6_font_glyph(1);
	}
}

void menu_6_font_prev()
{
	if (btn[2].held) {
		menu_6_font_index(-1);
	} else {
		menu_6_font_glyph(-1);
	}
}

void menu_6_btn_use()
{
	btn[1].push_cb = &menu_6_font_prev;
	btn[1].held_cb = &menu_6_font_prev;
	btn[1].hold_time = 8;
	btn[1].hold_retrig = 61;

	btn[2].push_cb = &menu_6_font_next;
	btn[2].held_cb = &menu_6_font_next;
	btn[2].hold_time = 8;
	btn[2].hold_retrig = 61;
}

void menu_6_disp(uint8_t idx)
{
	uint8_t i;
	char xdata txt[12];

	const uint8_t code led_pos[4][2] = {
		{10, 17},
		{88, 17},
		{88, 1},
		{10, 1}
	};

	// which item selected?
	switch (idx) {
		case 0: {
			ssd1306fb_set_cursor(10, 1);
			ssd1306fb_draw_str(font_Orbitron_28,    "GAT", 1);
			ssd1306fb_set_cursor(83, 0);
			ssd1306fb_draw_str(font_Dialog_plain_8, "Nametag", 1);
			ssd1306fb_set_cursor(83, 10);
			ssd1306fb_draw_str(font_Dialog_plain_8, "by true", 1);

			sprintf(txt, "er%s", ver);
			ssd1306fb_set_cursor(11, 24);
			ssd1306fb_draw_str(font_Dialog_plain_8, "v", 0);
			ssd1306fb_set_cursor(15, 24);
			ssd1306fb_draw_str(font_Dialog_plain_8, txt, 0);
			ssd1306fb_set_cursor(54, 24);
			ssd1306fb_draw_str(font_Dialog_plain_8, "@WhiskeyHackers", 0);
			break;
		}
		case 1: {
			ssd1306fb_set_cursor(56, -1);
			ssd1306fb_draw_str(font_DejaVu_Sans_Mono_Bold_11, "LED", 1);

			for (i = 0; i < 4; i++) {
				sprintf(txt, "h%03X", hsv[i].h);
				ssd1306fb_set_cursor(led_pos[i][0], led_pos[i][1]);
				ssd1306fb_draw_str(font_Dialog_plain_8, txt, 1);
				sprintf(txt, "s%02BX v%02BX", hsv[i].s, hsv[i].v);
				oled.cursor_x = led_pos[i][0];
				oled.cursor_y += 7;
				ssd1306fb_draw_str(font_Dialog_plain_8, txt, 1);
			}
			break;
		}
		case 2: {
			ssd1306fb_set_cursor(10, -1);
			ssd1306fb_draw_str(font_DejaVu_Sans_Mono_Bold_11, "Accelerometer", 1);

			sprintf(txt, "X: %3d", adxl.x);
			ssd1306fb_set_cursor(11, 10);
			ssd1306fb_draw_str(font_Dialog_plain_8, txt, 1);
			sprintf(txt, "Y: %3d", adxl.y);
			ssd1306fb_set_cursor(11, 17);
			ssd1306fb_draw_str(font_Dialog_plain_8, txt, 1);
			sprintf(txt, "Z: %3d", adxl.z);
			ssd1306fb_set_cursor(11, 24);
			ssd1306fb_draw_str(font_Dialog_plain_8, txt, 1);

			ssd1306fb_draw_line(80, 23, 80 + (adxl.y >> 1), 23 + (adxl.x >> 2));

			ssd1306fb_set_color(SSD1306_STATE_INVERT_PIXEL);
			ssd1306fb_draw_circle(80 + (adxl.y >> 1), 23 + (adxl.x >> 2), 2);
			ssd1306fb_set_color(SSD1306_STATE_SET_PIXEL);

			break;
		}
		case 3: {
			sprintf(txt, "CPU Load: %3Bu%%", cpu_use);
			ssd1306fb_set_cursor(10, -1);
			ssd1306fb_draw_str(font_DejaVu_Sans_Mono_Bold_11, txt, 1);

			sprintf(txt, "CPU  Max: %3Bu%%", cpu_max);
			ssd1306fb_set_cursor(10, 9);
			ssd1306fb_draw_str(font_DejaVu_Sans_Mono_Bold_11, txt, 1);

			ssd1306fb_set_cursor(10, 24);
			sprintf(txt, "Up %01d:%02d:%02d",
					(int16_t)((uptime >> 2) / 3600),
					(int16_t)((uptime >> 2) /   60) % 60,
					(int16_t)((uptime >> 2)       ) % 60);
			ssd1306fb_draw_str(font_Dialog_plain_8, txt, 0);
			break;
		}
		case 4: {
			ssd1306fb_set_cursor(10, -1);
			ssd1306fb_draw_str(font_Dialog_plain_8, "Light Level: ", 1);
			oled.cursor_x = 72;
			sprintf(txt, "todo", uconf.iter);
			ssd1306fb_draw_str(font_Dialog_plain_8, txt, 1);

			ssd1306fb_set_cursor(10, 7);
			ssd1306fb_draw_str(font_Dialog_plain_8, "Core Temp: ", 1);
			oled.cursor_x = 72;
			sprintf(txt, "todo", uconf.iter);
			ssd1306fb_draw_str(font_Dialog_plain_8, txt, 1);

			ssd1306fb_set_cursor(10, 16);
			ssd1306fb_draw_str(font_Dialog_plain_8, "Flash Config Writes: ", 0);
			sprintf(txt, "%u", uconf.iter);
			ssd1306fb_draw_str(font_Dialog_plain_8, txt, 1);

			ssd1306fb_set_cursor(10, 24);
			ssd1306fb_draw_str(font_Dialog_plain_8, "Total Flash: ", 1);
			sprintf(txt, (hwid_get_model() == HWID_MODEL_F64G) ? "64K" : "32K");
			ssd1306fb_draw_str(font_Dialog_plain_8, txt, 1);

			break;
		}
		case 5: {
			ssd1306fb_set_cursor(10, -1);
			ssd1306fb_draw_str(font_DejaVu_Sans_Mono_Bold_11, "Font Test", 1);

			sprintf(txt, "Index:%3Bu", (uint8_t)font_index);
			ssd1306fb_set_cursor(10, 9);
			ssd1306fb_draw_str(font_DejaVu_Sans_Mono_Bold_11, txt, 1);

			sprintf(txt, "Glyph:%3Bu", (uint8_t)(font_glyph + 0x20));
			ssd1306fb_set_cursor(10, 19);
			ssd1306fb_draw_str(font_DejaVu_Sans_Mono_Bold_11, txt, 1);

			sprintf(txt, "%c", (font_glyph + 0x20));
			ssd1306fb_draw_rect(83, 3, 116, 28);
			ssd1306fb_set_cursor(85, 5);
			ssd1306fb_draw_str(font_table[font_index].font, txt, 1);
			break;
		}
	}

	// draw extras
	menu_draw_tabs(idx);

	// buttons
	switch (idx) {
		case 0:
		case 1: {
			menu_btn_use_std();
			break;
		}
		case 5: {
			menu_6_btn_use();
			menu_draw_buttons(MENU_BTNSTYLE_ABOUT, 0x0c);
			break;
		}
		default: {
			menu_btn_use_std();
			menu_draw_buttons(MENU_BTNSTYLE_ABOUT, 0x08);
			break;
		}
	}
}


