Two advice please - *.ino development
Posted: 31 Oct 2019, 20:45
Below is the code for the FM tuner on which I started working. It works, but I don't really know why
I have two questions.
The variable PCONFIG(0) contains the tuner I2C address. But the PLUGIN_INIT section not use any address. Oddly enough, the whole thing works on the default address, regardless of what address I set in the GUI.
How correctly should I use the PCONFIG(0) variable to make the tuner initialize correctly? I looked at other plugins but found no answer.
The second question is how to write the value to a variable stored in flash.
The "volfm" variable is defined in the PLUGIN_WRITE section. In this variable the volume value is stored. I want to save it to flash so that after powercycle or reboot, the volume remains unchanged.
That's why I used the line:
But unfortunately after reboot the volume remains the same as it was previously set in the GUI.
So how do you save PCONFIG(1) to flash correctly?
Will be glad for kind answer.
I have two questions.
The variable PCONFIG(0) contains the tuner I2C address. But the PLUGIN_INIT section not use any address. Oddly enough, the whole thing works on the default address, regardless of what address I set in the GUI.
How correctly should I use the PCONFIG(0) variable to make the tuner initialize correctly? I looked at other plugins but found no answer.
The second question is how to write the value to a variable stored in flash.
The "volfm" variable is defined in the PLUGIN_WRITE section. In this variable the volume value is stored. I want to save it to flash so that after powercycle or reboot, the volume remains unchanged.
That's why I used the line:
Code: Select all
PCONFIG(1) = volfm;
So how do you save PCONFIG(1) to flash correctly?
Will be glad for kind answer.
Code: Select all
#ifdef USES_P089
#define PLUGIN_089
#define PLUGIN_ID_089 89
#define PLUGIN_NAME_089 "Radio FM - RDA5807M [TESTING]"
#define PLUGIN_VALUENAME1_089 ""
#include <Wire.h>
#include <radio.h>
#include <RDA5807M.h>
RDA5807M radio;
boolean Plugin_089(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
int minFreq = 875;
int maxFreq = 1080;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_089;
Device[deviceCount].Type = DEVICE_TYPE_I2C;
Device[deviceCount].VType = SENSOR_TYPE_NONE;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = false;
Device[deviceCount].ValueCount = 0;
Device[deviceCount].SendDataOption = false;
Device[deviceCount].TimerOption = false;
Device[deviceCount].GlobalSyncOption = false;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_089);
break;
}
case PLUGIN_WEBFORM_LOAD:
{
byte choice = PCONFIG(0);
int optionValues[5] = { 0x10, 0x11, 0x20, 0x21, 0x60 };
addFormSelectorI2C(F("p089_adr"), 5, optionValues, choice);
addFormCheckBox(F("Enable default volume"), F("p089_vol"), PCONFIG(5));
addFormNote(F("Uncheck to use the last volume set"));
addFormNumericBox(F("Default volume"), F("volume"), PCONFIG(1), 0, 15);
addFormCheckBox(F("Mono"), F("p089_mono"), PCONFIG(2));
addFormCheckBox(F("Bass Boost"), F("p089_bass"), PCONFIG(3));
addFormSubHeader(F("Stations tuning"));
addFormNote(F("Frequencies should be entered without a comma or period, e.g. 1050 means 105.0 MHz in range 875 to 1080"));
addFormCheckBox(F("Enable default station"), F("p089_station"), PCONFIG(6));
addFormNote(F("Uncheck to use last station tuned"));
addFormNumericBox(F("Default station"), F("station"), PCONFIG(4), minFreq, maxFreq);
addUnit(F("MHz * 10"));
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
PCONFIG(0) = getFormItemInt(F("p089_adr"));
PCONFIG(1) = getFormItemInt(F("volume"));
PCONFIG(2) = isFormItemChecked(F("p089_mono"));
PCONFIG(3) = isFormItemChecked(F("p089_bass"));
PCONFIG(4) = getFormItemInt(F("station"));
PCONFIG(5) = isFormItemChecked(F("p089_vol"));
PCONFIG(6) = isFormItemChecked(F("p089_station"));
success = true;
break;
}
case PLUGIN_INIT:
{
radio.init();
radio.setBand(RADIO_BAND_FM);
radio.setFrequency(PCONFIG(4)*10);
radio.setBassBoost(true);
radio.setVolume(PCONFIG(1));
radio.setMono(true);
radio.setMute(false);
success = true;
break;
}
case PLUGIN_WRITE:
{
String command = parseString(string, 1);
String param = parseString(string, 2);
if (command == F("tune"))
{
int freq;
if (validIntFromString(param, freq)) {
if (freq >= minFreq && freq <= maxFreq)
{
radio.setFrequency(freq*10);
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
String log = F("FM : Freq=");
log += freq;
addLog(LOG_LEVEL_INFO, log);
}
}
}
success = true;
}
if (command == F("seekup"))
{
String log = F("FM : Seek Up");
radio.seekUp();
addLog(LOG_LEVEL_INFO, log);
success = true;
}
if (command == F("seekdown"))
{
String log = F("FM : Seek Down");
radio.seekUp();
addLog(LOG_LEVEL_INFO, log);
success = true;
}
if (command == F("volfm"))
{
int volfm;
String log = F("FM : vol=");
if (volfm < 1) volfm = 0;
if (volfm > 15) volfm = 15;
volfm = param.toInt();
PCONFIG(1) = volfm;
radio.setVolume(volfm);
log += volfm;
addLog(LOG_LEVEL_INFO, log);
success = true;
}
break;
}
}
return success;
}
#endif // USES_P089