added doxygen comments, small fixes to menu

main
xad1 3 years ago
parent d2f0e9f481
commit 2f98d8c2c2

@ -1,7 +1,7 @@
#ifndef PIN_H
#define PIN_H
#include <Arduino.h>
const String LOCK_PIN = "";
const String LOCK_PIN = "454";
/**
* 0 = A Button
* 1 = B Button

@ -96,14 +96,14 @@ int buttonListener(const int &numItems)
return 0;
}
/** @brief A function to draw a menu on the screen. This is for static menus which have a function tied to each item.
* Loops through the menu items and draws a ">" before the currently selected item.
/** @brief A function to draw a menu on the screen. This is for static menus which have a function tied to each item.
* Loops through the menu items and draws a ">" before the currently selected item.
* After printing the items, listens for button inputs. If the A button is pressed then the function of the currently selected item is run. If the B button is pressed the function stops running (returning the previous menu).
* If the directional pad buttons are pressed it means that the selected item or page has changed, so the function is run again to redraw the menu with the new values.
* @param menuItems An array of menuItems used to draw the menu.
* @param numItems A reference to an int which holds the number of items in the menu.
* @param menuTitle A reference to a string which holds the title of the menu.
**/
* If the directional pad buttons are pressed it means that the selected item or page has changed, so the function is run again to redraw the menu with the new values.
* @param menuItems An array of menuItems used to draw the menu.
* @param numItems A reference to an int which holds the number of items in the menu.
* @param menuTitle A reference to a string which holds the title of the menu.
**/
void drawMenu(MenuItem menuItems[], const int &numItems, const String &menuTitle)
{
GO.lcd.clearDisplay();
@ -118,7 +118,9 @@ void drawMenu(MenuItem menuItems[], const int &numItems, const String &menuTitle
if (selectedItem == i)
{
GO.lcd.print("> ");
} else {
}
else
{
GO.lcd.print(" ");
}
GO.lcd.println(menuItems[i].name);
@ -171,6 +173,12 @@ void manageContainerMenu()
mainMenu();
}
/**
* @brief Callback function to print a container in the menu. Prints an online indicator which is red if offline and green if online.
* Then prints the id of the container followed by it's name.
*
* @param container The container to print.
*/
void containerMenuPrint(void *container)
{
Container *cont = static_cast<Container *>(container);
@ -188,6 +196,12 @@ void containerMenuPrint(void *container)
GO.lcd.println(String((*cont).id) + ": " + (*cont).name);
};
/**
* @brief Callback function to print a VM in the menu. Prints an online indicator which is red if offline and green if online.
* Then prints the id of the VM followed by it's name.
*
* @param vm The VM to print.
*/
void vmMenuPrint(void *vm)
{
VM *v = static_cast<VM *>(vm);
@ -205,6 +219,12 @@ void vmMenuPrint(void *vm)
GO.lcd.println(String((*v).id) + ": " + (*v).name);
};
/**
* @brief Callback function to print a node in the menu. Prints an online indicator which is red if offline and green if online.
* Then prints the name of the node.
*
* @param node
*/
void nodeMenuPrint(void *node)
{
Node *n = static_cast<Node *>(node);
@ -222,18 +242,45 @@ void nodeMenuPrint(void *node)
GO.lcd.println((*n).name);
}
/**
* @brief Callback function to print a disk in the menu. Prints the devpath of the disk.
*
* @param disk The disk to print
*/
void diskMenuPrint(void *disk)
{
Disk *d = static_cast<Disk *>(disk);
GO.lcd.println((*d).devpath);
}
/**
* @brief Callback function to print a ZFS pool in the menu. Prints the name of the pool.
*
* @param pool The pool to print.
*/
void poolMenuPrint(void *pool)
{
Pool *p = static_cast<Pool *>(pool);
GO.lcd.println((*p).name);
}
/**
* @brief Main function to print a menu of items (such as nodes or containers). First prints a title, followed by page number.
* Then prints the items starting at the page number multiplied by the number of items per page, and stops when the items per page is reached.
* Prints a ">" on the currently selected item to indicate that it is selected.
* Then uses the callback function to print the name of whatever type of item is being printed.
*
* After printing items listens for input from the user using the ButtonListener() function.
*
*
* @param items The array of items to print.
* @param item The item selected by the user.
* @param title The menu title.
* @param numItems The number of items in the array.
* @param itemSize The size of each item. Used for pointer calculation.
* @param backEnabled Whether the back button should be enabled for this menu.
* @param printMachine The callback function to print the name of the item.
*/
void listItems(void *items, void **item, const String title, const int &numItems, const int &itemSize, const bool &backEnabled, MenuPrintCallback printMachine)
{
GO.lcd.clearDisplay();
@ -251,7 +298,9 @@ void listItems(void *items, void **item, const String title, const int &numItems
if (selectedItem == i)
{
GO.lcd.print("> ");
} else {
}
else
{
GO.lcd.print(" ");
}
printMachine(itemsAsBytes + i * itemSize);
@ -274,43 +323,120 @@ void listItems(void *items, void **item, const String title, const int &numItems
}
}
/**
* @brief The calling function for listing nodes. Calls the listItems() function with the appropriate parameters for nodes.
* Passes an item pointer to the function which gets set to whatever node has been selected during the listItems() function.
* Then sets the item pointer to the selected node.
*
* Passes false to the backEnabled parameter as the back button should be disabled on the node selection screen.
*
* Passes through nodeMenuPrint() as the callback function so that the correct names are printed.
*
* @param nodes The array of nodes being listed.
* @param numItems The number of nodes being listed.
*/
void listNodes(Node *nodes, const int &numItems)
{
void *item;
listItems(nodes, &item, "Select Node", numItems, sizeof(Node), false, nodeMenuPrint);
Node *node = static_cast<Node *>(item);
Serial.println((*node).name);
selectedNode = (*node).name;
if (item != nullptr)
{
Node *node = static_cast<Node *>(item);
selectedNode = (*node).name;
}
}
/**
* @brief The calling function for listing containers. Calls the listItems() function with the appropriate parameters for containers.
* Passes an item pointer to the function which gets set to whatever container has been selected during the listItems() function.
* Then sets the item pointer to the selected container.
*
* Passes true to the backEnabled parameter as the back button should be enabled on this screen.
*
* Passes through containerMenuPrint() as the callback function so that the correct names are printed.
*
* @param containers The array of containers to list.
* @param numItems The number of containers to list.
*/
void listContainers(Container *containers, const int &numItems)
{
void *item;
listItems(containers, &item, "Select Container", numItems, sizeof(Container), true, containerMenuPrint);
Container *container = static_cast<Container *>(item);
selectedLXC = (*container).id;
if (item != nullptr)
{
Container *container = static_cast<Container *>(item);
selectedLXC = (*container).id;
}
}
/**
* @brief The calling function for listing VMs. Calls the listItems() function with the appropriate parameters for VMs.
* Passes an item pointer to the function which gets set to whatever VM has been selected during the listItems() function.
* Then sets the item pointer to the selected VM.
*
* Passes true to the backEnabled parameter as the back button should be enabled on this screen.
*
* Passes through vmMenuPrint() as the callback function so that the correct names are printed.
*
* @param vms The array of VMs to list.
* @param numItems The number of VMs to list.
*/
void listVMs(VM *vms, const int &numItems)
{
void *item;
listItems(vms, &item, "Select VM", numItems, sizeof(VM), true, vmMenuPrint);
VM *vm = static_cast<VM *>(item);
selectedVM = (*vm).id;
if (item != nullptr)
{
VM *vm = static_cast<VM *>(item);
selectedVM = (*vm).id;
}
}
/**
* @brief The calling function for listing disks. Calls the listItems() function with the appropriate parameters for disks.
* Passes an item pointer to the function which gets set to whatever disk has been selected during the listItems() function.
* Then sets the item pointer to the selected disk.
*
* Passes true to the backEnabled parameter as the back button should be enabled on this screen.
*
* Passes through diskMenuPrint() as the callback function so that the correct names are printed.
*
* @param disks The array of disks to list.
* @param numItems The number of disks to list.
*/
void listDisks(Disk *disks, const int &numItems)
{
void *item;
listItems(disks, &item, "Select Disk", numItems, sizeof(Disk), true, diskMenuPrint);
Disk *disk = static_cast<Disk *>(item);
selectedDisk = (*disk).devpath;
if (item != nullptr)
{
Disk *disk = static_cast<Disk *>(item);
selectedDisk = (*disk).devpath;
}
}
/**
* @brief The calling function for listing pools. Calls the listItems() function with the appropriate parameters for pools.
* Passes an item pointer to the function which gets set to whatever pool has been selected during the listItems() function.
* Then sets the item pointer to the selected pool.
*
* Passes true to the backEnabled parameter as the back button should be enabled on this screen.
*
* Passes through poolMenuPrint() as the callback function so that the correct names are printed.
*
* @param pools The array of pools to list.
* @param numItems The number of pools to list.
*/
void listPools(Pool *pools, const int &numItems)
{
void *item;
listItems(pools, &item, "Select Pool", numItems, sizeof(Pool), true, poolMenuPrint);
Pool *pool = static_cast<Pool *>(item);
selectedPool = (*pool).name;
if (item != nullptr)
{
Pool *pool = static_cast<Pool *>(item);
selectedPool = (*pool).name;
}
}

@ -5,31 +5,47 @@
#include <odroid_go.h>
#include <utils.h>
// Constants for UI
const int BACKGROUND_COLOR = WHITE;
const int TEXT_COLOR = BLACK;
const int TEXT_SIZE = 2;
// How often to update the statistics on the screen.
const int UPDATE_INTERVAL = 3000;
/**
* @brief Function to convert bytes to more human readable formats. Works by checking if the value is over a certain size before dividing it and adding the appropriate label.
*
* @param value The value in bytes to convert.
* @return String The converted value in a human readable string.
*/
String convertBytes(const long long &value)
{
if (value > 1099511627776)
{
return String(value / 1099511627776) + " TB";
return String(value / 1099511627776) + " TiB";
}
if (value > 1073741824)
{
return String(value / 1073741824) + " GB";
return String(value / 1073741824) + " GiB";
}
if (value > 1048576)
{
return String(value / 1048576) + " MB";
return String(value / 1048576) + " MiB";
}
if (value > 1024)
{
return String(value / 1024) + " KB";
return String(value / 1024) + " KiB";
}
return String(value) + " Bytes";
}
/**
* @brief Function convert seconds to more human readable formats. Works by checking if the value is over a certain number of seconds and then divides it before adding a label.
*
* @param value The time in seconds.
* @return String The converted time as a human readable string.
*/
String convertTime(const long long &value)
{
if (value > 3600)
@ -43,6 +59,11 @@ String convertTime(const long long &value)
return String(value) + " secs";
}
/**
* @brief Function to print the stats for a node. Uses convertTime() and convertBytes() to make values human friendly.
*
* @param node The node to print the statistics of.
*/
void printNodeStats(const Node &node)
{
GO.lcd.fillScreen(BACKGROUND_COLOR);
@ -63,6 +84,11 @@ void printNodeStats(const Node &node)
GO.lcd.println("Max Disk: " + convertBytes(node.maxdisk));
}
/**
* @brief Function to print the stats for a container. Uses convertTime() and convertBytes() to make values human friendly.
*
* @param container The container to print the statistics of.
*/
void printContainerStats(const Container &container)
{
@ -79,6 +105,11 @@ void printContainerStats(const Container &container)
GO.lcd.println("Max Disk: " + convertBytes(container.maxdisk));
}
/**
* @brief Function to print the stats for a VM. Uses convertTime() and convertBytes() to make values human friendly.
*
* @param vm The VM to print the statistics of.
*/
void printVMStats(const VM &vm)
{
@ -94,6 +125,11 @@ void printVMStats(const VM &vm)
GO.lcd.println("Max Disk: " + convertBytes(vm.maxdisk));
}
/**
* @brief Function to print the stats for a disk. Uses convertBytes() to make values human friendly.
*
* @param disk The disk to print the statistics of.
*/
void printDiskStats(const Disk &disk)
{
@ -112,6 +148,11 @@ void printDiskStats(const Disk &disk)
GO.lcd.println("Health: " + disk.health);
}
/**
* @brief Function to print the stats for a pool. Uses convertBytes() to make values human friendly.
*
* @param pool The pool to print statistics of.
*/
void printPoolStats(const Pool &pool)
{
@ -127,14 +168,18 @@ void printPoolStats(const Pool &pool)
GO.lcd.println("Health: " + pool.health);
}
/**
* @brief The calling function to print and retrieve node information.
* Retrieves the node statistics from the API by calling the getNode() function before using the printNodeStats() function to update the statistics on the screen at the specified interval.
* Returns to the main menu if the back button is pressed.
*/
void nodeInfo()
{
Serial.println("nodeinfo");
unsigned long lastUpdate = millis();
int updateInterval = 3000;
long long lastUpdate = 0;
while (true)
{
if (lastUpdate + updateInterval < millis())
if (lastUpdate + UPDATE_INTERVAL < millis())
{
printNodeStats(getNode(selectedNode));
lastUpdate = millis();
@ -149,23 +194,30 @@ void nodeInfo()
mainMenu();
}
/**
* @brief The calling function to print and retrieve container information.
* Retrieves the list of containers from the API by calling the getContainerInfo() function. Then asks the user to select a container using listContainers().
* After a container is selected, uses the getContainer() function to retrieve the container statistics and then prints them on the screen using the printContainerStats() function at the specified interval.
*
* Returns to the previous screen if the back button is pressed.
*/
void containerInfo()
{
Serial.println("container info");
selectedItem = 0;
selectedPage = 0;
selectedLXC = 0;
int numContainers;
Container *containers = getContainerInfo(numContainers, selectedNode);
listContainers(containers, numContainers);
delete[] containers;
unsigned long lastUpdate = millis();
int updateInterval = 3000;
long long lastUpdate = 0;
while (true)
while (selectedLXC > 0)
{
if (lastUpdate + updateInterval < millis())
if (lastUpdate + UPDATE_INTERVAL < millis())
{
printContainerStats(getContainer(selectedLXC, selectedNode));
lastUpdate = millis();
@ -174,26 +226,34 @@ void containerInfo()
GO.update();
if (GO.BtnB.isPressed())
{
mainMenu();
containerInfo();
break;
}
}
}
/**
* @brief The calling function to print and retrieve VM information.
* Retrieves the list of VMs from the API by calling the getVMInfo() function. Then asks the user to select a VM using listVMs().
* After a VM is selected, uses the getVM() function to retrieve the VM statistics and then prints them on the screen using the printVMStats() function at the specified interval.
*
* Returns to the previous screen if the back button is pressed.
*/
void vmInfo()
{
Serial.println("vm info");
selectedItem = 0;
selectedPage = 0;
selectedVM = 0;
int numVMs;
VM *vms = getVMInfo(numVMs, selectedNode);
listVMs(vms, numVMs);
delete[] vms;
unsigned long lastUpdate = millis();
int updateInterval = 3000;
while (true)
unsigned long lastUpdate = 0;
while (selectedVM > 0)
{
if (lastUpdate + updateInterval < millis())
if (lastUpdate + UPDATE_INTERVAL < millis())
{
printVMStats(getVM(selectedVM, selectedNode));
lastUpdate = millis();
@ -201,54 +261,76 @@ void vmInfo()
GO.update();
if (GO.BtnB.isPressed())
{
mainMenu();
vmInfo();
break;
}
}
}
/**
* @brief The calling function to print and retrieve disk information.
* Retrieves the list of disks from the API by calling the getDiskInfo() function. Then asks the user to select a disk using listDisks().
* After a disk is selected, uses the getDisk() function to retrieve the disk statistics and then prints them on the screen using the printDiskStats() function.
*
* Returns to the previous screen if the back button is pressed.
*/
void diskInfo()
{
Serial.println("disk info");
selectedItem = 0;
selectedPage = 0;
selectedDisk = "";
int numDisks;
Disk *disks = getDiskInfo(numDisks, selectedNode);
listDisks(disks, numDisks);
delete[] disks;
printDiskStats(getDisk(selectedDisk, selectedNode));
while (true)
if (selectedDisk != "")
{
GO.update();
if (GO.BtnB.isPressed())
printDiskStats(getDisk(selectedDisk, selectedNode));
while (true)
{
mainMenu();
break;
GO.update();
if (GO.BtnB.isPressed())
{
diskInfo();
break;
}
}
}
}
/**
* @brief The calling function to print and retrieve pool information.
* Retrieves the list of pools from the API by calling the getPoolInfo() function. Then asks the user to select a pool using listPools().
* After a pool is selected, uses the getPool() function to retrieve the pool statistics and then prints them on the screen using the printPoolStats() function.
*
* Returns to the previous screen if the back button is pressed.
*/
void poolInfo()
{
Serial.println("pool info");
selectedItem = 0;
selectedPage = 0;
selectedPool = "";
int numPools;
Pool *pools = getPoolInfo(numPools, selectedNode);
listPools(pools, numPools);
delete[] pools;
printPoolStats(getPool(selectedPool, selectedNode));
while (true)
if (selectedPool != "")
{
GO.update();
if (GO.BtnB.isPressed())
printPoolStats(getPool(selectedPool, selectedNode));
while (true)
{
mainMenu();
break;
GO.update();
if (GO.BtnB.isPressed())
{
mainMenu();
break;
}
}
}
}

@ -3,9 +3,13 @@
#include <WiFi.h>
#include <pin.h>
#include <json/retrieve.h>
/**
Display an error on the screen
*/
* @brief Function to display an error on the screen. Displays the error in red text for 3 seconds before continuing.
*
* @param message The message to display.
*/
void displayError(String message) {
GO.lcd.clearDisplay();
GO.lcd.setCursor(0, 0);
@ -15,23 +19,31 @@ void displayError(String message) {
delay(3000);
}
/**
* @brief Function to connect to a WiFi network. Attempts to connect to the network using the SSID and password defined in the header file.
* Displays a connecting message until the network has connected.
*
*/
void connectWiFi() {
WiFi.begin(WIFI_SSID, WIFI_PASS);
GO.lcd.clearDisplay();
GO.lcd.setCursor(0, 0);
GO.lcd.setTextColor(WHITE);
GO.lcd.setTextSize(2);
GO.lcd.setCursor(0,0);
GO.lcd.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
GO.lcd.print(".");
}
}
}
/**
* @brief Function to listen for input when entering a pin. Waits for the user to press a button and then returns a value based on the button they have pressed.
*
* @return int The value indicating which button was pressed.
*/
int pinListener()
{
delay(300);
@ -82,6 +94,14 @@ int pinListener()
return 7;
}
/**
* @brief The main function for the pin system. Uses the pinListener() function to listen for input. Adds the user's input to a string.
* Once the user is done and a start button press is detected, compares the string of the entered pin to the one defines in the header file.
* If the menu button is pressed the function sets the entered pin to blank and does not show an incorrect pin error. This will mean that the input is reset and the function starts again.
*
* @return true The entered pin is correct.
* @return false The entered pin is incorrect.
*/
bool enterPin()
{
GO.lcd.clearDisplay();

Loading…
Cancel
Save