#include “menu.h”
#include “main.h”
// ??GPIO??
#define KEY_UP_PIN GPIO_PIN_2
#define KEY_DOWN_PIN GPIO_PIN_3
#define KEY_ENTER_PIN GPIO_PIN_4
#define KEY_BACK_PIN GPIO_PIN_5 // ???
#define KEY_PORT GPIOE
// ???
static void Power_On(void);
static void Power_Off(void);
static void Set_Voltage_Value(void);
static void Set_Current_Value(void);
static void Save_Settings(void);
static void Factory_Reset(void);
// ???
static MenuItem voltageMenuItems[] = {
{“Set Voltage”, Set_Voltage_Value, NULL},
{“Back”, Menu_BackToParent, NULL}
};
// ???
static MenuItem currentMenuItems[] = {
{“Set Current”, Set_Current_Value, NULL},
{“Back”, Menu_BackToParent, NULL}
};
// ???
static MenuItem settingsMenuItems[] = {
{“Save Settings”, Save_Settings, NULL},
{“Factory Reset”, Factory_Reset, NULL},
{“Back”, Menu_BackToParent, NULL}
};
// ???
static MenuItem mainMenuItems[] = {
{“Power On”, Power_On, NULL},
{“Power Off”, Power_Off, NULL},
{“Set Voltage”, NULL, &voltageMenu},
{“Set Current”, NULL, ¤tMenu},
{“System Settings”, NULL, &settingsMenu}
};
// ???
Menu voltageMenu = {
.title = “VOLTAGE SETTING”,
.items = voltageMenuItems,
.count = sizeof(voltageMenuItems)/sizeof(MenuItem),
.selected = 0,
.parent = &mainMenu
};
Menu currentMenu = {
.title = “CURRENT SETTING”,
.items = currentMenuItems,
.count = sizeof(currentMenuItems)/sizeof(MenuItem),
.selected = 0,
.parent = &mainMenu
};
Menu settingsMenu = {
.title = “SYSTEM SETTINGS”,
.items = settingsMenuItems,
.count = sizeof(settingsMenuItems)/sizeof(MenuItem),
.selected = 0,
.parent = &mainMenu
};
Menu mainMenu = {
.title = “MAIN MENU”,
.items = mainMenuItems,
.count = sizeof(mainMenuItems)/sizeof(MenuItem),
.selected = 0,
.parent = NULL
};
// ???
static Menu *currentMenuPtr = &mainMenu;
void Menu_Init(void) {
// ???
}
void Menu_Draw(Menu *menu) {
OLED_Clear();
OLED_ShowString((128 - strlen(menu->title)*6)/2, 0, menu->title);
for(int i = 0; i < 128; i++) {
OLED_ShowChar(i, 1, ‘-’);
}
uint8_t startIdx = 0;
if(menu->selected > 3) {
startIdx = menu->selected - 3;
}
uint8_t endIdx = startIdx + 4; if(endIdx > menu->count) { endIdx = menu->count; } for(uint8_t i = startIdx; i < endIdx; i++) { uint8_t displayLine = i - startIdx + 2; if(i == menu->selected) { OLED_ShowString(0, displayLine, ">"); OLED_ShowString(6, displayLine, menu->items[i].text); } else { OLED_ShowString(6, displayLine, menu->items[i].text); } if(menu->items[i].submenu != NULL) { OLED_ShowString(120, displayLine, ">"); } } // ????? if(menu->count > 4) { uint8_t barHeight = 32 * 4 / menu->count; uint8_t barPos = 32 * menu->selected / menu->count; for(uint8_t i = 0; i < barHeight; i++) { OLED_ShowChar(124, 2 + barPos + i, '|'); } } // ?????? if(menu->parent != NULL) { OLED_ShowString(0, 6, "B:Back"); }
}
KeyPress Menu_GetKey(void) {
static uint8_t last_state = 0;
uint8_t current_state = HAL_GPIO_ReadPin(KEY_PORT, KEY_UP_PIN) << 0 |
HAL_GPIO_ReadPin(KEY_PORT, KEY_DOWN_PIN) << 1 |
HAL_GPIO_ReadPin(KEY_PORT, KEY_ENTER_PIN) << 2 |
HAL_GPIO_ReadPin(KEY_PORT, KEY_BACK_PIN) << 3;
// ???? if(current_state != last_state) { HAL_Delay(20); current_state = HAL_GPIO_ReadPin(KEY_PORT, KEY_UP_PIN) << 0 | HAL_GPIO_ReadPin(KEY_PORT, KEY_DOWN_PIN) << 1 | HAL_GPIO_ReadPin(KEY_PORT, KEY_ENTER_PIN) << 2 | HAL_GPIO_ReadPin(KEY_PORT, KEY_BACK_PIN) << 3; } last_state = current_state; if(!HAL_GPIO_ReadPin(KEY_PORT, KEY_UP_PIN)) return KEY_UP; if(!HAL_GPIO_ReadPin(KEY_PORT, KEY_DOWN_PIN)) return KEY_DOWN; if(!HAL_GPIO_ReadPin(KEY_PORT, KEY_ENTER_PIN)) return KEY_ENTER; if(!HAL_GPIO_ReadPin(KEY_PORT, KEY_BACK_PIN)) return KEY_BACK; return KEY_NONE;
}
void Menu_Process(Menu **currentMenu) {
KeyPress key = Menu_GetKey();
switch(key) { case KEY_UP: if((*currentMenu)->selected > 0) { (*currentMenu)->selected--; } break; case KEY_DOWN: if((*currentMenu)->selected < (*currentMenu)->count - 1) { (*currentMenu)->selected++; } break; case KEY_ENTER: if((*currentMenu)->items[(*currentMenu)->selected].submenu != NULL) { // ????? *currentMenu = (*currentMenu)->items[(*currentMenu)->selected].submenu; } else if((*currentMenu)->items[(*currentMenu)->selected].action != NULL) { // ??????? (*currentMenu)->items[(*currentMenu)->selected].action(); } break; case KEY_BACK: if((*currentMenu)->parent != NULL) { // ????? *currentMenu = (*currentMenu)->parent; } break; default: break; }
}
void Menu_EnterSubmenu(Menu *submenu) {
currentMenuPtr = submenu;
}
void Menu_BackToParent(void) {
if(currentMenuPtr->parent != NULL) {
currentMenuPtr = currentMenuPtr->parent;
}
}
// ???
static void Power_On(void) {
OLED_Clear();
OLED_ShowString(20, 2, “Power ON”);
OLED_ShowString(10, 4, “Selected”);
// ???
// HAL_GPIO_WritePin(POWER_GPIO_Port, POWER_Pin, GPIO_PIN_SET);
HAL_Delay(1000);
}
static void Power_Off(void) {
OLED_Clear();
OLED_ShowString(20, 2, “Power OFF”);
OLED_ShowString(10, 4, “Selected”);
// ???
// HAL_GPIO_WritePin(POWER_GPIO_Port, POWER_Pin, GPIO_PIN_RESET);
HAL_Delay(1000);
}
static void Set_Voltage_Value(void) {
static uint16_t voltage = 0;
uint8_t exitFlag = 0;
OLED_Clear(); OLED_ShowString(20, 0, "SET VOLTAGE"); while(!exitFlag) { char buf[20]; sprintf(buf, "Voltage: %d.%dV", voltage/10, voltage%10); OLED_ShowString(10, 2, buf); OLED_ShowString(10, 4, "UP/DOWN: Adjust"); OLED_ShowString(10, 5, "ENTER: Confirm"); OLED_ShowString(10, 6, "BACK: Cancel"); KeyPress key = Menu_GetKey(); switch(key) { case KEY_UP: if(voltage < 300) voltage += 1; break; case KEY_DOWN: if(voltage > 0) voltage -= 1; break; case KEY_ENTER: // ?????? exitFlag = 1; OLED_ShowString(10, 3, "Setting saved!"); HAL_Delay(1000); break; case KEY_BACK: exitFlag = 1; break; default: break; } HAL_Delay(50); }
}
static void Set_Current_Value(void) {
static uint16_t current = 0;
uint8_t exitFlag = 0;
OLED_Clear(); OLED_ShowString(20, 0, "SET CURRENT"); while(!exitFlag) { char buf[20]; sprintf(buf, "Current: %d.%dA", current/10, current%10); OLED_ShowString(10, 2, buf); OLED_ShowString(10, 4, "UP/DOWN: Adjust"); OLED_ShowString(10, 5, "ENTER: Confirm"); OLED_ShowString(10, 6, "BACK: Cancel"); KeyPress key = Menu_GetKey(); switch(key) { case KEY_UP: if(current < 50) current += 1; break; case KEY_DOWN: if(current > 0) current -= 1; break; case KEY_ENTER: // ?????? exitFlag = 1; OLED_ShowString(10, 3, "Setting saved!"); HAL_Delay(1000); break; case KEY_BACK: exitFlag = 1; break; default: break; } HAL_Delay(50); }
}
static void Save_Settings(void) {
OLED_Clear();
OLED_ShowString(30, 2, “Saving…”);
// ???
HAL_Delay(500);
OLED_ShowString(20, 4, “Settings saved!”);
HAL_Delay(1000);
}
static void Factory_Reset(void) {
OLED_Clear();
OLED_ShowString(10, 2, “Factory Reset?”);
OLED_ShowString(10, 4, “ENTER: Confirm”);
OLED_ShowString(10, 5, “BACK: Cancel”);
uint8_t exitFlag = 0; while(!exitFlag) { KeyPress key = Menu_GetKey(); switch(key) { case KEY_ENTER: // ???????? OLED_Clear(); OLED_ShowString(20, 3, "Resetting..."); HAL_Delay(1000); // ???????? exitFlag = 1; break; case KEY_BACK: exitFlag = 1; break; default: break; } HAL_Delay(50); }
}…\Src\menu.c(89): error: #167: argument of type “const char *” is incompatible with parameter of type “char *”
OLED_ShowString((128 - strlen(menu->title)*6)/2, 0, menu->title);
…\Src\menu.c(108): error: #167: argument of type “const char *” is incompatible with parameter of type “char *”
OLED_ShowString(6, displayLine, menu->items[i].text);
…\Src\menu.c(110): error: #167: argument of type “const char *” is incompatible with parameter of type “char *”
OLED_ShowString(6, displayLine, menu->items[i].text);帮我修改一下,然后输出完整的代码