Specifically I am working with an MPU6050. Part of the operation is to define the range of the gyroscope. There are four values 250, 500 1,000 and 2,000.
enum eGyroscope {
Gyro250,
Gyro500,
Gyro1000,
Gyro2000,
MaskGyro
};
uint8_t getRange(eGyroscope in) {
switch (in) {
case Gyro250: return 0x00;
case Gyro500: return 0x08;
case Gyro1000: return 0x10;
case Gyro2000: return 0x18;
case MaskGyro: return 0x18;
}
return 0;
}
uint8_t getDivisor(eGyroscope in) {
switch (in) {
case Gyro250: return 131;
case Gyro500: return 132;
case Gyro1000: return 133;
case Gyro2000: return 134;
case MaskGyro: return 135;
}
return 0;
}
void setup() {
Serial.begin(115200);
Serial.println(getDivisor(Gyro250), HEX);
Serial.println(getRange(Gyro250));
}
void loop() {
// put your main code here, to run repeatedly:
}
//
when you re-arange the order in the enum the code will still work
when you delete an entry in the enumeration and forget to edit the switch cases the compiler will give an error
when you add a new entry (or insert a new one within the existing values) and you miss to add the new value in the switch case the compiler will at least warn you that you missed a case (... when you have activated the warnings - what you should do anyway).