Skip to content

Commit

Permalink
LCDKeypadRadio example cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mathertel committed Jan 30, 2023
1 parent 5884da1 commit 2089140
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 80 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/buildAVR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# GitHub Action Workflow

name: Build Examples for AVR UNO

# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches: [master]
pull_request:
branches: [master]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:

# This defines a job for checking the Arduino library format specifications
# see <https://github.com/marketplace/actions/arduino-arduino-lint-action>
compile-uno:
name: check library format
runs-on: ubuntu-latest
continue-on-error: true

steps:
- uses: actions/checkout@v3

# Arduino - lint
- name: Arduino-lint
uses: arduino/arduino-lint-action@v1
with:
library-manager: update
verbose: false

# Run Arduino Compiler
# see https://github.com/arduino/compile-sketches
- name: Compile examples
uses: arduino/compile-sketches@v1
with:
verbose: true
fqbn: arduino:avr:uno

libraries: |
- source-path: ./
- name: LiquidCrystal
sketch-paths: |
- 'examples/LCDKeypadRadio'
# size-report-sketch: 'ConnectionHandlerDemo'
# enable-size-deltas-report: 'true'
# sketches-report-path: ${{ env.SKETCHES_REPORTS_PATH }}
148 changes: 78 additions & 70 deletions examples/LCDKeypadRadio/LCDKeypadRadio.ino
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
/// * 04.10.2014 working.
/// * 22.03.2015 Copying to LCDKeypadRadio.
/// * 23.01.2023 more robust, nicer LCD messages and cleanup.
/// * 30.01.2023 avoid using analogRead() too often.

#include <LiquidCrystal.h>

Expand All @@ -57,6 +58,10 @@ enum KEYSTATE {
KEYSTATE_RIGHT
} __attribute__((packed));

// Define RESET and Mode signals for SI4703 chips:
// #define RESET_PIN 2
// #define MODE_PIN A4 // same as SDA

// ----- forwards -----
// You need this because the function is not returning a simple value.
KEYSTATE getLCDKeypadKey();
Expand All @@ -80,20 +85,25 @@ unsigned int presetIndex = 0; ///< Start at Station with index=5
/// The radio object has to be defined by using the class corresponding to the used chip.
/// by uncommenting the right radio object definition.

// RADIO radio; ///< Create an instance of a non functional radio.
// RDA5807M radio; ///< Create an instance of a RDA5807 chip radio
// SI4703 radio; ///< Create an instance of a SI4703 chip radio.
// SI4705 radio; ///< Create an instance of a SI4705 chip radio.
SI47xx radio; ///< Create an instance of a SI4721 chip radio.
// TEA5767 radio; ///< Create an instance of a TEA5767 chip radio.
// RADIO radio; ///< Create an instance of a non functional radio.
RDA5807M radio; ///< Create an instance of a RDA5807 chip radio
// SI4703 radio; ///< Create an instance of a SI4703 chip radio.
// SI4705 radio; ///< Create an instance of a SI4705 chip radio.
// SI47xx radio; ///< Create an instance of a SI4721 chip radio.
// TEA5767 radio; ///< Create an instance of a TEA5767 chip radio.


/// get a RDS parser
RDSParser rds;

unsigned long nextFreqTime = 0;
unsigned long nextKeyTime = 0;
unsigned long nextClearTime = 0;

bool doReportKey = false;
KEYSTATE lastKey = KEYSTATE_NONE;


// - - - - - - - - - - - - - - - - - - - - - - - - - -


