Help to write my first .ino

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
mackowiakp
Normal user
Posts: 543
Joined: 07 Jun 2018, 06:47
Location: Gdynia/Poland

Help to write my first .ino

#1 Post by mackowiakp » 26 Oct 2019, 20:32

I want to write .ino file for I2C controlled FM radio tuner. I want to create setup webform for this device. I try to use function:

Code: Select all

addFloatNumberBox
It is define in webserver files but - i dont know why - nobody use it. Int version of this function is commonly use, because it check range of entered numbers.
I want to have similar functionality in case of floats, because entered fe=requency must be from range 76.0 to 108.0 MHz, according to European standard.
So the initial part of .ino file looks like this:

Code: Select all

boolean Plugin_089(byte function, struct EventStruct *event, String& string)
{
  boolean success = false;
float minFreq = 76.0;
float maxFreq = 108.0;

  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[4] = { 0x10, 0x11, 0x20, 0x21 };
        addFormSelectorI2C(F("p089_adr"), 4, optionValues, choice);
        addFormNumericBox(F("Volume"), F("volume"), PCONFIG(1), 0, 15);
        addFormCheckBox(F("Mono"), F("p089_mono"), PCONFIG(2));
        addFormSubHeader(F("Stations tuning"));
        addFormNote(F("Frequencies should be entered without a comma or period, e.g. 1050 means 105.0 MHz"));

       addFloatNumberBox(F("Station-1"), F("station[0]"), PCONFIG_FLOAT(0), minFreq, maxFreq);
        addHtml(F(" MHz"));
       addFloatNumberBox(F("Station-2"), F("station[1]"), PCONFIG_FLOAT(1), minFreq, maxFreq);
        addHtml(F(" MHz"));
       addFloatNumberBox(F("Station-3"), F("station[2]"), PCONFIG_FLOAT(2), minFreq, maxFreq);
        addHtml(F(" MHz"));
       addFloatNumberBox(F("Station-4"), F("station[3]"), PCONFIG_FLOAT(3), minFreq, maxFreq);
        addHtml(F(" MHz"));
       addFloatNumberBox(F("Station-5"), F("station[4]"), PCONFIG_FLOAT(4), minFreq, maxFreq);
        addHtml(F(" MHz"));
       addFloatNumberBox(F("Station-6"), F("station[5]"), PCONFIG_FLOAT(5), minFreq, maxFreq);
        addHtml(F(" MHz"));
       addFloatNumberBox(F("Station-7"), F("station[6]"), PCONFIG_FLOAT(6), minFreq, maxFreq);
        addHtml(F(" MHz"));
       addFloatNumberBox(F("Station-8"), F("station[7]"), PCONFIG_FLOAT(7), minFreq, maxFreq);
        addHtml(F(" MHz"));
          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_FLOAT(0) = getFormItemFloat(F("station[0]"));
        PCONFIG_FLOAT(1) = getFormItemFloat(F("station[1]"));
        PCONFIG_FLOAT(2) = getFormItemFloat(F("station[2]"));
        PCONFIG_FLOAT(3) = getFormItemFloat(F("station[3]"));
        PCONFIG_FLOAT(4) = getFormItemFloat(F("station[4]"));
        PCONFIG_FLOAT(5) = getFormItemFloat(F("station[5]"));
        PCONFIG_FLOAT(6) = getFormItemFloat(F("station[6]"));
        PCONFIG_FLOAT(7) = getFormItemFloat(F("station[7]"));

        success = true;
        break;
      }
But during compilation I got such errors:

Code: Select all

 ^
/home/maciek/TMP/ESPEasy_mega-20190928/source/src/_P089_RadioFM.ino: In function 'boolean Plugin_089(byte, EventStruct*, String&)':
/home/maciek/TMP/ESPEasy_mega-20190928/source/src/_P089_RadioFM.ino:65:93: error: cannot convert 'const __FlashStringHelper*' to 'float' for argument '2' to 'void addFloatNumberBox(const String&, float, float, float)'
        addFloatNumberBox(F("Station-1"), F("station[0]"), PCONFIG_FLOAT(0), minFreq, maxFreq);
                                                                                             ^
