Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/lvgl/lvgl_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion examples/tests/screen-tock/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
14 changes: 7 additions & 7 deletions examples/tests/screen/screen_init/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/tests/screen/sierpinski_carpet/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
160 changes: 71 additions & 89 deletions libtock-sync/display/screen.c
Original file line number Diff line number Diff line change
@@ -1,173 +1,155 @@
#include <libtock/defer.h>
#include <libtock/display/screen.h>
#include <libtock/display/syscalls/screen_syscalls.h>

#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
Comment thread
brghena marked this conversation as resolved.
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;
}
30 changes: 29 additions & 1 deletion libtock-sync/display/screen.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
#pragma once

#include <libtock/display/screen.h>
#include <libtock/display/screen_types.h>
#include <libtock/tock.h>

#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);

Expand All @@ -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);

Expand Down
Loading
Loading