/*
 * $Id: menu_5_fn.c 453 2019-06-14 06:19:53Z true $
 * begin 20190613 true
 *
 * settings menu functions
 */

#include "ui/menu.h"
#include "disp/font.h"

#include "user_config.h"

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


void menu_5_disp(uint8_t idx)
{
	int8_t w, x;
	char xdata txt[12];

	ssd1306fb_set_cursor(10, 7);

	// which item selected?
	switch (idx) {
		case 0: {
			ssd1306fb_draw_str(font_table[0].font, "Enable LEDs", 1);
			strcpy(txt, (uconf.flags & UCONF_FLAGS_LEDS_ENABLE) ? "On" : "Off");
			break;
		}
		case 1: {
			ssd1306fb_draw_str(font_table[0].font, "Debug: Show CPU%", 1);
			strcpy(txt, (uconf.flags & UCONF_FLAGS_SHOW_CPU_USAGE) ? "Yes" : "No");
			break;
		}
		case 2: {
			ssd1306fb_draw_str(font_table[0].font, "Debug: Show Accel", 1);
			strcpy(txt, (uconf.flags & UCONF_FLAGS_SHOW_ACCEL_ANGLE) ? "Yes" : "No");
			break;
		}
	}

	w = ssd1306fb_get_str_width(font_table[1].font, txt, strlen(txt), 1);
	x = 68 - (w >> 1);
	ssd1306fb_set_cursor(x, 18);
	ssd1306fb_draw_str(font_table[1].font, txt, 1);

	// draw box
	ssd1306fb_draw_rect(x - 2, 18, x + w + 2, 31);

	// draw extras
	menu_draw_tabs(idx);

	menu_draw_buttons(MENU_BTNSTYLE_MENU, 0x0f);
}

void menu_5_enter(uint8_t idx)
{
	switch (idx) {
		case 0: {
			if (uconf.flags & UCONF_FLAGS_LEDS_ENABLE) {
				uconf.flags &= ~UCONF_FLAGS_LEDS_ENABLE;
			} else {
				uconf.flags |=  UCONF_FLAGS_LEDS_ENABLE;
			}
			break;
		}
		case 1: {
			if (uconf.flags & UCONF_FLAGS_SHOW_CPU_USAGE) {
				uconf.flags &= ~UCONF_FLAGS_SHOW_CPU_USAGE;
			} else {
				uconf.flags |=  UCONF_FLAGS_SHOW_CPU_USAGE;
			}
			break;
		}
		case 2: {
			if (uconf.flags & UCONF_FLAGS_SHOW_ACCEL_ANGLE) {
				uconf.flags &= ~UCONF_FLAGS_SHOW_ACCEL_ANGLE;
			} else {
				uconf.flags |=  UCONF_FLAGS_SHOW_ACCEL_ANGLE;
			}
			break;
		}
	}
}
