Add button support in OS

This commit is contained in:
shylie 2026-05-26 07:38:31 -04:00
parent b82b1311f4
commit da5c324618
Signed by: shylie
GPG Key ID: 20C8D70580687D9D
4 changed files with 62 additions and 13 deletions

View File

@ -7,9 +7,9 @@ int main(void)
card_os_put_rect((card_os_rect){ 0, 0, 0, 0, 0, 240, 320 });
card_os_put_rect((card_os_rect){
.r = 0xFF, .g = 0xFF, .b = 0xFF, .x = 60, .y = 80, .w = 120, .h = 160 });
.r = 0xFF, .g = 0xFF, .b = 0xFF, .x = 60, .y = 80, .w = 160, .h = 160 });
while (value < 250)
while (!card_os_get_button(OS_BUTTON_MIDDLE))
{
card_os_put_rect((card_os_rect){
.r = value++, .g = 0, .b = 0, .x = 0, .y = 0, .w = 60, .h = 60 });

View File

@ -6,7 +6,8 @@
typedef enum
{
OS_CMD_SBRK,
OS_CMD_DRAW_RECT
OS_CMD_DRAW_RECT,
OS_CMD_GET_BUTTON
} os_command;
typedef struct
@ -23,6 +24,14 @@ typedef struct
uint16_t x, y, w, h;
} card_os_rect;
typedef enum
{
OS_BUTTON_LEFT,
OS_BUTTON_MIDDLE,
OS_BUTTON_RIGHT
} card_os_button;
void card_os_put_rect(card_os_rect rect);
int card_os_get_button(card_os_button button);
#endif // CARD_OS_USER

View File

@ -27,6 +27,13 @@ void card_os_put_rect(card_os_rect rect)
oscall(&(os_message){ .command = OS_CMD_DRAW_RECT, .data = &rect });
}
int card_os_get_button(card_os_button button)
{
int status = button;
oscall(&(os_message){ .command = OS_CMD_GET_BUTTON, .data = &status });
return status;
}
extern uint8_t __bss_start__;
extern uint8_t __bss_end__;
int main(void);

View File

@ -17,6 +17,10 @@ const uint8_t DISPLAY_CS = 5;
const uint8_t DISPLAY_DC = 4;
spi_inst_t* const DISPLAY_SPI = spi0;
const uint8_t BUTTON_LEFT = 15;
const uint8_t BUTTON_MIDDLE = 16;
const uint8_t BUTTON_RIGHT = 17;
extern uint8_t __user_ram_start;
typedef int (*user_program_fn)(void);
@ -24,7 +28,7 @@ static volatile user_program_fn user_program;
void core1_entry(void);
void os_task(void);
void os_handle_message(const os_message* msg, os_message* out);
void os_handle_message(os_message* msg);
int main(void)
{
@ -37,6 +41,14 @@ int main(void)
gpio_set_function(DISPLAY_DC, GPIO_FUNC_SIO);
gpio_set_dir(DISPLAY_DC, GPIO_OUT);
gpio_set_dir(BUTTON_LEFT, GPIO_IN);
gpio_set_dir(BUTTON_MIDDLE, GPIO_IN);
gpio_set_dir(BUTTON_RIGHT, GPIO_IN);
gpio_pull_up(BUTTON_LEFT);
gpio_pull_up(BUTTON_MIDDLE);
gpio_pull_up(BUTTON_RIGHT);
spi_init(DISPLAY_SPI, 100 * 1000 * 1000);
spi_set_format(DISPLAY_SPI, 8, SPI_CPOL_1, SPI_CPHA_1, SPI_MSB_FIRST);
@ -139,13 +151,11 @@ void os_task(void)
os_message msg = core1_buffer.buffer[index];
core1_buffer.read_index
= (core1_buffer.read_index + 1) % OS_RING_BUFFER_SIZE;
os_message out;
os_handle_message(&msg, &out);
out.command = msg.command;
os_handle_message(&msg);
int wrote_index = -1;
while (wrote_index < 0)
{
wrote_index = os_ring_buffer_write(&core0_buffer, &out);
wrote_index = os_ring_buffer_write(&core0_buffer, &msg);
}
multicore_fifo_push_blocking(wrote_index);
}
@ -155,7 +165,7 @@ extern uint8_t __end__;
extern uint8_t __StackLimit;
static uint8_t* heap_end;
void os_sbrk(const ptrdiff_t* incr, void** out)
void os_sbrk(ptrdiff_t* incr)
{
if (!heap_end)
{
@ -167,12 +177,12 @@ void os_sbrk(const ptrdiff_t* incr, void** out)
if (next >= &__StackLimit || next < &__end__)
{
*out = (void*)-1;
*incr = -1;
}
else
{
heap_end = next;
*out = prev;
*incr = (ptrdiff_t)prev;
}
}
@ -182,16 +192,39 @@ void os_put_rect(const card_os_rect* rect)
rect->h);
}
void os_handle_message(const os_message* msg, os_message* out)
void os_get_button(int* button)
{
switch (*button)
{
case OS_BUTTON_LEFT:
*button = !gpio_get(BUTTON_LEFT);
break;
case OS_BUTTON_MIDDLE:
*button = !gpio_get(BUTTON_MIDDLE);
break;
case OS_BUTTON_RIGHT:
*button = !gpio_get(BUTTON_RIGHT);
break;
}
}
void os_handle_message(os_message* msg)
{
switch (msg->command)
{
case OS_CMD_SBRK:
os_sbrk(msg->data, &out->data);
os_sbrk(msg->data);
break;
case OS_CMD_DRAW_RECT:
os_put_rect(msg->data);
break;
case OS_CMD_GET_BUTTON:
os_get_button(msg->data);
break;
}
}