In my STM32 code i have an
#define USART1 ((USART_TypeDef *) USART1_BASE)
and i would like to have
switch((uint32_t)ptr)
{
case USART1: return 1;
(...)
But gcc-arm (6.3.1) give me an error
error: reinterpret_cast from integer to pointer
I found an information that i can use
case __builtin_constant_p(USART1): return 0;
but this is only gcc solution? Is there something more generic?
There is UART1_BASE which is just the sum of a couple of unsigned integer constants. See the machine header:
#define PERIPH_BASE 0x40000000U
#define APB2PERIPH_BASE (PERIPH_BASE + 0x00010000U)
#define USART1_BASE (APB2PERIPH_BASE + 0x1000U)
So should be able to use
switch((unsigned int)ptr) {
case USART1_BASE: return 1;
}
You forgot to cast pointer to integer in the case statement as well.
Example from my working code:
switch ((uint32_t)gpio) {
case (uint32_t)GPIOA:
EXTI_cfgr = 0b0000;
break;
case (uint32_t)GPIOB:
EXTI_cfgr = 0b0001;
break;
case (uint32_t)GPIOC:
EXTI_cfgr = 0b0010;
break;
default:
break;
}
USART1_BASE is a number in the header files of the STM32 development environment. You can compile your code when you decided what type shall be used in the switch. I recommend uint32_t:
switch((uint32_t)ptr)
{
case USART1_BASE: return 1;
(...)
If you like more readability you can try to remove the cast from the switch:
uint32_t ptr_value = (uint32_t)ptr;
switch(ptr_value)
{
case USART1_BASE: return 1;
(...)
Related
i am trying to figure out a way in esp32 to have (a list) for lack of better terms, that i could call upon as a variable input. I have a void playwav(); function. I want to use a global variable to define the file that should be played. ie playwav('alarm'). I had a case switch setup but was using the a b c method. As my code is growing (next step is going to add 71 new wav files), i am quickly realizing that method will no longer work. I have been told to do an array but for the life of me no matter what ive read i just cant grasp how to do it nor call upon the files by name. Does anyone have advice on what i should do? Any help is much appreciated.
This is what i currently have.
void playWAV (char effect, int bg1248_general_volume)
{
Serial.println("Entered playWAV");
switch (effect)
{
case 'a':
file = new AudioFileSourceSD("/wav/alarm.wav");
break;
case 'b':
file = new AudioFileSourceSD("/wav/alarmhigh.wav");
break;
case 'c':
file = new AudioFileSourceSD("/wav/alarmlow.wav");
break;
case 'd':
file = new AudioFileSourceSD("/wav/error.wav");
break;
case 'e':
file = new AudioFileSourceSD("/wav/error1.wav");
break;
case 'f':
file = new AudioFileSourceSD("/wav/noread.wav");
break;
case 'g':
file = new AudioFileSourceSD("/wav/noreadings.wav");
break;
case 'h':
file = new AudioFileSourceSD("/wav/normalrange.wav");
break;
case 'i':
file = new AudioFileSourceSD("/wav/warning.wav");
break;
case 'j':
file = new AudioFileSourceSD("/wav/warninghigh.wav");
break;
case 'k':
file = new AudioFileSourceSD("/wav/warninglow.wav");
break;
case 'l':
file = new AudioFileSourceSD("/wav/startup.wav");
break;
case 'm':
file = new AudioFileSourceSD("/wav/startup.wav");
break;
case 'n':
file = new AudioFileSourceSD("/wav/update.wav");
break;
case 'o':
file = new AudioFileSourceSD("/wav/startup_dev.wav");
}
float volumeGain = ((float)bg1248_general_volume / 100.0) * 39.0;
Serial.print("volumeGain:");
Serial.println(volumeGain);
id3 = new AudioFileSourceID3(file);
out = new AudioOutputI2S(0, 0); // Output to builtInDAC
out->SetPinout(12, 0, 2);
out->SetOutputModeMono(true);
out->SetGain(volumeGain);
wav = new AudioGeneratorWAV();
wav->begin(id3, out);
However because I used the alphabet im going to run out of cases very very soon. I want to be able to call for a specific wav file anywhere in the project preferably like playWAV(filename) or something similar.
I call the function(file) like this (example condition)
if((ns->sensSgv<=cfg.snd_alarm) && (ns->sensSgv>=0.1)) {
// red alarm state
// M5.Lcd.fillRect(110, 220, 100, 20, TFT_RED);
Serial.println("ALARM LOW");
M5.Lcd.fillRect(0, 220, 320, 20, TFT_RED);
M5.Lcd.setTextColor(TFT_BLACK, TFT_RED);
int stw=M5.Lcd.textWidth(tmpStr);
M5.Lcd.drawString(tmpStr, 159-stw/2, 220, GFXFF);
if( (alarmDifSec>cfg.alarm_repeat*60) && (snoozeRemaining<=0) ) {
playWAV('a', 100);
'a'=case 100 = volume.
Alan, this is what I currently had in my enum, i was following a tutorial but doubt i did it right. even if i did i was getting errors saying i had duplicate cases when i don't.
EDIT:
The condition is set using this:
if((ns->sensSgv<=cfg.snd_alarm) && (ns->sensSgv>=0.1))
and if you notice at the bottom there is:
if( (alarmDifSec>cfg.alarm_repeat*60) && (snoozeRemaining<=0) ) {
playWAV('a', 100);
The a corelates to the case A in the switch case. shown above. currently all the places that use the playWAV function are manually specified.
My understanding was using the alphabets in switch case will ran out soon because of large no. of files (>26). So, I implemented the logic using enum first and below is the complete code.
#include<stdio.h>
enum soundEffects {
alarm,
alarm_high,
alarm_low,
Dev_startup,
error,
error1,
noread,
noreadings,
normalrange,
startup,
startup_dev,
update,
warning,
warning_high,
warning_low,
};
void playWav (enum soundEffects effect, int paci_volume)
{
//Serial.println("Entered paciplay_day");
//printf("Entered paciplay_day\n");
switch (effect)
{
case alarm:
//file = new AudioFileSourceSD("/wav/alarm.wav");
printf("/wav/alarm.wav\n");
break;
case alarm_high:
//file = new AudioFileSourceSD("/wav/alarmhigh.wav");
printf("/wav/alarmhigh.wav\n");
break;
case alarm_low:
//file = new AudioFileSourceSD("/wav/alarmlow.wav");
printf("/wav/alarmlow.wav\n");
break;
case error:
//file = new AudioFileSourceSD("/wav/error.wav");
printf("/wav/error.wav\n");
break;
case error1:
//file = new AudioFileSourceSD("/wav/error1.wav");
printf("/wav/error1.wav\n");
break;
case noread:
//file = new AudioFileSourceSD("/wav/noread.wav");
printf("/wav/noread.wav\n");
break;
case noreadings:
//file = new AudioFileSourceSD("/wav/noreadings.wav");
printf("/wav/noreadings.wav\n");
break;
case normalrange:
//file = new AudioFileSourceSD("/wav/normalrange.wav");
printf("/wav/normalrange.wav\n");
break;
case warning:
//file = new AudioFileSourceSD("/wav/warning.wav");
printf("/wav/warning.wav\n");
break;
case warning_high:
//file = new AudioFileSourceSD("/wav/warninghigh.wav");
printf("/wav/warninghigh.wav\n");
break;
case warning_low:
//file = new AudioFileSourceSD("/wav/warninglow.wav");
printf("/wav/warninglow.wav\n");
break;
case startup:
//file = new AudioFileSourceSD("/wav/startup.wav");
printf("/wav/startup.wav\n");
break;
case update:
//file = new AudioFileSourceSD("/wav/update.wav");
printf("/wav/update.wav\n");
break;
}
}
int main()
{
//below are the 3 ways of calling your playWav() function
//1 - using direct numbers
//this will throw compilation error when compiled using cpp compilers
for (int i = 0; i<14; i++)
{
printf("enum value: %d\n", i);
playWav(i, 50);
}
//2 - directly calling using the enum
playWav(startup, 100);
//3 - Just another way of calling
enum soundEffects sound;
sound = error;
playWav(sound, 100);
return 0;
}
Also, I implemented this same logic using double array's. Please do find it below. This uses the name of the file as a parameter so altogether we can eliminate the use of switch statement.
#include<stdio.h>
void playWav (const char *filename, int paci_volume)
{
//Serial.println("Entered paciplay_day");
printf("Entered paciplay_day\n");
//file = new AudioFileSourceSD("/wav/update.wav");
printf("/wav/%s\n", filename);
}
int main()
{
const char list_of_files[][25] = { "startup.wav", "update.wav", "warning.wav", "alarm.wav" };
for (int i = 0; i<4; i++)
{
printf("name: %s\n", list_of_files[i]);
playWav(list_of_files[i], 50);
}
return 0;
}
Please omit my printf statements which I used for debugging. I hope this is useful.
I'm struggling to understand what is happening in part of an Arduino program I wrote, I copied the switch in processInput from another post about reading data from serial ports but have changed it a bit since then.
To me it seems like the output to the LCD screen should always be 0 since recievedNumber is set to 0 at the beginning of the function, it does output the data from the serial port correctly though. I'm also not sure what is happening on the line "receivedNumber += b - '0';" inside the last case.
Any advice or a breakdown of the logic would be appreciated, I intended to replace the last case with an if statement to learn how to do it without using the GNU extension but I just don't understand what is happening.
An example of data in the serial port:
<45,56><55,54>
Sorry if this is the wrong place but it felt too related to programming to post on the Arduino StackExchage.
const char startOfNumberDelimiter = '<';
const char middleOfNumbersDelimiter = ',';
const char endOfNumberDelimiter = '>';
void processNumberC (const long c)
{
lcd.setCursor(10,0);
lcd.print(c);
lcd.print("C");
}
void processNumberG (const long g)
{
lcd.setCursor(10,1);
lcd.print(g);
lcd.print("C");
}
void processInput ()
{
static long receivedNumber = 0;
byte b = Serial.read ();
switch (b)
{
case middleOfNumbersDelimiter:
processNumberC (receivedNumber);
receivedNumber = 0;
break;
case endOfNumberDelimiter:
processNumberG (receivedNumber);
break;
case startOfNumberDelimiter:
receivedNumber = 0;
break;
case '0' ... '9':
receivedNumber *= 10;
receivedNumber += b - '0';
break;
} // end of switch
} // end of processInput
void loop()
{
while (Serial.available ())
processInput ();
}
I have the following switch/case statement in Arduino 1.8.7 where the variable led is an integer:
switch (led) {
case ALL: {
/* do stuff */
break;
}
case LED1: {
/* do stuff */
break;
}
case LED2: {
/* do stuff */
break;
}
case LED3: {
/* do stuff */
break;
}
case LED4: {
/* do stuff */
break;
}
default: {
break;
}
}
I also have the following enum:
enum LED_References_e
{
ALL = 0,
LED1 = 1,
LED2 = 2,
LED3 = 3,
LED4 = 4
};
When using the enumerated values as cases to the statement, the statement always hits the default clause. If I substitute the enumerated values for the integers that they represent (i.e.case 0: ... case 1: ...) then the statement functions as expected.
I have tried, when using the enumerated values within the statement, to reference the enumerator as the value that the switch is performed on:
switch ((LED_References_e)led)
But this also defaults every time.
I am using another enumerator within my program and this functions correctly, however it is conditionally tested using if/else as opposed to switch/case.
My question is twofold:
Why does the switch/case statement seemingly not work with enumerated values?
What fundamental difference am I missing between if/else and switch/case?
Assuming Max Langhof is correct and there are other names ALL, LED1, etc... in scope at the switch so that the LED_References_e ones are shadowed, this should help:
I'm not 100% certain about the differences between standard C++ and Arduino C++, but you should be able to do the following:
enum LED_References_e
{
ALL = 0,
LED1 = 1,
LED2 = 2,
LED3 = 3,
LED4 = 4
};
switch (led) {
case LED_References_e::ALL: {
/* do stuff */
break;
}
case LED_References_e::LED1: {
/* do stuff */
break;
}
case LED_References_e::LED2: {
/* do stuff */
break;
}
case LED_References_e::LED3: {
/* do stuff */
break;
}
case LED_References_e::LED4: {
/* do stuff */
break;
}
default: {
break;
}
}
What this does is it tells the compiler you explicitly want LED1...LED4 from the LED_References_e enum.
If there are other LEDxes in the same scope, this should disambiguate.
I am wondering if I made a good decision when I defined all states of my enum to their shorter counterparts: just to tidy up the code.Code:Enum:
enum class ESelectedCharacterState : uint8
{
SS_WantsWalk,
SS_WantsJog,
SS_WantsCrouch,
SS_WantsProne,
SS_WantsJump
};
Defining:
#define WantsWalk ESelectedCharacterState::SS_WantsWalk
#define WantsJog ESelectedCharacterState::SS_WantsJog
#define WantsCrouch ESelectedCharacterState::SS_WantsCrouch
#define WantsProne ESelectedCharacterState::SS_WantsProne
#define WantsJump ESelectedCharacterState::SS_WantsJump
Case with no #defined enum states:
switch (StateSelected)
{
case ESelectedCharacterState::SS_WantsWalk:
break;
case ESelectedCharacterState::SS_WantsJog:
break;
case ESelectedCharacterState::SS_WantsCrouch:
break;
case ESelectedCharacterState::SS_WantsProne:
break;
case ESelectedCharacterState::SS_WantsJump:
break;
default:
break;
}
Case with #defined enum states:
switch (StateSelected)
{
case WantsWalk:
break;
case WantsJog:
break;
case WantsCrouch:
break;
case WantsProne:
break;
case WantsJump:
break;
default:
break;
}
This is actually a small bit of code but I use this enum very frequently in my project.
Using a typedef would be a cleaner way to achieve this, by making your enum's type shorter.
typedef ESelectedCharacterState ESCS;
I have seen this and I have corrected my code:
int solutionChooser = m_configFile.getChosenSolution();
ISolution* currentSolution;
switch (solutionChooser)
{
case 1:
{
currentSolution = new Solution1());
break;
}
case 2:
{
currentSolution = new Solution2());
break;
}
case 3:
{
currentSolution = new Solution3());
break;
}
case 4:
{
currentSolution = new Solution4());
break;
}
default:
{
std::cout << "The specified solution does not exists\n";
return;
}
}
using unique_ptr as:
int solutionChooser = m_configFile.getChosenSolution();
std::unique_ptr<ISolution> currentSolution;
switch (solutionChooser)
{
case 1:
{
currentSolution.reset(new Solution1());
break;
}
case 2:
{
currentSolution.reset(new Solution2());
break;
}
case 3:
{
currentSolution.reset(new Solution3());
break;
}
case 4:
{
currentSolution.reset(new Solution4());
break;
}
default:
{
currentSolution = std::move(nullptr); // here is the error
std::cout << "The specified solution does not exists\n";
return;
}
}
and now I am getting the error below:
error: no match for ‘operator=’ (operand types are ‘std::unique_ptr<ISolution>’ and ‘std::remove_reference<long int>::type {aka long int}’)
I have ISolution as interface and the SolutionX are classes derived from ISolution
How to fix this? What am I doing wrong?
std::unique_ptr has deleted operator=, that is why you can not use it.
To reset the std::unique_ptr, use reset() method :
currentSolution.reset(nullptr);
but you do not have to do it, since the initial value is nullptr anyway.
Your compiler is wrong, by n3376 std::unique_ptr should have following overloading
unique_ptr& operator=(nullptr_t) noexcept;
so, your code should work fine.
I can't add comments so I'll just post my idea about the answer..
I believe your problem is the copy constructor. The operator = is not defined with unique_ptr so instead you'll have to use a move constructor. I don't remember the correct syntax but it should be something similar to:
/* Header file */
std::unique_ptr<ISolution> currentSolution;
/* CPP file */
std::unique_ptr<ISolution> currentSolution2(new ISolution);
currentSolution = std::move(currentSolution2);
There's probably some mistakes here but hopefully it can get you to the right track. If you want a working example, I got one on floobits, user Simple2012.
Link here: https://floobits.com/Simple2012/Laboration_SearchMethods
Check arr.h and arr.cpp for a concrete example, however I'm using an array there instead of a class, not much difference though.