Hi all,
I have been working on a menu system and got it working nicely, but decided it needed to store the strings in PROGMEM. I have managed to set up PROGMEM ok, and can Serial.print the buffer, but I simply cannot get the string into the display menu function? It just displays random characters. I know that you have to handle the strings differently but i'm stumped!
Any help would be very much appreciated, as I have now spent 2 days reading the various pages covering PROGMEM and can't work out where to start.
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define maxItemSize 16
const int itemsPerScreen = 9;
const int fontSize = 8;
static int pinA = 2; // Our first hardware interrupt pin is digital pin 2
static int pinB = 3; // Our second hardware interrupt pin is digital pin 3
static int enSW = 9;
volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
volatile uint16_t encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
volatile uint16_t
oldEncPos = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
volatile byte reading = 0; //somewhere to store the direct values we read from our interrupt pins before checking to see if we have moved a whole detent
#define maxItemSize 11
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
PROGMEM const char Messaging[] ="Messaging";
PROGMEM const char UVScanner[] ="UV Scanner";
PROGMEM const char SecuritySystem[] ="Security";
PROGMEM const char BACK[] ="Main Menu";
int cnt = 0;
int itemSelected, subMenuSelected;
int itemsToDisplay=0;
const char* const menu_table[] PROGMEM = {Messaging, UVScanner, SecuritySystem, BACK};
char menu[50]; // make sure this is large enough for the largest string it must hold
void setup() {
display.begin();
pinMode(enSW, INPUT_PULLUP);
pinMode(pinA, INPUT_PULLUP);
pinMode(pinB, INPUT_PULLUP);
attachInterrupt(0,PinA,RISING);
attachInterrupt(1,PinB,RISING);
display.clearDisplay();
display.setTextSize(fontSize/8);
display.setTextColor(WHITE);
display.println("Initialising...");
display.display();
Serial.begin(9600);
while(!Serial);//wait for serial to connect
for (int i = 0; i < 4; i++) //Build menu table
{
strcpy_P(menu, (char*)pgm_read_word(&(menu_table[i]))); // Necessary casts and dereferencing, just copy.
Serial.println(menu);
delay( 250 );
}
}
void loop() {
display.setTextSize(1);
display.clearDisplay();
display.setCursor(25,0);
display.println(F("Menu System"));
display.setCursor(20,25);
display.println(F("Press control to enter the menu system"));
display.display();
display.setTextSize(fontSize/8);
// Enter the settings menu if select Switch is pressed
if(digitalRead(enSW)==0){
while(digitalRead(enSW)==0);//wait till switch is released.
itemSelected = displayMenu(*menu,sizeof(menu)/maxItemSize );
switch(itemSelected){
case 0:
// subMenuSelected = displayMenu(subMenu0, sizeof(subMenu0)/maxItemSize);
break;
case 1:
// subMenuSelected = displayMenu(subMenu1, sizeof(subMenu1)/maxItemSize);
break;
case 2:
// subMenuSelected = displayMenu(subMenu2, sizeof(subMenu1)/maxItemSize);
break;
default: break;
}
}
}
// This function accepts the a 2D character array and its length and display it on the screen.
// The function montiers the encoder position and moves the menu up and down.
// It returns the selected menu option to the calling functions
int displayMenu(char *menuInput[], int menuLength){
Serial.println(menuLength);
int curPos,startPos, endPos;
do{
startPos = encoderPos%menuLength;
display.clearDisplay();
endPos = itemsPerScreen;
if(menuLength < itemsPerScreen)
{
endPos = menuLength -startPos;
}
if((menuLength-startPos)<itemsPerScreen)
{
endPos = menuLength -startPos;
}
for(cnt = 0; cnt<=(endPos-1); cnt++){
if(cnt == 0)
{
display.setCursor(0,0);
display.print(F("Scroll to select"));
display.drawFastHLine(0,10,200,WHITE);
display.setCursor(0,16);// sets the selection pointer
display.print(F("->"));
}
display.setCursor(16,16 + cnt*fontSize);// sets the height of the submenu text
display.println(*menuInput[cnt+startPos]);
}
display.display();
cnt = 0;
if(oldEncPos != encoderPos) {
oldEncPos = encoderPos;
}
}while(digitalRead(enSW));
while(digitalRead(enSW)==0); //wait till switch is reseleased
return startPos;
}
/*******************************Utility Functions *******************************/
void PinA(){
cli(); //stop interrupts happening before we read pin values
reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
if(reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
encoderPos --; //decrement the encoder's position count
bFlag = 0; //reset flags for the next turn
aFlag = 0; //reset flags for the next turn
}
else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
sei(); //restart interrupts
}
void PinB(){
cli(); //stop interrupts happening before we read pin values
reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
if (reading == B00001100 && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
encoderPos ++; //increment the encoder's position count
bFlag = 0; //reset flags for the next turn
aFlag = 0; //reset flags for the next turn
}
else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
sei(); //restart interrupts
}