Skip to content
This repository has been archived by the owner on Aug 4, 2018. It is now read-only.

Commit

Permalink
LCD System (MENU IS CURRENTLY BROKEN)
Browse files Browse the repository at this point in the history
  • Loading branch information
brenapp committed Jul 12, 2017
1 parent 80fd4ca commit 580059c
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/includes/lcd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* lcd.h
* Defines a basic LCD library, with support for a little more than PROS provides by default
*/

#pragma once


namespace lcd {

void init();

// Utility
static const char * centerText(const char * text);

// Basic writing
void write(int line, char * buffer);
void center(int line, char * buffer);

// Menu system
void menu(std::vector<std::string> options[16][12], void(*callback)(int choice));
void menuReset();

bool menuActive;
unsigned int menuPoint = 0;
std::vector<std::string> menuOptions[16][12];
void ( * menuCallback) (int choice);

};
107 changes: 107 additions & 0 deletions src/lcd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* lcd.cpp
* Implements a basic LCD library, with support for a little more than PROS provides by default
*/

#include "main.h"
#include "includes/lcd.h"
#include "includes/util.h"
#include <stdlib.h>


/**
* Runs in the background, to perform LCD actions
*/
static void lcdTask(void * param) {

lcdInit(uart1);
lcdSetBacklight(uart1, true);

// Initalization Message
lcd::center(1, "Initalizing...");
lcd::center(2, "Please wait");

while(true) {
// The state of the buttons is reflected in a bitmask
int buttonMask = lcdReadButtons(uart1);

// Menu Implementation
if(lcd::menuActive) {
// Control the state of the menu
switch(buttonMask) {
// Go Back
case 0x100:
case 0x101:
case 0x110:
case 0x111:
if (lcd::menuPoint > 0) lcd::menuPoint--;
break;

// Confirm
case 0x010:
lcd::menuCallback(lcd::menuPoint);
lcd::menuReset();
break;

// Go Forward
case 0x001:
case 0x011:
if (lcd::menuPoint < sizeof(lcd::menuOptions)) lcd::menuPoint++;
break;

default: break;
}

// Display the current menu state
lcdPrint(uart1, 1, "< %s >", lcd::menuOptions[lcd::menuPoint]);
lcd::center(2, "< ok >");
}
delay(50);
}

}

namespace lcd {

void init() {
taskCreate(lcdTask, TASK_DEFAULT_STACK_SIZE, NULL, TASK_PRIORITY_DEFAULT);
}

/**
* Converted from @PixelToast's LCD library
* https://lab.pxtst.com/PixelToast/8762A-2016/
*/
static const char * centerText(const char * text) {
static char buffer[17];
strcpy(buffer, " ");
size_t strsize = strlen(text);
size_t start = 8 - (strsize / 2);
for (size_t i = 0; i < strsize; i++) buffer[start + i] = text[i];
return buffer;
}

/**
* Basic write functions to make life easier
*/
static void write(int line, const char * buffer) {
lcdSetText(uart1, line, buffer);
}

static void center(int line, const char * buffer) {
lcdSetText(uart1, line, lcd::centerText(buffer));
}


/**
* Menu functions
*/
void menu(std::vector<std::string> options[] , void(*callback)(int choice)) {
lcd::menuOptions = options;
lcd::menuCallback = callback;
}

void menuReset() {
lcd::menuActive = false;
lcd::menuPoint = 0;
}
}

0 comments on commit 580059c

Please sign in to comment.