Error trying to use enum alignment {LEFT, RIGHT, CENTER};

Error trying to use enum alignment {LEFT, RIGHT, CENTER};
Please can you keep it simple as I am not very good it this.
the code is:

void drawString(int x, int y, String text, alignment align) {
int16_t x1, y1; //the bounds of x,y and w and h of the variable ‘text’ in pixels.
uint16_t w, h;
display.setTextWrap(false);
display.getTextBounds(text, x, y, &x1, &y1, &w, &h);
if (align == RIGHT) x = x - w;
if (align == CENTER) x = x - w / 2;
display.setCursor(x, y + h);
display.print(text);
}

And I get the following error:

src\main.cpp:49:44: error: ‘alignment’ has not been declared
void drawString(int x, int y, String text, alignment align);
^
src\main.cpp:50:68: error: ‘alignment’ has not been declared
void drawStringMaxWidth(int x, int y, int text_width, String text, alignment align);
^
*** [.pioenvs\esp32dev\src\main.cpp.o] Error 1

Thank you

Where did you declare the `enum alignment? Must be above the function, best to be at the start of the file.

1 Like

How do you declare the enum alignment, I have tried:
enum alignment {};
enum alignment {LEFT, RIGHT, CENTER};
enum alignment ();
enum alignment (LEFT, RIGHT, CENTER);

How do you do it?

Correct but where do you declare it? Show the full code file please.

1 Like

I tried to post all the code but it will not fit on the page.
Can you tell me where to put the declare?

/* ESP32 Weather Display using an EPD 4.2" Display, obtains data from Open Weather Map, decodes the weather data and then displays it.

####################################################################################################################################

This software, the ideas and concepts is Copyright (c) David Bird 2019. All rights to this software are reserved.

Any redistribution or reproduction of any part or all of the contents in any form is prohibited other than the following:

1. You may print or download to a local hard disk extracts for your personal and non-commercial use only.

2. You may copy the content to individual third parties for their personal use, but only if you acknowledge the author David Bird as the source of the material.

3. You may not, except with my express written permission, distribute or commercially exploit the content.

4. You may not transmit it or store it in any other website or other form of electronic retrieval system for commercial purposes.

The above copyright ('as annotated') notice and this permission notice shall be included in all copies or substantial portions of the Software and where the

software use is visible to an end-user.

THE SOFTWARE IS PROVIDED "AS IS" FOR PRIVATE USE ONLY, IT IS NOT FOR COMMERCIAL USE IN WHOLE OR PART OR CONCEPT. FOR PERSONAL USE IT IS SUPPLIED WITHOUT WARRANTY

OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.

IN NO EVENT SHALL THE AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING

FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

See more at http://www.dsbird.org.uk

*/

#include <Arduino.h>

#include "owm_credentials.h" // See 'owm_credentials' tab and enter your OWM API key and set the Wifi SSID and PASSWORD

#include "Sys_Variables.h"

#include "Void_S.h"

#include "Wx_Icons.h" // Weather Icons

#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson

#include <WiFi.h> // Built-in

#include "time.h" // Built-in

#include <SPI.h> // Built-in

#define ENABLE_GxEPD2_display 0

#include <GxEPD2_BW.h>

#include <GxEPD2_3C.h>

#include <Fonts/FreeMonoBold12pt7b.h>

#include "epaper_fonts.h"

**enum alignment {LEFT, RIGHT, CENTER};**

void addmoon (int x, int y, int scale);

int StartWiFi();

void SetupTime();

bool obtain_wx_data(String RequestType);

void StopWiFi();

void DisplayWeatherInfo();

void DisplaySystemStatus(int x, int y);

void begin_sleep();

void DisplayHeadingSection();

void DisplayTempSection(int x, int y);

void DisplayWxPerson(int x, int y, String IconName);

void DisplayWxIcon(int x, int y, String IconName, bool LargeSize);

void DisplayMainWeatherSection(int x, int y);

void DisplayForecastSection(int x, int y);

void DisplayAstronomySection(int x, int y);

void DisplayWindSection(int x, int y, float angle, float windspeed, int Cradius);

void drawString(int x, int y, String text, alignment align);

void drawStringMaxWidth(int x, int y, int text_width, String text, alignment align);

String TitleCase(String text);

String WindDegToDirection(float winddirection);

int JulianDate(int d, int m, int y);

void MostlySunny(int x, int y, bool LargeSize, String IconName);

void Cloudy(int x, int y, bool LargeSize, String IconName);

void ChanceRain(int x, int y, bool LargeSize, String IconName);

void Rain(int x, int y, bool LargeSize, String IconName);

void Tstorms(int x, int y, bool LargeSize, String IconName);

void Haze(int x, int y, bool LargeSize, String IconName);

void Fog(int x, int y, bool LargeSize, String IconName);

void Nodata(int x, int y, bool LargeSize);

bool DecodeWeather(String json, String Type);

void Convert_Readings_to_Imperial();

void UpdateLocalTime();

void ExpectRain(int x, int y, bool LargeSize, String IconName);

void Sunny(int x, int y, bool LargeSize, String IconName);

void MostlyCloudy(int x, int y, bool LargeSize, String IconName);

void DisplayRSSI(int x, int y);

void DisplayBattery(int x, int y);

void Snow(int x, int y, bool LargeSize, String IconName);

void DrawMoon(int x, int y, int dd, int mm, int yy, String hemisphere);

String MoonPhase(int d, int m, int y, String hemisphere);

void arrow(int x, int y, int asize, float aangle, int pwidth, int plength);

String ConvertUnixTime(int unix_time);

void DisplayForecastWeather(int x, int y, int index);

#define SCREEN_WIDTH 400.0 // Set for landscape mode, don't remove the decimal place!

#define SCREEN_HEIGHT 300.0 // Not used but included for completeness

**enum alignment {LEFT, RIGHT, CENTER};**

// pins_arduino.h, e.g. LOLIN32 LITE

static const uint8_t EPD_BUSY = 4;

static const uint8_t EPD_SS = 5;

static const uint8_t EPD_RST = 16;

static const uint8_t EPD_DC = 17;

static const uint8_t EPD_SCK = 18;

static const uint8_t EPD_MISO = 19; // Master-In Slave-Out not used, as no data from display

static const uint8_t EPD_MOSI = 23;

Since it’s so long… try putting the code in a code/pastebin sharing site… like pastebin.com, hastebin.com or 0bin.net and giving us the link to the pasted code.

But from the snippet you shared… it looks like you’ve defined it twice… above AND below the function prototypes/declarations? Get rid of the second one… it shouldn’t be defined twice.

Just as a simple outline, this is the order stuff should be in…

#include <Arduino.h>

enum alignment {LEFT, CENTER, RIGHT};

void displayStuff(String text, alignment align);

void setup() {
  // set stuff up
}

void loop() {
  // do stuff

  //write something to the screen
  displayStuff("stuff", CENTER);

  delay(1000);
}

void displayStuff(String text, alignment align)
{
  int x = 0;
  int y = 30;

  if (align == CENTER) {
    x = 120;
  } else if (align == LEFT) {
    x = 0;
  }

  //tft.setCursor(x,y);
  //tft.print(text)
}