add example helper, translate more examples

update to wip servicepoint lib
This commit is contained in:
Vinzenz Schroeter 2025-05-10 14:58:50 +02:00
parent b8a55d0433
commit 4ab5305377
15 changed files with 195 additions and 61 deletions

View file

@ -1,16 +1,9 @@
#include <stdio.h>
#include "servicepoint.h"
#include "helpers.h"
int main(void) {
printf("test\n");
sock_init();
UdpSocket *connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342);
//UdpSocket *connection = sp_udp_open_ipv4(127, 0, 0, 1, 2342);
if (connection == NULL)
return 1;
sp_udp_send_header(connection, (Header) {.command_code = COMMAND_CODE_CLEAR});
sp_udp_send_header(sock, (Header) {.command_code = COMMAND_CODE_CLEAR});
CharGrid *grid = sp_char_grid_new(5, 2);
if (grid == NULL)
@ -30,8 +23,7 @@ int main(void) {
Packet *packet = sp_char_grid_into_packet(grid, 0, 0);
if (packet == NULL)
return 1;
sp_udp_send_packet(connection, packet);
sp_udp_send_packet(sock, packet);
sp_udp_free(connection);
return 0;
}

View file

@ -1,6 +1,5 @@
#include "servicepoint.h"
static UdpSocket *connection = NULL;
#include "helpers.h"
void enable_all_pixels(void) {
Bitmap *all_on = sp_bitmap_new_max_sized();
@ -9,26 +8,18 @@ void enable_all_pixels(void) {
BitmapCommand *bitmapCommand = sp_cmd_bitmap_from_bitmap(all_on);
Packet *packet = sp_cmd_bitmap_try_into_packet(bitmapCommand);
if (packet != NULL)
sp_udp_send_packet(connection, packet);
sp_udp_send_packet(sock, packet);
}
void make_brightness_pattern(BrightnessGrid *grid) {
ByteSlice slice = sp_brightness_grid_unsafe_data_ref(grid);
for (size_t index = 0; index < slice.length; index++) {
slice.start[index] = (uint8_t) (index % ((size_t) Brightness_MAX));
slice.start[index] = (uint8_t)(index % ((size_t) Brightness_MAX));
}
}
void run_at_exit() {
sp_udp_free(connection);
}
int main(void) {
//UdpSocket *connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342);
connection = sp_udp_open_ipv4(127, 0, 0, 1, 2342);
if (connection == NULL)
return -1;
atexit(run_at_exit);
sock_init();
enable_all_pixels();
@ -37,8 +28,8 @@ int main(void) {
Packet *packet = sp_cmd_brightness_grid_into_packet(sp_cmd_brightness_grid_from_grid(grid));
if (packet == NULL)
return -2;
return -2;
sp_udp_send_packet(connection, packet);
sp_udp_send_packet(sock, packet);
return 0;
}

45
example/src/helpers.h Normal file
View file

@ -0,0 +1,45 @@
#pragma once
#ifndef SERVICEPOINT_BINDING_C_MSLEEP_H
#define SERVICEPOINT_BINDING_C_MSLEEP_H
#include <time.h>
#include <errno.h>
#include <stdlib.h>
#include "servicepoint.h"
static UdpSocket *sock = NULL;
void sock_free() {
sp_udp_free(sock);
}
void sock_init() {
//sock = sp_udp_open_ipv4(127, 0, 0, 1, 2342);
sock = sp_udp_open_ipv4(172, 23, 42, 29, 2342);
if (sock == NULL)
exit(-1);
atexit(sock_free);
}
/// TODO: all of this for sleeping n ms? There should be a better way!
int msleep(long msec) {
int res;
if (msec < 0) {
errno = EINVAL;
return -1;
}
struct timespec ts = {
.tv_sec = msec / 1000,
.tv_nsec = (msec % 1000) * 1000000,
};
do {
res = nanosleep(&ts, &ts);
} while (res && errno == EINTR);
return res;
}
#endif //SERVICEPOINT_BINDING_C_MSLEEP_H

34
example/src/moving_line.c Normal file
View file

@ -0,0 +1,34 @@
#include "servicepoint.h"
#include "helpers.h"
int main() {
sock_init();
int result = 0;
Bitmap *bitmap = sp_bitmap_new_max_sized();
for (int x = 0; x < sp_bitmap_width(bitmap); x++) {
sp_bitmap_fill(bitmap, false);
for (int y = 0; y < sp_bitmap_height(bitmap); y++) {
sp_bitmap_set(bitmap, (y + x) % PIXEL_WIDTH, y, true);
}
BitmapCommand *command = sp_cmd_bitmap_from_bitmap(sp_bitmap_clone(bitmap));
Packet *packet = sp_cmd_bitmap_try_into_packet(command);
if (packet == NULL) {
result = -2;
goto exit;
}
if (!sp_udp_send_packet(sock, packet)) {
result = -3;
goto exit;
}
msleep(SP_FRAME_PACING_MS);
}
exit:
sp_bitmap_free(bitmap);
return result;
}

View file

@ -1,10 +1,9 @@
#include <stdio.h>
#include "servicepoint.h"
#include "helpers.h"
int main(void) {
UdpSocket *connection = sp_udp_open_ipv4(127, 0, 0, 1, 2342);
if (connection == NULL)
return 1;
sock_init();
Bitmap *pixels = sp_bitmap_new(PIXEL_WIDTH, PIXEL_HEIGHT);
if (pixels == NULL)
@ -19,8 +18,6 @@ int main(void) {
Header *header = sp_packet_get_header(packet);
printf("[%d, %d, %d, %d, %d]\n", header->command_code, header->a, header->b, header->c, header->d);
sp_udp_send_packet(connection, packet);
sp_udp_free(connection);
sp_udp_send_packet(sock, packet);
return 0;
}

View file

@ -0,0 +1,34 @@
#include "servicepoint.h"
#include "helpers.h"
int main() {
sock_init();
Bitmap *enabled_pixels = sp_bitmap_new_max_sized();
sp_bitmap_fill(enabled_pixels, true);
int result = 0;
for (int x = 0; x < PIXEL_WIDTH; x++) {
for (int y = 0; y < PIXEL_HEIGHT; y++) {
sp_bitmap_set(enabled_pixels, x, y, false);
}
BitVec *bitvec = sp_bitmap_into_bitvec(sp_bitmap_clone(enabled_pixels));
BitVecCommand *command = sp_cmd_bitvec_new(bitvec, 0, BINARY_OPERATION_AND, COMPRESSION_CODE_LZMA);
Packet *packet = sp_cmd_bitvec_try_into_packet(command);
if (packet == NULL) {
result = -2;
goto exit;
}
if (!sp_udp_send_packet(sock, packet)) {
result = -3;
goto exit;
}
msleep(SP_FRAME_PACING_MS);
}
exit:
sp_bitmap_free(enabled_pixels);
return result;
}