TI-UI/src/main.c

138 lines
2.4 KiB
C

/*
*--------------------------------------
* Program Name: UI_TEST
* Author: Erin L. Nova
* License: CNPLv7+
* Description: A test TI-84 UI program
*--------------------------------------
*/
#include <tice.h>
#include <keypadc.h>
#include <stdio.h>
enum Directions {
None,
Left,
Right,
Up,
Down,
};
int print(const char *string, int x, int y) { // Function to easily print to a location on screen
os_SetCursorPos(x, y);
os_PutStrFull(string);
return 0;
}
int clearLine(int x) {
os_SetCursorPos(x, 0);
os_PutStrFull(" ");
return 0;
}
int print_ui(enum Directions direction, int prev_index) {
os_SetCursorPos(0, 0); // Reset cursor position
print("Test: ", 0, 0);
int index = prev_index;
int num_items = 3;
char *items[8];
items[0] = "\[ ] one";
items[1] = "\[ ] two";
items[2] = "\[ ] three";
switch (direction) {
case Down:
if (index != num_items-1) {
index++;
} else {
index = 0;
}
break;
case Up:
if (index != 0) {
index--;
} else {
index = num_items-1;
}
break;
}
items[index] = "\[x] three";
int i;
for (i = 0; i < num_items; i++) {
clearLine(i+1);
print(items[i], i+1, 2);
}
print("<Enter> pressed: ", num_items+2, 0);
return index;
}
void get_input() {
// Key variable
kb_key_t key, dir_key, prev_key, prev_dir_key;
int counter = 0;
int index = 0;
// Loop until 2nd is pressed
do {
// Update kb_Data
prev_key = kb_Data[6];
prev_dir_key = kb_Data[7];
kb_Scan();
// Load group registers
key = kb_Data[6];
dir_key = kb_Data[7];
char str[25];
// Make sure it's a new press
if (key != prev_key) {
// Check what key it is
switch (key) {
case kb_Enter:
sprintf(str, "%d", counter);
print(str, 5, 17);
counter++;
break;
default:
break;
}
}
// Make sure it's a new press
if (dir_key != prev_dir_key) {
// Check what key it is
switch (dir_key) {
case kb_Down:
index = print_ui(Down, index);
break;
case kb_Up:
index = print_ui(Up, index);
break;
}
}
} while (kb_Data[6] != kb_Clear);
}
int main(void) {
os_ClrHome(); // Clear the screen
print_ui(None, 0);
get_input();
// Wait for input
while (!os_GetCSC());
return 0;
}