Expand Down Expand Up @@ -149,14 +159,10 @@ void RDS_process(uint16_t block1, uint16_t block2, uint16_t block3, uint16_t blo
/// * returns a specific key down only once.
///
/// [Next] [seek--] [Vol+] [seek++] [RST]
/// [Vol-]
/// [Vol-]
///
KEYSTATE getLCDKeypadKey() {
static unsigned long lastChange = 0;
static KEYSTATE lastKey = KEYSTATE_NONE;

unsigned long now = millis();
KEYSTATE newKey;
KEYSTATE newKey = KEYSTATE_NONE;

// Get current key state
int v = analogRead(A0); // the buttons are read from the analog0 pin
Expand All @@ -177,28 +183,30 @@ KEYSTATE getLCDKeypadKey() {

if (newKey != lastKey) {
// a new situation - just remember but do not return anything pressed.
lastChange = now;
doReportKey = true;
lastKey = newKey;
return (KEYSTATE_NONE);

} else if (lastChange == 0) {
// nothing new.
return (KEYSTATE_NONE);
} else if ((newKey == lastKey) && (doReportKey)) {
doReportKey = false; // don't report twice
return (newKey);
}
if (now > lastChange + 50) {
// now its not a bouncing button any more.
lastChange = 0; // don't report twice
return (newKey);
return (KEYSTATE_NONE);
} // getLCDKeypadKey()

} else {
return (KEYSTATE_NONE);

} // if
} // getLCDKeypadKey()
void displayKeyValue(const char *key, int value) {
lcd.setCursor(0, 1);
lcd.print(key);
lcd.print(": ");
lcd.print(value);
lcd.print(" ");
} // displayKeyValue


/// Setup a FM only radio configuration with I/O for commands and debugging on the Serial port.
void setup() {
delay(1000);
// open the Serial port
Serial.begin(115200);
Serial.println("LCDKeypadRadio...");
Expand All @@ -211,6 +219,12 @@ void setup() {
delay(2000);
lcd.clear();

#if defined(RESET_PIN)
// This is required for SI4703 chips:
radio.setup(RADIO_RESETPIN, RESET_PIN);
radio.setup(RADIO_MODEPIN, MODE_PIN);
#endif

// Initialize the Radio
bool found = radio.initWire(Wire);

Expand All @@ -223,22 +237,18 @@ void setup() {
lcd.setCursor(0, 1);
lcd.print("Press reset...");

while (1) {};
while (1) {}; // forever
}

// Enable information to the Serial port
radio.debugEnable();

radio.setBandFrequency(RADIO_BAND_FM, preset[presetIndex]); // first preset.

// delay(100);

radio.setMono(false);
radio.setMute(false);
radio.setVolume(radio.getMaxVolume());

Serial.write('>');

// setup the information chain for RDS data.
radio.attachReceiveRDS(RDS_process);
rds.attachServiceNameCallback(DisplayServiceName);
Expand All @@ -250,51 +260,50 @@ void setup() {
void loop() {
unsigned long now = millis();

// check for RDS data
radio.checkRDS();

// some internal static values for parsing the input
static RADIO_FREQ lastFrequency = 0;
RADIO_FREQ f = 0;

KEYSTATE k = getLCDKeypadKey();

if (k == KEYSTATE_RIGHT) {
radio.seekUp(true);

} else if (k == KEYSTATE_UP) {
// increase volume
int v = radio.getVolume();
if (v < radio.getMaxVolume()) radio.setVolume(++v);
lcd.setCursor(0, 1);
lcd.print("vol:");
lcd.print(v);
nextClearTime = now + 2000;

} else if (k == KEYSTATE_DOWN) {
// decrease volume
int v = radio.getVolume();
if (v > 0) radio.setVolume(--v);
lcd.setCursor(0, 1);
lcd.print("vol:");
lcd.print(v);
nextClearTime = now + 2000;

} else if (k == KEYSTATE_LEFT) {
radio.seekDown(true);

} else if (k == KEYSTATE_SELECT) {
// 10110
if (presetIndex < (sizeof(preset) / sizeof(RADIO_FREQ)) - 1) {
presetIndex++;
} else {
presetIndex = 0;
// detect any key press on LCD Keypad Module
if (now > nextKeyTime) {
nextKeyTime = now + 100;

KEYSTATE k = getLCDKeypadKey();

if (k == KEYSTATE_RIGHT) {
radio.seekUp(true);

} else if (k == KEYSTATE_UP) {
// increase volume
int v = radio.getVolume();
if (v < radio.getMaxVolume()) radio.setVolume(++v);
displayKeyValue("vol", v);
nextClearTime = now + 2000;

} else if (k == KEYSTATE_DOWN) {
// decrease volume
int v = radio.getVolume();
if (v > 0) radio.setVolume(--v);
displayKeyValue("vol", v);
nextClearTime = now + 2000;

} else if (k == KEYSTATE_LEFT) {
radio.seekDown(true);

} else if (k == KEYSTATE_SELECT) {
if (presetIndex < (sizeof(preset) / sizeof(RADIO_FREQ)) - 1) {
presetIndex++;
} else {
presetIndex = 0;
}

radio.setFrequency(preset[presetIndex]);
nextClearTime = now;
}
radio.setFrequency(preset[presetIndex]);

} else {
//
}

// check for RDS data
radio.checkRDS();
} // if

// update the display from time to time
if (now > nextFreqTime) {
Expand All @@ -313,7 +322,6 @@ void loop() {
lcd.print(" ");
nextClearTime = 0;
} // if

} // loop

// End.
16 changes: 16 additions & 0 deletions examples/LCDKeypadRadio/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# LCDKeypadRadio Example

This example allows controlling the a supported radio chip by using the LCDKeypad Shield
stacked on a Arduino UNO board.

It is designed for Arduino UNO only.

You have to modify the source code line where the radio variable is defined to the chip you use.
There are out-commented lines for every chip in the sketch.

Be sure to activate the lines for the RST signal for SI4703 radio chips.

Please read the README.md files in the TEST____ examples for more chip specific hints.

There is a picture of this setup with a RDA5807 chip available at <http://localhost:8080/elements/audio/rda5807.htm>

6 changes: 3 additions & 3 deletions examples/SerialRadio/SerialRadio.ino
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void runSerialCommand(char cmd, int16_t value) {
else if (cmd == '+') {
// increase volume
int v = radio.getVolume();
radio.setVolume(++v);
radio.setVolume(++v);
} else if (cmd == '-') {
// decrease volume
int v = radio.getVolume();
Expand Down Expand Up @@ -277,14 +277,14 @@ void setup() {
if (!radio.initWire(Wire)) {
Serial.println("no radio chip found.");
delay(4000);
ESP.restart();
while (1) {};
};

radio.setBandFrequency(RADIO_BAND_FM, preset[presetIndex]); // 5. preset.

radio.setMono(false);
radio.setMute(false);
radio.setVolume(10);
radio.setVolume(radio.getMaxVolume());

// setup the information chain for RDS data.
radio.attachReceiveRDS(RDS_process);
Expand Down
17 changes: 10 additions & 7 deletions src/RDA5807M.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ bool RDA5807M::init() {
_i2cPort->begin();
_i2cPort->beginTransmission(I2C_INDX);
result = _i2cPort->endTransmission();
if (result == 0) {
if (result != 0) {
DEBUG_STR("NO radio found.");

} else {
DEBUG_STR("radio found.");
result = true;

Expand Down Expand Up @@ -139,9 +142,9 @@ void RDA5807M::term() {

void RDA5807M::setVolume(int8_t newVolume) {
DEBUG_FUNC1("setVolume", newVolume);
RADIO::setVolume(newVolume); // will constrain the _volume in the range 0.._maxVolume
RADIO::setVolume(newVolume); // will constrain the _volume in the range 0.._maxVolume
uint8_t v = _volume & RADIO_REG_VOL_VOL;

registers[RADIO_REG_VOL] &= (~RADIO_REG_VOL_VOL);
registers[RADIO_REG_VOL] |= v;
_saveRegister(RADIO_REG_VOL);
Expand Down Expand Up @@ -332,9 +335,9 @@ void RDA5807M::_saveRegister(byte regNr) {

// read a register value using 2 bytes in a row
// uint16_t RDA5807M::_read16(void) {
// uint8_t hiByte = _i2cPort->read();
// uint8_t loByte = _i2cPort->read();
// return (256 * hiByte + loByte);
// uint8_t hiByte = _i2cPort->read();
// uint8_t loByte = _i2cPort->read();
// return (256 * hiByte + loByte);
// } // _read16


Expand All @@ -357,7 +360,7 @@ void RDA5807M::checkRDS() {
registers[RADIO_REG_RA] = _read16HL(_i2cPort);

// if (registers[RADIO_REG_RA] & RADIO_REG_RA_RDSBLOCK) {
// DEBUG_STR("BLOCK_E found.");
// DEBUG_STR("BLOCK_E found.");
// } // if

if (registers[RADIO_REG_RA] & RADIO_REG_RA_RDS) {
Expand Down
Loading

0 comments on commit 2089140

Please sign in to comment.