diff --git a/examples/lvgl/lvgl_driver.c b/examples/lvgl/lvgl_driver.c index 231df62f5..12d0785cb 100644 --- a/examples/lvgl/lvgl_driver.c +++ b/examples/lvgl/lvgl_driver.c @@ -58,12 +58,12 @@ static uint32_t tick_cb(void) { int lvgl_driver_init(int buffer_lines) { uint32_t width, height; - int error = libtock_screen_get_resolution(&width, &height); + int error = libtocksync_screen_get_resolution(&width, &height); if (error != RETURNCODE_SUCCESS) return error; uint32_t buffer_size = width * buffer_lines * PIXEL_SIZE; uint8_t* buffer = NULL; - error = libtock_screen_buffer_init(buffer_size, &buffer); + error = libtocksync_screen_buffer_init(buffer_size, &buffer); if (error != RETURNCODE_SUCCESS) return error; /* initialize littlevgl */ diff --git a/examples/tests/screen-tock/main.c b/examples/tests/screen-tock/main.c index 70fbc5eaa..05958bdaf 100644 --- a/examples/tests/screen-tock/main.c +++ b/examples/tests/screen-tock/main.c @@ -12,7 +12,7 @@ int main(void) { printf("[SCREEN-TOCK] Test Screen Display\n"); uint32_t width, height; - libtock_screen_get_resolution(&width, &height); + libtocksync_screen_get_resolution(&width, &height); libtocksync_screen_set_brightness(32000); diff --git a/examples/tests/screen/screen_init/main.c b/examples/tests/screen/screen_init/main.c index 289d4a1f9..0633fa521 100644 --- a/examples/tests/screen/screen_init/main.c +++ b/examples/tests/screen/screen_init/main.c @@ -12,26 +12,26 @@ int main(void) { printf("available resolutions\n"); uint32_t resolutions; - err = libtock_screen_get_supported_resolutions(&resolutions); + err = libtocksync_screen_get_supported_resolutions(&resolutions); if (err < 0) return -1; for (uint32_t idx = 0; idx < resolutions; idx++) { uint32_t width, height; - libtock_screen_get_supported_resolution(idx, &width, &height); + libtocksync_screen_get_supported_resolution(idx, &width, &height); printf(" %ld x %ld\n", width, height); } printf("available colors depths\n"); uint32_t pixel_format; - err = libtock_screen_get_supported_pixel_formats(&pixel_format); + err = libtocksync_screen_get_supported_pixel_formats(&pixel_format); if (err < 0) return -1; for (uint32_t idx = 0; idx < pixel_format; idx++) { libtock_screen_format_t format; - libtock_screen_get_supported_pixel_format(idx, &format); - int bits = libtock_screen_get_bits_per_pixel(format); + libtocksync_screen_get_supported_pixel_format(idx, &format); + int bits = libtocksync_screen_get_bits_per_pixel(format); printf(" %d bpp\n", bits); } - err = libtock_screen_buffer_init(BUFFER_SIZE, &buffer); + err = libtocksync_screen_buffer_init(BUFFER_SIZE, &buffer); if (err < 0) { printf("buffer allocation failed\n"); return -1; @@ -40,7 +40,7 @@ int main(void) { printf("screen init\n"); libtocksync_screen_set_brightness(100); uint32_t width, height; - libtock_screen_get_resolution(&width, &height); + libtocksync_screen_get_resolution(&width, &height); libtocksync_screen_set_frame(0, 0, width, height); libtocksync_screen_fill(buffer, BUFFER_SIZE, 0); bool invert = false; diff --git a/examples/tests/screen/sierpinski_carpet/main.c b/examples/tests/screen/sierpinski_carpet/main.c index 67dad4ca9..f2a0a802f 100644 --- a/examples/tests/screen/sierpinski_carpet/main.c +++ b/examples/tests/screen/sierpinski_carpet/main.c @@ -42,7 +42,7 @@ int main(void) { uint32_t width, height; - err = libtock_screen_get_resolution(&width, &height); + err = libtocksync_screen_get_resolution(&width, &height); if (err != RETURNCODE_SUCCESS) return -1; // Make width smaller of width and height. diff --git a/libtock-sync/display/screen.c b/libtock-sync/display/screen.c index d27e1bba3..1158e7a14 100644 --- a/libtock-sync/display/screen.c +++ b/libtock-sync/display/screen.c @@ -1,173 +1,155 @@ +#include +#include #include #include "screen.h" -struct screen_done { - returncode_t ret; - bool fired; -}; -struct screen_format { - libtock_screen_format_t format; - returncode_t ret; - bool fired; -}; -struct screen_rotation { - int rotation; - returncode_t ret; - bool fired; -}; +static returncode_t screen_set_color(uint8_t* buffer, int buffer_len, int position, size_t color) { + // TODO color mode + if (position < buffer_len - 2) { + buffer[position * 2] = (color >> 8) & 0xFF; + buffer[position * 2 + 1] = color & 0xFF; + return RETURNCODE_SUCCESS; + } else { + return RETURNCODE_ESIZE; + } +} -static struct screen_done result; -static struct screen_format result_format; -static struct screen_rotation result_rotation; +bool libtocksync_screen_exists(void) { + return libtock_screen_driver_exists(); +} -static void screen_cb_done(returncode_t ret) { - result.ret = ret; - result.fired = true; +statuscode_t libtocksync_screen_buffer_init(size_t len, uint8_t** buffer) { + return libtock_screen_buffer_init(len, buffer); } -static void screen_cb_format(returncode_t ret, libtock_screen_format_t format) { - result_format.ret = ret; - result_format.format = format; - result_format.fired = true; +returncode_t libtocksync_screen_get_supported_resolutions(uint32_t* resolutions) { + return libtock_screen_get_supported_resolutions(resolutions); } -static void screen_cb_rotation(returncode_t ret, libtock_screen_rotation_t rotation) { - result_rotation.ret = ret; - result_rotation.rotation = rotation; - result_rotation.fired = true; +returncode_t libtocksync_screen_get_supported_resolution(size_t index, uint32_t* width, uint32_t* height) { + return libtock_screen_get_supported_resolution(index, width, height); } -bool libtocksync_screen_exists(void) { - return libtock_screen_driver_exists(); +returncode_t libtocksync_screen_get_supported_pixel_formats(uint32_t* formats) { + return libtock_screen_get_supported_pixel_formats(formats); } +returncode_t libtocksync_screen_get_supported_pixel_format(size_t index, libtock_screen_format_t* format) { + return libtock_screen_get_supported_pixel_format(index, format); +} + +int libtocksync_screen_get_bits_per_pixel(libtock_screen_format_t format) { + return libtock_screen_get_bits_per_pixel(format); +} + + returncode_t libtocksync_screen_set_brightness(uint32_t brightness) { returncode_t ret; - result.fired = false; - - ret = libtock_screen_set_brightness(brightness, screen_cb_done); + ret = libtock_screen_command_set_brightness(brightness); if (ret != RETURNCODE_SUCCESS) return ret; - // Wait for the callback. - yield_for(&result.fired); - return result.ret; + ret = libtocksync_screen_yield_wait_for(); + return ret; } returncode_t libtocksync_screen_invert_on(void) { returncode_t ret; - result.fired = false; - - ret = libtock_screen_invert_on(screen_cb_done); + ret = libtock_screen_command_invert_on(); if (ret != RETURNCODE_SUCCESS) return ret; - // Wait for the callback. - yield_for(&result.fired); - return result.ret; + ret = libtocksync_screen_yield_wait_for(); + return ret; } returncode_t libtocksync_screen_invert_off(void) { returncode_t ret; - result.fired = false; - - ret = libtock_screen_invert_on(screen_cb_done); + ret = libtock_screen_command_invert_off(); if (ret != RETURNCODE_SUCCESS) return ret; - // Wait for the callback. - yield_for(&result.fired); - return result.ret; + ret = libtocksync_screen_yield_wait_for(); + return ret; } returncode_t libtocksync_screen_get_pixel_format(libtock_screen_format_t* format) { returncode_t ret; - result_format.fired = false; - - ret = libtock_screen_get_pixel_format(screen_cb_format); + ret = libtock_screen_command_get_pixel_format(); if (ret != RETURNCODE_SUCCESS) return ret; - // Wait for the callback. - yield_for(&result_format.fired); - if (result_format.ret != RETURNCODE_SUCCESS) return result_format.ret; + ret = libtocksync_screen_yield_wait_for_format(format); + return ret; +} - *format = result_format.format; - return RETURNCODE_SUCCESS; +returncode_t libtocksync_screen_get_resolution(uint32_t* width, uint32_t* height) { + return libtock_screen_command_get_resolution(width, height); } returncode_t libtocksync_screen_get_rotation(libtock_screen_rotation_t* rotation) { returncode_t ret; - result_rotation.fired = false; - - ret = libtock_screen_get_rotation(screen_cb_rotation); + ret = libtock_screen_command_get_rotation(); if (ret != RETURNCODE_SUCCESS) return ret; - // Wait for the callback. - yield_for(&result_rotation.fired); - if (result_rotation.ret != RETURNCODE_SUCCESS) return result_rotation.ret; - - *rotation = result_rotation.rotation; - return RETURNCODE_SUCCESS; + ret = libtocksync_screen_yield_wait_for_rotation(rotation); + return ret; } returncode_t libtocksync_screen_set_rotation(libtock_screen_rotation_t rotation) { returncode_t ret; - result.fired = false; - - ret = libtock_screen_set_rotation(rotation, screen_cb_done); + ret = libtock_screen_command_set_rotation(rotation); if (ret != RETURNCODE_SUCCESS) return ret; - // Wait for the callback. - yield_for(&result.fired); - return result.ret; + ret = libtocksync_screen_yield_wait_for(); + return ret; } returncode_t libtocksync_screen_set_frame(uint16_t x, uint16_t y, uint16_t width, uint16_t height) { returncode_t ret; - result.fired = false; - - ret = libtock_screen_set_frame(x, y, width, height, screen_cb_done); + ret = libtock_screen_command_set_frame(x, y, width, height); if (ret != RETURNCODE_SUCCESS) return ret; - // Wait for the callback. - yield_for(&result.fired); - return result.ret; + ret = libtocksync_screen_yield_wait_for(); + return ret; } returncode_t libtocksync_screen_fill(uint8_t* buffer, int buffer_len, size_t color) { returncode_t ret; - result.fired = false; + ret = screen_set_color(buffer, buffer_len, 0, color); + if (ret != RETURNCODE_SUCCESS) return ret; + + ret = libtock_screen_set_readonly_allow(buffer, buffer_len); + if (ret != RETURNCODE_SUCCESS) return ret; + defer { libtock_screen_set_readonly_allow(NULL, 0); + } - ret = libtock_screen_fill(buffer, buffer_len, color, screen_cb_done); + ret = libtock_screen_command_fill(); if (ret != RETURNCODE_SUCCESS) return ret; - // Wait for the callback. - yield_for(&result.fired); - if (result.ret != RETURNCODE_SUCCESS) return result.ret; + ret = libtocksync_screen_yield_wait_for(); - ret = libtock_screen_set_readonly_allow(NULL, 0); return ret; } returncode_t libtocksync_screen_write(uint8_t* buffer, int buffer_len, size_t length) { returncode_t ret; - result.fired = false; + ret = libtock_screen_set_readonly_allow(buffer, buffer_len); + if (ret != RETURNCODE_SUCCESS) return ret; + defer { libtock_screen_set_readonly_allow(NULL, 0); + } - ret = libtock_screen_write(buffer, buffer_len, length, screen_cb_done); + ret = libtock_screen_command_write(length); if (ret != RETURNCODE_SUCCESS) return ret; - // Wait for the callback. - yield_for(&result.fired); - if (result.ret != RETURNCODE_SUCCESS) return result.ret; + ret = libtocksync_screen_yield_wait_for(); - ret = libtock_screen_set_readonly_allow(NULL, 0); return ret; } diff --git a/libtock-sync/display/screen.h b/libtock-sync/display/screen.h index 55dbb2929..4e0ccb639 100644 --- a/libtock-sync/display/screen.h +++ b/libtock-sync/display/screen.h @@ -1,14 +1,39 @@ #pragma once -#include +#include #include +#include "syscalls/screen_syscalls.h" + #ifdef __cplusplus extern "C" { #endif bool libtocksync_screen_exists(void); +// Allocate and setup `buffer` with size `len`. +// +// This will allocate len bytes and assign buffer to the array. This buffer can +// be used for drawing the screen. +statuscode_t libtocksync_screen_buffer_init(size_t len, uint8_t** buffer); + +// Get the number of supported resolutions for the screen. +returncode_t libtocksync_screen_get_supported_resolutions(uint32_t* resolutions); + +// Get a particular resolution based on `index`. `width` and `height` will be +// set with the resolution corresponding to that index. +returncode_t libtocksync_screen_get_supported_resolution(size_t index, uint32_t* width, uint32_t* height); + +// Get the number or supported pixel formats. +returncode_t libtocksync_screen_get_supported_pixel_formats(uint32_t* formats); + +// Get the particular format based on `index`. `format` will be set with the +// correct pixel format for that index. +returncode_t libtocksync_screen_get_supported_pixel_format(size_t index, libtock_screen_format_t* format); + +// Calculate and return the number of bits per pixel for the given pixel format. +int libtocksync_screen_get_bits_per_pixel(libtock_screen_format_t format); + // Set the screen brightness. returncode_t libtocksync_screen_set_brightness(uint32_t brightness); @@ -21,6 +46,9 @@ returncode_t libtocksync_screen_invert_off(void); // Get the current pixel format used by the screen. returncode_t libtocksync_screen_get_pixel_format(libtock_screen_format_t* format); +// Get the current screen resolution. +returncode_t libtocksync_screen_get_resolution(uint32_t* width, uint32_t* height); + // Get the current screen rotation. returncode_t libtocksync_screen_get_rotation(libtock_screen_rotation_t* rotation); diff --git a/libtock-sync/display/syscalls/screen_syscalls.c b/libtock-sync/display/syscalls/screen_syscalls.c new file mode 100644 index 000000000..8e9383c5e --- /dev/null +++ b/libtock-sync/display/syscalls/screen_syscalls.c @@ -0,0 +1,29 @@ +#include "screen_syscalls.h" + +returncode_t libtocksync_screen_yield_wait_for(void) { + yield_waitfor_return_t ret; + ret = yield_wait_for(DRIVER_NUM_SCREEN, 0); + + int status = tock_status_to_returncode((statuscode_t) ret.data0); + return (returncode_t) status; +} + +returncode_t libtocksync_screen_yield_wait_for_format(libtock_screen_format_t* format) { + yield_waitfor_return_t ret; + ret = yield_wait_for(DRIVER_NUM_SCREEN, 0); + + *format = (libtock_screen_format_t) ret.data1; + + int status = tock_status_to_returncode((statuscode_t) ret.data0); + return (returncode_t) status; +} + +returncode_t libtocksync_screen_yield_wait_for_rotation(libtock_screen_rotation_t* rotation) { + yield_waitfor_return_t ret; + ret = yield_wait_for(DRIVER_NUM_SCREEN, 0); + + *rotation = (libtock_screen_rotation_t) ret.data1; + + int status = tock_status_to_returncode((statuscode_t) ret.data0); + return (returncode_t) status; +} diff --git a/libtock-sync/display/syscalls/screen_syscalls.h b/libtock-sync/display/syscalls/screen_syscalls.h new file mode 100644 index 000000000..9e20a9a19 --- /dev/null +++ b/libtock-sync/display/syscalls/screen_syscalls.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +returncode_t libtocksync_screen_yield_wait_for(void); +returncode_t libtocksync_screen_yield_wait_for_format(libtock_screen_format_t* format); +returncode_t libtocksync_screen_yield_wait_for_rotation(libtock_screen_rotation_t* rotation); + +#ifdef __cplusplus +} +#endif diff --git a/libtock/display/screen.h b/libtock/display/screen.h index 3b126173c..454a14bae 100644 --- a/libtock/display/screen.h +++ b/libtock/display/screen.h @@ -1,28 +1,12 @@ #pragma once #include "../tock.h" +#include "screen_types.h" #ifdef __cplusplus extern "C" { #endif -// Supported pixel formats. -typedef enum { - MONO =0, - RGB_233 =1, - RGB_565 =2, - RGB_888 =3, - ARGB_8888=4, -} libtock_screen_format_t; - -// Supported screen rotations. -typedef enum { - ROTATION_NORMAL=0, - ROTATION_90 =1, - ROTATION_180 =2, - ROTATION_270 =3, -} libtock_screen_rotation_t; - // Callback when an operation has completed. // // This callback is used for several operations, but is generic as it only diff --git a/libtock/display/screen_types.h b/libtock/display/screen_types.h new file mode 100644 index 000000000..a1d362b93 --- /dev/null +++ b/libtock/display/screen_types.h @@ -0,0 +1,26 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +// Supported pixel formats. +typedef enum { + MONO =0, + RGB_233 =1, + RGB_565 =2, + RGB_888 =3, + ARGB_8888=4, +} libtock_screen_format_t; + +// Supported screen rotations. +typedef enum { + ROTATION_NORMAL=0, + ROTATION_90 =1, + ROTATION_180 =2, + ROTATION_270 =3, +} libtock_screen_rotation_t; + +#ifdef __cplusplus +} +#endif diff --git a/u8g2/u8g2-tock.c b/u8g2/u8g2-tock.c index 43050a0e7..92243e635 100644 --- a/u8g2/u8g2-tock.c +++ b/u8g2/u8g2-tock.c @@ -57,7 +57,7 @@ static uint8_t u8x8_d_ssd1306_tock(u8x8_t *u8x8, // Update the default display_info with actual information from the // kernel. uint32_t width = 0, height = 0; - libtock_screen_get_resolution(&width, &height); + libtocksync_screen_get_resolution(&width, &height); u8x8_ssd1306_tock.tile_width = width/8; u8x8_ssd1306_tock.tile_height = height/8; u8x8_ssd1306_tock.pixel_width = width;