for (y=x ; y<16 div="" y="">16>
barGraph[y] = 16;//32;
barGraph[y] = '>';
}
dmd.drawString(0,9,barGraph,16,0);
}
void loop() {
lcd_key = read_LCD_buttons();
if ((lcd_key != btnNONE) && radio.isStandBy() && buttonWasReleased) {
buttonWasReleased = false;
applicationState = 0;
radio.mute();
radio.setStandByOff();
delay(150);
radio.turnTheSoundBackOn();
analogWrite(BACKLIGHT_PIN, (byte) backlightIntensity);
dmd.clearScreen(true);
dmd.drawString(0,0," MHz",16,0);
printSelectedFrequency(radio.readFrequencyInMHz());
}
switch (lcd_key) {
case btnRIGHT: {
if (buttonWasReleased) {
buttonWasReleased = false;
switch (applicationState) {
case 0: {
if ((stationIndex < 15) && (stations[stationIndex+1] != 0.0)) {
stationIndex++;
} else {
stationIndex = 0;
}
radio.mute();
radio.selectFrequency(stations[stationIndex]);
radio.turnTheSoundBackOn();
printSelectedFrequency(radio.readFrequencyInMHz());
saveStation(stations[stationIndex]);
break;
}
// Search ahead
case 5: {
byte isBandLimitReached = false;
if (selectedMenuItem == 1) {
radio.setSearchUp();
isBandLimitReached = radio.searchNextMuting();
if (isBandLimitReached) {
radio.setSearchDown();
isBandLimitReached = radio.searchNextMuting();
}
printSelectedFrequency(radio.readFrequencyInMHz(), 2, 0);
}
break;
}
case 6: {
if (selectedStation < 108.0) {
selectedStation += .1;
radio.selectFrequency(selectedStation);
printSelectedFrequency(selectedStation, 2, 0);
saveStation(selectedStation);
}
break;
}
case 7: {
backlightIntensity += 10;
if (backlightIntensity > 255) {
backlightIntensity = 255;
}
analogWrite(BACKLIGHT_PIN, (byte) backlightIntensity);
EEPROM.write(3, (byte) backlightIntensity);
break;
}
}
}
break;
}
case btnLEFT: {
if (buttonWasReleased) {
buttonWasReleased = false;
switch (applicationState) {
// First screen, so change station
case 0: {
if (stationIndex > 0) {
stationIndex--;
} else {
stationIndex = 15;
while((stations[stationIndex] == 0.0) && (stationIndex > 0)) {
stationIndex--;
}
}
radio.mute();
radio.selectFrequency(stations[stationIndex]);
radio.turnTheSoundBackOn();
printSelectedFrequency(radio.readFrequencyInMHz());
saveStation(stations[stationIndex]);
break;
}
// Search backwards
case 5: {
byte isBandLimitReached;
if (selectedMenuItem == 1) {
radio.setSearchDown();
isBandLimitReached = radio.searchNextMuting();
if (isBandLimitReached) {
radio.setSearchUp();
isBandLimitReached = radio.searchNextMuting();
}
printSelectedFrequency(radio.readFrequencyInMHz(), 2, 0);
}
break;
}
// Decrement fine search station
case 6: {
if (selectedStation > 88.0) {
selectedStation -= .1;
radio.selectFrequency(selectedStation);
printSelectedFrequency(selectedStation, 2, 0);
saveStation(selectedStation);
}
break;
}
// Decrement backlight intensity
case 7: {
backlightIntensity -= 10;
if (backlightIntensity < 0) {
backlightIntensity = 0;
}
analogWrite(BACKLIGHT_PIN, (byte) backlightIntensity);
EEPROM.write(3, (byte) backlightIntensity);
break;
}
}
}
break;
}
case btnUP: {
if (buttonWasReleased || (applicationState == 0)) {
buttonWasReleased = false;
switch (applicationState) {
//Volume UP
case 0: {
digitalWrite(UPDOWN_PIN, HIGH);
digitalWrite(INC_PIN, LOW);
delay(DELAY_VOLUME_TRANSITION);
digitalWrite(INC_PIN, HIGH);
delay(DELAY_VOLUME_TRANSITION);
break;
}
// Menu navigation up
case 1: {
if (selectedMenuItem > 0) {
selectedMenuItem--;
if (selectedMenuItem != 3) {
dmd.clearScreen(true);
dmd.drawString(0,0,menu[0][selectedMenuItem],16,0);
dmd.drawString(0,9,menu[0][selectedMenuItem+1],16,0);
}
}
markSelectedMenuItem("<", " ");
break;
}
case 3: {
if (selectedMenuItem > 0) {
selectedMenuItem--;
if (selectedMenuItem != 3) {
dmd.clearScreen(true);
dmd.drawString(0,0,menu[1][selectedMenuItem],16,0);
dmd.drawString(0,9,menu[1][selectedMenuItem+1],16,0);
}
}
markSelectedMenuItem("<", " ");
break;
}
// Search level navigation up
case 4: {
if (selectedMenuItem > 0) {
selectedMenuItem--;
if (selectedMenuItem != 2) {
dmd.clearScreen(true);
dmd.drawString(0,0,menu[2][selectedMenuItem],16,0);
dmd.drawString(0,9,menu[2][selectedMenuItem+1],16,0);
}
}
markSelectedMenuItem("<", " ");
break;
}
}
}
break;
}
case btnDOWN: {
if (buttonWasReleased || (applicationState == 0)) {
buttonWasReleased = false;
switch (applicationState) {
//Volume DOWN
case 0: {
digitalWrite(UPDOWN_PIN, LOW);
digitalWrite(INC_PIN, LOW);
delay(DELAY_VOLUME_TRANSITION);
digitalWrite(INC_PIN, HIGH);
delay(DELAY_VOLUME_TRANSITION);
break;
}
// Menu navigation down
case 1: {
if (selectedMenuItem < (MENU_LINES-1)) {
selectedMenuItem++;
if (selectedMenuItem != 1) {
dmd.clearScreen(true);
dmd.drawString(0,0,menu[0][selectedMenuItem-1],16,0);
dmd.drawString(0,9,menu[0][selectedMenuItem],16,0);
}
}
markSelectedMenuItem(" ", "<");
break;
}
case 3:
if (selectedMenuItem < 2) {
selectedMenuItem++;
if (selectedMenuItem != 1) {
dmd.clearScreen(true);
dmd.drawString(0,0,menu[1][selectedMenuItem-1],16,0);
dmd.drawString(0,9,menu[1][selectedMenuItem],16,0);
}
}
markSelectedMenuItem(" ", "<");
break;
// Search level navigation down
case 4: {
if (selectedMenuItem < 2) {
selectedMenuItem++;
if (selectedMenuItem != 1) {
dmd.clearScreen(true);
dmd.drawString(0,0,menu[2][selectedMenuItem-1],16,0);
dmd.drawString(0,9,menu[2][selectedMenuItem],16,0);
}
}
markSelectedMenuItem(" ", "<");
break;
}
}
}
break;
}
// Typically starts the menu or execute a command
case btnSELECT: {
if (buttonWasReleased) {
buttonWasReleased = false;
switch (applicationState) {
case 0: {
applicationState = 1;
// lcd.clear();
dmd.clearScreen(true);
dmd.drawString(0,0,menu[0][0],16,0);
dmd.drawString(0,9,menu[0][1],16,0);
markSelectedMenuItem("<", " ");
selectedMenuItem = 0;
break;
}
case 1: {
switch(selectedMenuItem) {
//Mute function
case 0: {
applicationState = 0;
if (!radio.isMuted()) {
radio.mute();
} else {
radio.turnTheSoundBackOn();
}
dmd.drawString(0,0, " MHz",16,0);
dmd.drawString(0,9, " ",16,0);
printMuteStatus();
// Starts station
printSelectedFrequency(radio.readFrequencyInMHz());
break;
}
//Search
case 1: {
applicationState = 5;
dmd.clearScreen(true);
dmd.drawString(0,0, "<",1,0);
dmd.drawString(90,0, ">",1,0);
printSelectedFrequency(radio.readFrequencyInMHz(), 2, 0);
break;
}
//Fine search
case 2: {
applicationState = 6;
dmd.clearScreen(true);
dmd.drawString(0,0, "<<",2,0);
dmd.drawString(84,0, ">>",2,0);
selectedStation = radio.readFrequencyInMHz();
printSelectedFrequency(selectedStation, 2, 0);
break;
}
case 3: {
byte isBandLimitReached = 0;
byte progress = 0;
applicationState = 0;
dmd.clearScreen(true);
dmd.drawString(0,0, "Searching ...",16,0);
radio.mute();
radio.selectFrequency(88.0);
radio.setSearchUp();
while(!isBandLimitReached && (progress < 16)) {
isBandLimitReached = radio.searchNext();
stations[progress] = radio.readFrequencyInMHz();
dmd.drawString(0,9, progress,16,0);
dmd.drawString(0,9,(char)255,16,0);
progress++;
}
radio.turnTheSoundBackOn();
dmd.drawString(0,0," MHz ",16,0);
dmd.drawString(0,9," ",16,0);
// Starts station
stationIndex = 0;
saveStation(stations[stationIndex]);
printSelectedFrequency(stations[stationIndex]);
break;
}
//Configuration
case 4: {
applicationState = 3;
dmd.clearScreen(true);
dmd.drawString(0,0,menu[1][0],16,0);
dmd.drawString(0,9,menu[1][1],16,0);
markSelectedMenuItem("<", " ");
selectedMenuItem = 0;
break;
}
case 5: {
applicationState = 2;
//lcd.clear();
dmd.clearScreen(true);
radio.setStandByOn();
analogWrite(BACKLIGHT_PIN, 0);
break;
}
//Load default station
case 6: {
applicationState = 0;
dmd.drawString(0,0," MHz ",16,0);
dmd.drawString(0,9," ",16,0);
// Starts station
loadDefaultStations();
radio.mute();
radio.selectFrequency(stations[0]);
radio.turnTheSoundBackOn();
printSelectedFrequency(radio.readFrequencyInMHz());
saveStation(stations[stationIndex]);
break;
}
//Exit
case 7: {
applicationState = 0;
dmd.drawString(0,0," MHz ",16,0);
dmd.drawString(0,9," ",16,0);
// Starts station
printMuteStatus();
// Starts station
printSelectedFrequency(radio.readFrequencyInMHz());
break;
}
}
break;
}
case 3: {
switch(selectedMenuItem) {
case 0: {
applicationState = 4;
//lcd.clear();
dmd.clearScreen(true);
if (searchLevel < 3) {
dmd.drawString(0,0,menu[2][0],16,0);
dmd.drawString(0,9,menu[2][1],16,0);
} else {
dmd.drawString(0,0,menu[2][1],16,0);
dmd.drawString(0,9,menu[2][2],16,0);
}
if (searchLevel == 1) {
markSelectedMenuItem("<", " ");
} else {
markSelectedMenuItem(" ", "<");
}
selectedMenuItem = searchLevel - 1;
break;
}
// Backlight intensity selected
case 1: {
applicationState = 7;
dmd.clearScreen(true);
dmd.drawString(0,0,"Backlight int.",16,0);
break;
}
// Exit
case 2: {
applicationState = 0;
dmd.drawString(0,0," MHz ",16,0);
dmd.drawString(0,9," ",16,0);
printMuteStatus();
// Starts station
printSelectedFrequency(radio.readFrequencyInMHz());
break;
}
}
break;
}
// One of the levels selected
case 4: {
applicationState = 0;
switch(selectedMenuItem+1) {
case LOW_STOP_LEVEL: {
radio.setSearchLowStopLevel();
saveSearchLevel(LOW_STOP_LEVEL);
break;
}
case MID_STOP_LEVEL: {
radio.setSearchMidStopLevel();
saveSearchLevel(MID_STOP_LEVEL);
break;
}
case HIGH_STOP_LEVEL: {
radio.setSearchHighStopLevel();
saveSearchLevel(HIGH_STOP_LEVEL);
break;
}
}
dmd.drawString(0,0," MHz ",16,0);
dmd.drawString(0,9," ",16,0);
printMuteStatus();
// Starts station
printSelectedFrequency(radio.readFrequencyInMHz());
break;
}
// Exit
// Several states to exit from
case 5: // Exit from Search
case 6: // Exit from Fine search
case 7: { // Exit from Backlight intensity
applicationState = 0;
dmd.clearScreen(true);
dmd.drawString(0,0," MHz ",16,0);
printMuteStatus();
// Starts station
printSelectedFrequency(radio.readFrequencyInMHz());
break;
}
}
}
break;
}
// The buttons were scanned and none were pressed
case btnNONE: {
if (!buttonWasReleased) {
buttonWasReleased = true;
}
switch (applicationState) {
case 0: {
if (!radio.isStandBy()) {
printStereoStatus();
}
updateLevelIndicator();
break;
}
case 5: {
updateLevelIndicator();
break;
}
}
break;
}
}
delay(10);
}