Skip to content
Open
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
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
38 changes: 16 additions & 22 deletions libtock-sync/crypto/sha.c
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
#include "sha.h"

struct sha_data {
bool fired;
returncode_t ret;
};

static struct sha_data result = {.fired = false};

static void sha_cb_hash(returncode_t ret) {
result.fired = true;
result.ret = ret;
}

returncode_t libtocksync_sha_simple_hash(libtock_sha_algorithm_t hash_type,
uint8_t* input_buffer, uint32_t input_length,
uint8_t* hash_buffer, uint32_t hash_length) {
returncode_t ret;

result.fired = false;
ret = libtock_sha_command_set_algorithm((uint8_t) hash_type);
if (ret != RETURNCODE_SUCCESS) return ret;

ret = libtock_sha_simple_hash(hash_type, input_buffer, input_length, hash_buffer, hash_length, sha_cb_hash);
ret = libtock_sha_set_readonly_allow_data_buffer(input_buffer, input_length);
if (ret != RETURNCODE_SUCCESS) return ret;

// Wait for the callback.
yield_for(&result.fired);
if (result.ret != RETURNCODE_SUCCESS) return result.ret;
ret = libtock_sha_set_readwrite_allow_destination_buffer(hash_buffer, hash_length);
if (ret != RETURNCODE_SUCCESS) goto exit1;

ret = libtock_sha_set_readonly_allow_data_buffer(NULL, 0);
if (ret != RETURNCODE_SUCCESS) return ret;
ret = libtock_sha_command_run();
if (ret != RETURNCODE_SUCCESS) goto exit2;

ret = libtock_sha_set_readwrite_allow_destination_buffer(NULL, 0);
if (ret != RETURNCODE_SUCCESS) return ret;
// Wait for the operation.
ret = libtocksync_sha_yield_wait_for();

exit2:
libtock_sha_set_readwrite_allow_destination_buffer(NULL, 0);

exit1:
libtock_sha_set_readonly_allow_data_buffer(NULL, 0);

return RETURNCODE_SUCCESS;
return ret;
}
4 changes: 3 additions & 1 deletion libtock-sync/crypto/sha.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include <libtock/crypto/sha.h>
#include <libtock/crypto/sha_types.h>
#include <libtock/tock.h>

#include "syscalls/sha_syscalls.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand Down
8 changes: 8 additions & 0 deletions libtock-sync/crypto/syscalls/sha_syscalls.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "sha_syscalls.h"

returncode_t libtocksync_sha_yield_wait_for(void) {
yield_waitfor_return_t ret;
ret = yield_wait_for(DRIVER_NUM_SHA, 0);

return (returncode_t) ret.data0;
}
15 changes: 15 additions & 0 deletions libtock-sync/crypto/syscalls/sha_syscalls.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <libtock/crypto/syscalls/sha_syscalls.h>
#include <libtock/tock.h>

#ifdef __cplusplus
extern "C" {
#endif

// Wait for an SHA hash to finish.
returncode_t libtocksync_sha_yield_wait_for(void);

#ifdef __cplusplus
}
#endif
145 changes: 51 additions & 94 deletions libtock-sync/display/screen.c
Original file line number Diff line number Diff line change
@@ -1,167 +1,124 @@
#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 struct screen_done result;
static struct screen_format result_format;
static struct screen_rotation result_rotation;


static void screen_cb_done(returncode_t ret) {
result.ret = ret;
result.fired = true;
}

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;
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 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_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_fill(buffer, buffer_len, color, screen_cb_done);
ret = libtock_screen_set_readonly_allow(buffer, buffer_len);
if (ret != RETURNCODE_SUCCESS) return ret;

// Wait for the callback.
yield_for(&result.fired);
if (result.ret != RETURNCODE_SUCCESS) return result.ret;
ret = libtock_screen_command_fill();
if (ret != RETURNCODE_SUCCESS) goto exit;

ret = libtocksync_screen_yield_wait_for();

exit:
libtock_screen_set_readonly_allow(NULL, 0);

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_write(buffer, buffer_len, length, screen_cb_done);
ret = libtock_screen_set_readonly_allow(buffer, buffer_len);
if (ret != RETURNCODE_SUCCESS) return ret;

// Wait for the callback.
yield_for(&result.fired);
if (result.ret != RETURNCODE_SUCCESS) return result.ret;
ret = libtock_screen_command_write(length);
if (ret != RETURNCODE_SUCCESS) goto exit;

ret = libtocksync_screen_yield_wait_for();

exit:
libtock_screen_set_readonly_allow(NULL, 0);

ret = libtock_screen_set_readonly_allow(NULL, 0);
return ret;
}
7 changes: 6 additions & 1 deletion libtock-sync/display/screen.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#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
Expand All @@ -19,6 +21,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
29 changes: 29 additions & 0 deletions libtock-sync/display/syscalls/screen_syscalls.c
Original file line number Diff line number Diff line change
@@ -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;
}
17 changes: 17 additions & 0 deletions libtock-sync/display/syscalls/screen_syscalls.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <libtock/display/syscalls/screen_syscalls.h>
#include <libtock/display/screen_types.h>
#include <libtock/tock.h>

#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
Loading
Loading