2020-08-08 02:02 PM
Hi guys, Hope some one can help me with this
I have defined an alarm struct of repeating struct memers that must manage various alarm massages and alarm audio signals.
I want to address these alarm's in the following way, for example
1) alarm.spray.stat
but also as
2) alarmlist[0].stat;
Both the struct and the array are in fact arrays of repeating alarm_t types and it should be possible to map them over eacht other having the same start address
For this I need to assign the alarm struct address to the alarmlist[] array address.
Tricky i know but on a 8bit controller like the ATmega 328 it should not be a problem. I also want to use this technique on STM32 controllers. How can i do this with out having conflicts at compile time.
I did try this :
alarmlist=(alarm_t)&alarm but that gave me a error.
typedef struct alarm_t {
char * msg;
uint8_t prio : 4;
uint8_t stat : 4;
uint8_t Buzzpattern;
uint8_t duration : 4;
uint8_t intermit : 4;
} alarm_t;
struct alarm_state_t {
alarm_t spray;
alarm_t stall;
alarm_t empty;
} alarm = { { },{ },{ } };
alarm_t alarmlist[];
2020-08-08 03:05 PM
alarm_t * alarmlist = alarm;
Actually this is a bit trickier since the struct could be padded.
This works but is probably compiler dependent:
typedef struct alarm_t {
char * msg;
uint8_t prio : 4;
uint8_t stat : 4;
uint8_t Buzzpattern;
uint8_t duration : 4;
uint8_t intermit : 4;
} alarm_t;
struct alarm_state_t {
alarm_t spray;
alarm_t stall;
alarm_t empty;
} alarm = { { },{ },{ } };
alarm_t * alarmlist = (alarm_t *) &alarm.spray;
void main() {
ASSERT_EQ((uint32_t) &alarmlist[0], (uint32_t) &alarm.spray);
ASSERT_EQ((uint32_t) &alarmlist[1], (uint32_t) &alarm.stall);
ASSERT_EQ((uint32_t) &alarmlist[2], (uint32_t) &alarm.empty);
}
2020-08-09 12:21 AM
The requirement is strange...
I would drop the structure thing and use an array with named constants for indexes:
enum alarm {
ALARM_SPRAY,
ALARM_STALL,
ALARM_EMPTY,
ALARM_NUMBER
};
alarm_t alarmlist[ALARM_NUMBER];
alarmlist[ALARM_SPRAY].stat = 1;
2020-08-09 03:20 AM
Pointers?
One of the great things about 32bit arm programming is how everything on the chip is accessible thru a flat 32bit address space. With C, what that means is there is no real difference between a pointer and a uint32_t. You are free to manipulate the pointers (unsigned math & sizeof) and cast and reference/dereference as you need.
2020-08-10 12:53 AM
Thanks Piranha, I agree that is possibility too and I will take it in considderation. however it's not an answer to my question. How to map an array of structs on a struct with a series of equal structs as used for the array
2020-08-10 12:54 AM
Sorry RMcCa but I think you missed the point, but thanks for responding