/home/maciek/TMP/ESPEasy_mega-20190928/source/src/_P089_RadioFM.ino:67:93: error: cannot convert 'const __FlashStringHelper*' to 'float' for argument '2' to 'void addFloatNumberBox(const String&, float, float, float)'
        addFloatNumberBox(F("Station-2"), F("station[1]"), PCONFIG_FLOAT(1), minFreq, maxFreq);
                                                                                             ^
/home/maciek/TMP/ESPEasy_mega-20190928/source/src/_P089_RadioFM.ino:69:93: error: cannot convert 'const __FlashStringHelper*' to 'float' for argument '2' to 'void addFloatNumberBox(const String&, float, float, float)'
        addFloatNumberBox(F("Station-3"), F("station[2]"), PCONFIG_FLOAT(2), minFreq, maxFreq);
                                                                                             ^
/home/maciek/TMP/ESPEasy_mega-20190928/source/src/_P089_RadioFM.ino:71:93: error: cannot convert 'const __FlashStringHelper*' to 'float' for argument '2' to 'void addFloatNumberBox(const String&, float, float, float)'
        addFloatNumberBox(F("Station-4"), F("station[3]"), PCONFIG_FLOAT(3), minFreq, maxFreq);
                                                                                             ^
/home/maciek/TMP/ESPEasy_mega-20190928/source/src/_P089_RadioFM.ino:73:93: error: cannot convert 'const __FlashStringHelper*' to 'float' for argument '2' to 'void addFloatNumberBox(const String&, float, float, float)'
        addFloatNumberBox(F("Station-5"), F("station[4]"), PCONFIG_FLOAT(4), minFreq, maxFreq);
                                                                                             ^
/home/maciek/TMP/ESPEasy_mega-20190928/source/src/_P089_RadioFM.ino:75:93: error: cannot convert 'const __FlashStringHelper*' to 'float' for argument '2' to 'void addFloatNumberBox(const String&, float, float, float)'
        addFloatNumberBox(F("Station-6"), F("station[5]"), PCONFIG_FLOAT(5), minFreq, maxFreq);
                                                                                             ^
/home/maciek/TMP/ESPEasy_mega-20190928/source/src/_P089_RadioFM.ino:77:93: error: cannot convert 'const __FlashStringHelper*' to 'float' for argument '2' to 'void addFloatNumberBox(const String&, float, float, float)'
        addFloatNumberBox(F("Station-7"), F("station[6]"), PCONFIG_FLOAT(6), minFreq, maxFreq);
                                                                                             ^
/home/maciek/TMP/ESPEasy_mega-20190928/source/src/_P089_RadioFM.ino:79:93: error: cannot convert 'const __FlashStringHelper*' to 'float' for argument '2' to 'void addFloatNumberBox(const String&, float, float, float)'
        addFloatNumberBox(F("Station-8"), F("station[7]"), PCONFIG_FLOAT(7), minFreq, maxFreq);
                                                                                             ^
What I am doing wrong? As I can see in another .ino`s, all use "addTextBox" to enter floats. I really dont know why.
And next question, how many element can have PCONFIG_FLOAT array?

TD-er
Core team member
Posts: 9895
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Help to write my first .ino

#2 Post by TD-er » 26 Oct 2019, 21:46

I think you can better save the frequency as integer value.
Comparing floats is rather difficult (comparing if they are equal is difficult).
Also you know their range and resolution, so you can multiply by 100 and just subtract 8700 from it to store the value.
Showing the value as a float is OK.

I have to look at the code from my editor to see what's happening with your code to give more info on what is happening here.

The addHtml pieces can be changed to addUnit. That's what it is meant to be used for.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 19 guests