Ну так UB же! В общем случае нельзя enum'ом, если значение за пределы enum'а может выходить — мало ли какой компилятор попадется... Надежней дефайнами.
Я на гитхабе, в ЖЖ
Код: Выделить всё
#define DIRECTION_NORD 0
#define DIRECTION_EAST 1
#define DIRECTION_SOUTH 2
#define DIRECTION_WEST 3
typedef struct directions_st {
uint8_t direction:2;
} directions_t;
directions_t rotate(directions_t dir, int8_t angle){
dir.direction+=angle;
return dir;
}
Код: Выделить всё
#define DIRECTION_NORD 0
#define DIRECTION_EAST 1
#define DIRECTION_SOUTH 2
#define DIRECTION_WEST 3
#define DIRECTION_STEP_LEFT(d) d.direction--
#define DIRECTION_STEP_RIGHT(d) d.direction++
typedef struct directions_st {
uint8_t direction:2;
} directions_t;
Код: Выделить всё
typedef enum { north, east, south, west } Dir;
Dir dir = 555;
dir /= 111;
Код: Выделить всё
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb -DUSE_FULL_LL_DRIVER -DSTM32F103xB -D_STM32F1 -D_ILI9341 -D_DISP_SPI -D_DISP_320x240 -D_TDA7439 -D_TDA731X -D_PT232X -D_TDA7418 -D_RDA580X -D_SI470X -D_TEA5767 -Idisplay -Iusb -Idrivers/STM32F1xx_HAL_Driver/Inc -Idrivers/STM32_USB_Device_Library/Core/Inc -Idrivers/CMSIS/Device/ST/STM32F1xx/Include -Idrivers/CMSIS/Include -Isystem -Os -fshort-enums -ffunction-sections -fdata-sections -ffreestanding -Wall -Werror -g -gdwarf-2 -MMD -MP -MT build/main.o -MF build/./main.d -DUSE_FULL_LL_DRIVER -DSTM32F103xB -D_STM32F1 -D_ILI9341 -D_DISP_SPI -D_DISP_320x240 -D_TDA7439 -D_TDA731X -D_PT232X -D_TDA7418 -D_RDA580X -D_SI470X -D_TEA5767 -o build/main.o main.c
main.c: In function 'main':
main.c:112:15: error: unsigned conversion from 'int' to 'Dir' {aka 'enum <anonymous>'} changes value from '555' to '43' [-Werror=overflow]
Dir dir = 555;
^~~
cc1: all warnings being treated as errorsКод: Выделить всё
typedef enum {
straight = 0,
right = 1,
back = 2,
left = 3
} t_dir;
#define north straight
#define east right
#define south back
#define west leftКод: Выделить всё
move_direction += turn_direction;
move_direction &= 0x03;Код: Выделить всё
month_t operator ++( month_t &id, int )
{
month_t current = id;
if ( december < id + 1 ) id = january;
else id = static_cast<month_t>( id + 1 );
return ( current );
}Код: Выделить всё
typedef enum {
ROTATION_NONE = 0,
ROTATION_RIGHT = 1,
ROTATION_BACKWARD = 2,
ROTATION_LEFT = 3
} rotation_dir_t;
typedef enum {
BEARING_NORTH = 0,
BEARING_EAST = 1,
BEARING_SOUTH = 2,
BEARING_WEST = 3
} bearing_dir_t;
bearing_dir_t compute_bearing(bearing_dir_t current_bearing, rotation_dir_t rotation){
return (bearing_dir_t)(current_bearing + rotation) & 0x03;
}Код: Выделить всё
*** Using Compiler 'V5.06 update 6 (build 750)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin'
compiling Maze.c...
Maze.c(57): warning: #188-D: enumerated type mixed with another type
return (bearing_dir_t)(current_bearing + rotation) & 0x03;
Maze.c: 1 warning, 0 errors
"Maze.c" - 0 Error(s), 1 Warning(s).Код: Выделить всё
bearing_dir_t compute_bearing(bearing_dir_t current_bearing, rotation_dir_t rotation){
bearing_dir_t next_bearing;
next_bearing = (bearing_dir_t)(current_bearing + rotation);
return next_bearing &= 0x03;
}