Hey all,
Just trying to figure out Const and Struct. The reason for this is that I want to convert a sample code to regular AVR C language for the AVR-GCC.
But I can't get it to work. The original code is the following, for the Arduino;
/* Analog readings corresponding to different sensor states */
struct input_ranges {
int sensor_state;
const String label;
int range_bottom;
int range_optimum;
int range_top;
};
const input_ranges ranges[] {
{ STATE_SHORT, "Shorted", 0, 0, 162 }, // 0
{ STATE_NORMAL, "Normal", 163, 326, 409 }, // 1
{ STATE_TAMPER, "Tamper", 410, 495, 551 }, // 2
{ STATE_ALARM, "Alarm", 552, 609, 641 }, // 3
{ STATE_ALARM_TAMPER, "Alarm+Tamper", 642, 675, 848 }, // 4
{ STATE_CUT, "Cut", 849, 1023, 1023 }, // 5
{ STATE_UNKNOWN, "Unknown", 1024, 1024, 1024 }, // 6. Range beyond analog in because this is never read
};
This code sample is from the Freetronics blog for a security shield.
When pasting that in AtmelStudio it says the following;
X1 - unknown type name 'String'
X2 - unknown type name 'input_ranges'
X3 - expected '=', ',', ';', 'asm' or 'attribute' before '{' token
I'm no professional in C programming, so I might very well be doing it wrong. But, I've had various success with replacing "String" for char so I did that here, seems to work ok.
But then the X2 error, I guess it should be any sort of type such as "char" or so, but I don't know which. It feels like it should "connect" to the struct above it somehow, as it uses the same name "input_ranges". Not sure if thats correct?
Then the last one is the most troublesome.. What kind of "function"/listing/array? or what its called, is this supposed to be?
{ SENSOR_STATE_SHORT, "Shorted", 0, 0, 162 }
It feels like an array, but as I learned some years ago.. Arrays aren't what I used to think of them, coming from other languages where it works quite a different way.. So ok, but it feels like some sort of an array.
And the errors tells me that it's now allowed inside the ranges - array? I guess its 7 arrays inside the "ranges" array, correct?
So is it possible to convert this to regular C and could someone point me in the direction or even tell me exactly which functions/types I need for this?
I know that Arduino has some additional functionality compared to just standard C, so I'm up for it if I need to include an additional function or so, just to get this to work.
But as it stands right now, I have no idea where to look further.. Please help if you can!
Thanks!