diff --git a/example/src/header_logger.c b/example/src/header_logger.c index 93c17dd..8fe1455 100644 --- a/example/src/header_logger.c +++ b/example/src/header_logger.c @@ -15,6 +15,133 @@ void handle_error(const char *msg) { exit(EXIT_FAILURE); } +bool log_command(struct Command command) { + switch (command.tag) { + case COMMAND_TAG_INVALID: { + printf("-> this is an invalid command\n"); + break; + } + case COMMAND_TAG_HARD_RESET: { + printf("-> HardReset command - exiting\n"); + return true; + } + case COMMAND_TAG_BITMAP: { + BitmapCommand *bitmapCommand = command.data.bitmap; + + CompressionCode compression = sp_cmd_bitmap_get_compression(bitmapCommand); + + size_t x, y; + sp_cmd_bitmap_get_origin(bitmapCommand, &x, &y); + + Bitmap *bitmap = sp_cmd_bitmap_get(bitmapCommand); + size_t w = sp_bitmap_width(bitmap); + size_t h = sp_bitmap_height(bitmap); + + printf("-> BitmapCommand with params: x=%zu, y=%zu, w=%zu, h=%zu, compression=%hu\n", + x, y, w, h, compression); + break; + } + case COMMAND_TAG_BRIGHTNESS_GRID: { + BrightnessGridCommand *gridCommand = command.data.brightness_grid; + + size_t x, y; + sp_cmd_brightness_grid_get_origin(gridCommand, &x, &y); + + BrightnessGrid *grid = sp_cmd_brightness_grid_get(gridCommand); + size_t w = sp_brightness_grid_width(grid); + size_t h = sp_brightness_grid_height(grid); + + printf("-> BrightnessGridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", + x, y, w, h); + break; + } + case COMMAND_TAG_CHAR_GRID: { + CharGridCommand *gridCommand = command.data.char_grid; + + size_t x, y; + sp_cmd_char_grid_get_origin(gridCommand, &x, &y); + + CharGrid *grid = sp_cmd_char_grid_get(gridCommand); + size_t w = sp_char_grid_width(grid); + size_t h = sp_char_grid_height(grid); + + printf("-> CharGridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", + x, y, w, h); + break; + } + case COMMAND_TAG_CP437_GRID: { + Cp437GridCommand *gridCommand = command.data.cp437_grid; + + size_t x, y; + sp_cmd_cp437_grid_get_origin(gridCommand, &x, &y); + + Cp437Grid *grid = sp_cmd_cp437_grid_get(gridCommand); + size_t w = sp_cp437_grid_width(grid); + size_t h = sp_cp437_grid_height(grid); + + printf("-> Cp437GridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", + x, y, w, h); + break; + } + case COMMAND_TAG_BIT_VEC: { + BitVecCommand *bitvecCommand = command.data.bitvec; + + size_t offset = sp_cmd_bitvec_get_offset(bitvecCommand); + CompressionCode compression = sp_cmd_bitvec_get_compression(bitvecCommand); + + BinaryOperation operation = sp_cmd_bitvec_get_operation(bitvecCommand); + char *operationText; + switch (operation) { + case BINARY_OPERATION_AND: + operationText = "and"; + break; + case BINARY_OPERATION_OR: + operationText = "or"; + break; + case BINARY_OPERATION_XOR: + operationText = "xor"; + break; + case BINARY_OPERATION_OVERWRITE: + operationText ="overwrite"; + break; + default: + operationText ="unknown"; + break; + } + + BitVec *bitvec = sp_cmd_bitvec_get(bitvecCommand); + size_t len = sp_bitvec_len(bitvec); + + printf("-> BitVecCommand with params: offset=%zu, length=%zu, compression=%hu, operation=%s\n", + offset, len, compression, operationText); + break; + } + case COMMAND_TAG_CLEAR: { + printf("-> ClearCommand\n"); + break; + } + case COMMAND_TAG_BITMAP_LEGACY: { + printf("-> BitmapLinearLegacy\n"); + break; + } + case COMMAND_TAG_FADE_OUT:{ + printf("-> FadeOutCommand\n"); + break; + } + case COMMAND_TAG_GLOBAL_BRIGHTNESS: { + Brightness brightness = sp_cmd_brightness_global_get(command.data.global_brightness); + printf("-> GlobalBrightnessCommand with params: brightness=%hu\n", brightness); + break; + } + default: { + printf("-> unknown command tag %d\n", command.tag); + break; + } + } + + return false; +} + int main(int argc, char **argv) { //init_env_logger(); @@ -72,129 +199,7 @@ int main(int argc, char **argv) { payload.start, payload.length); struct Command command = sp_cmd_generic_try_from_packet(packet); - switch (command.tag) { - case COMMAND_TAG_INVALID: { - printf("-> this is an invalid command\n"); - break; - } - case COMMAND_TAG_HARD_RESET: { - printf("-> HardReset command - exiting\n"); - done = true; - break; - } - case COMMAND_TAG_BITMAP: { - BitmapCommand *bitmapCommand = command.data.bitmap; - - CompressionCode compression = sp_cmd_bitmap_get_compression(bitmapCommand); - - size_t x, y; - sp_cmd_bitmap_get_origin(bitmapCommand, &x, &y); - - Bitmap *bitmap = sp_cmd_bitmap_get(bitmapCommand); - size_t w = sp_bitmap_width(bitmap); - size_t h = sp_bitmap_height(bitmap); - - printf("-> BitmapCommand with params: x=%zu, y=%zu, w=%zu, h=%zu, compression=%hu\n", - x, y, w, h, compression); - break; - } - case COMMAND_TAG_BRIGHTNESS_GRID: { - BrightnessGridCommand *gridCommand = command.data.brightness_grid; - - size_t x, y; - sp_cmd_brightness_grid_get_origin(gridCommand, &x, &y); - - BrightnessGrid *grid = sp_cmd_brightness_grid_get(gridCommand); - size_t w = sp_brightness_grid_width(grid); - size_t h = sp_brightness_grid_height(grid); - - printf("-> BrightnessGridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", - x, y, w, h); - break; - } - case COMMAND_TAG_CHAR_GRID: { - CharGridCommand *gridCommand = command.data.char_grid; - - size_t x, y; - sp_cmd_char_grid_get_origin(gridCommand, &x, &y); - - CharGrid *grid = sp_cmd_char_grid_get(gridCommand); - size_t w = sp_char_grid_width(grid); - size_t h = sp_char_grid_height(grid); - - printf("-> CharGridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", - x, y, w, h); - break; - } - case COMMAND_TAG_CP437_GRID: { - Cp437GridCommand *gridCommand = command.data.cp437_grid; - - size_t x, y; - sp_cmd_cp437_grid_get_origin(gridCommand, &x, &y); - - Cp437Grid *grid = sp_cmd_cp437_grid_get(gridCommand); - size_t w = sp_cp437_grid_width(grid); - size_t h = sp_cp437_grid_height(grid); - - printf("-> Cp437GridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", - x, y, w, h); - break; - } - case COMMAND_TAG_BIT_VEC: { - BitVecCommand *bitvecCommand = command.data.bitvec; - - size_t offset = sp_cmd_bitvec_get_offset(bitvecCommand); - CompressionCode compression = sp_cmd_bitvec_get_compression(bitvecCommand); - - BinaryOperation operation = sp_cmd_bitvec_get_operation(bitvecCommand); - char *operationText; - switch (operation) { - case BINARY_OPERATION_AND: - operationText = "and"; - break; - case BINARY_OPERATION_OR: - operationText = "or"; - break; - case BINARY_OPERATION_XOR: - operationText = "xor"; - break; - case BINARY_OPERATION_OVERWRITE: - operationText ="overwrite"; - break; - default: - operationText ="unknown"; - break; - } - - BitVec *bitvec = sp_cmd_bitvec_get(bitvecCommand); - size_t len = sp_bitvec_len(bitvec); - - printf("-> BitVecCommand with params: offset=%zu, length=%zu, compression=%hu, operation=%s\n", - offset, len, compression, operationText); - break; - } - case COMMAND_TAG_CLEAR: { - printf("-> ClearCommand\n"); - break; - } - case COMMAND_TAG_BITMAP_LEGACY: { - printf("-> BitmapLinearLegacy\n"); - break; - } - case COMMAND_TAG_FADE_OUT:{ - printf("-> FadeOutCommand\n"); - break; - } - case COMMAND_TAG_GLOBAL_BRIGHTNESS: { - Brightness brightness = sp_cmd_brightness_global_get(command.data.global_brightness); - printf("-> GlobalBrightnessCommand with params: brightness=%hu\n", brightness); - break; - } - default: { - printf("-> unknown command tag %d\n", command.tag); - break; - } - } + done = log_command(command); sp_cmd_generic_free(command); }