add vibecoded artnet lib

This commit is contained in:
2025-12-17 02:24:38 +01:00
parent 97e9a03610
commit a450f351c5
4 changed files with 201 additions and 14 deletions

View File

@@ -0,0 +1,135 @@
/* --------------------------------------------------------------------
* artnet_receiver.c tiny ArtNet DMX receiver
* -------------------------------------------------------------------- */
#define _POSIX_C_SOURCE 200112L
#include "artnet_receiver.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <pthread.h>
#include <errno.h>
#define ARTNET_PORT 6454
#define ARTNET_HEADER_SIZE 18 /* "Art-Net\0" + OpCode + … */
#define OP_DMX 0x5000 /* lowbyte first (littleendian) */
/* --------------------------------------------------------------- */
static int artnet_sock = -1;
static pthread_t receiver_thread;
static volatile bool run_thread = false;
/* -----------------------------------------------------------------
* Helper: map a DMX byte (0255) to a signed or unsigned integer
* ----------------------------------------------------------------- */
static inline int map_range_uint8(uint8_t v, int dst_min, int dst_max)
{
return dst_min + (int)((v * (dst_max - dst_min)) / 255.0);
}
/* -----------------------------------------------------------------
* The thread that receives ArtNet packets
* ----------------------------------------------------------------- */
static void *receiver_func(void *arg)
{
(void)arg; /* unused */
struct sockaddr_in from;
socklen_t fromlen = sizeof(from);
uint8_t buf[1500]; /* big enough for a full packet */
while (run_thread) {
ssize_t n = recvfrom(artnet_sock, buf, sizeof(buf), 0,
(struct sockaddr *)&from, &fromlen);
if (n < 0) {
if (errno == EINTR) continue;
perror("recvfrom");
continue;
}
/* ------------------ quick sanity checks ------------------- */
if (n < ARTNET_HEADER_SIZE) continue; /* too short */
if (memcmp(buf, "Art-Net\0", 8) != 0) continue; /* not ArtNet */
uint16_t opcode = buf[8] | (buf[9] << 8); /* littleendian */
if (opcode != OP_DMX) continue; /* not ArtDMX */
/* --------------------- DMX payload ----------------------- */
uint16_t universe = buf[14] | (buf[15] << 8); /* not used */
uint16_t length = buf[16] << 8 | buf[17]; /* bigendian */
if (length + ARTNET_HEADER_SIZE != (size_t)n) continue; /* malformed */
uint8_t *dmx = buf + ARTNET_HEADER_SIZE; /* first DMX slot */
/* --------------------- update shared variables ------------ */
/* 0 … 11 → rotation angles (0180°) */
for (int i = 0; i < 12 && i < length; ++i) {
rotAnglesDeg[i] = map_range_uint8(dmx[i], 0, 180);
}
///* channel 12 → line thickness (110) */
//if (length > 12) {
// LINE_THICKNESS = map_range_uint8(dmx[12], 1, 10);
//}
///* channel 13 → top margin (0200px) */
//if (length > 13) {
// MARGIN_TOP = map_range_uint8(dmx[13], 0, 200);
//}
///* channel 14 → bottom margin (0200px) */
//if (length > 14) {
// MARGIN_BOTTOM = map_range_uint8(dmx[14], 0, 200);
//}
/* (Add more channels here if you need them) */
}
return NULL;
}
/* -----------------------------------------------------------------
* Public API
* ----------------------------------------------------------------- */
bool artnet_init(void)
{
struct sockaddr_in sa;
artnet_sock = socket(AF_INET, SOCK_DGRAM, 0);
if (artnet_sock < 0) {
perror("socket");
return false;
}
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = htons(ARTNET_PORT);
if (bind(artnet_sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
perror("bind");
close(artnet_sock);
artnet_sock = -1;
return false;
}
run_thread = true;
if (pthread_create(&receiver_thread, NULL, receiver_func, NULL) != 0) {
perror("pthread_create");
close(artnet_sock);
artnet_sock = -1;
run_thread = false;
return false;
}
return true;
}
void artnet_stop(void)
{
if (!run_thread) return;
run_thread = false;
/* Wake the thread if it is blocked in recvfrom */
shutdown(artnet_sock, SHUT_RDWR);
pthread_join(receiver_thread, NULL);
close(artnet_sock);
artnet_sock = -1;
}

View File

@@ -0,0 +1,41 @@
/* --------------------------------------------------------------------
* artnet_receiver.c tiny ArtNet DMX receiver
*
* This file contains only the pieces you need to:
* * open a UDP socket on port 6454,
* * receive ArtDMX packets,
* * extract the DMX data (up to 512 bytes),
* * copy the first N bytes into the variables of the main program.
*
* The code is deliberately simple it ignores ArtPoll, ArtSync,
* multiple universes, etc. If you need those features you can replace
* the receive loop with libartnet or another full implementation.
* -------------------------------------------------------------------- */
#ifndef ARTNET_RECEIVER_H
#define ARTNET_RECEIVER_H
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <pthread.h>
/* ---------------------------------------------------------------
* Public API call from the main program
*
* artnet_init() : creates the receiving thread.
* artnet_stop() : asks the thread to terminate and joins it.
* --------------------------------------------------------------- */
bool artnet_init(void); /* returns true on success */
void artnet_stop(void);
/* ---------------------------------------------------------------
* The variables that the ArtNet thread will write to.
* They are declared `extern` here and defined in the main file.
* --------------------------------------------------------------- */
extern int16_t rotAnglesDeg[12]; /* 0 … 180° (mapped from 0255) */
extern int LINE_THICKNESS; /* 1 … 10 (clamped) */
extern int MARGIN_TOP; /* 0 … 200px (scaled) */
extern int MARGIN_BOTTOM; /* 0 … 200px (scaled) */
#endif /* ARTNET_RECEIVER_H */

View File

@@ -23,6 +23,8 @@
#include <arpa/inet.h>
#include <sys/socket.h>
#include "artnet_receiver.h"
#define FB_W 800 /* framebuffer width */
#define FB_H 600 /* framebuffer height */
#define BPP 4 /* bytes per pixel (A,R,G,B) */
@@ -35,9 +37,9 @@
#define LINE_THICKNESS 4 /* thickness of outline (pixels) */
#define NUM_RECTS 12
/*---------------------------------------------------------------*/
/* 1. Perrectangle tilt angles (in degrees). Edit as you wish. */
static int16_t rotAnglesDeg[NUM_RECTS] = {
///*---------------------------------------------------------------*/
///* 1. Perrectangle tilt angles (in degrees). Edit as you wish. */
int16_t rotAnglesDeg[NUM_RECTS] = {
0, 5, 10, 15, 20, 25,
30, 35, 40, 45, 50, 55
};
@@ -205,6 +207,15 @@ static int send_framebuffer(const uint8_t *fb, size_t len,
/*---------------------------------------------------------------*/
int main(void)
{
/* ---------------------------------------------------------------
* 1⃣ Start ArtNet receiver it will fill the globals
* --------------------------------------------------------------- */
if (!artnet_init()) {
fprintf(stderr, "Failed to start ArtNet receiver\n");
return EXIT_FAILURE;
}
uint8_t *framebuf = malloc(FRAMEBUF_SIZE);
if (!framebuf) {
fprintf(stderr, "Failed to allocate framebuffer (%zu bytes)\n",
@@ -213,8 +224,8 @@ int main(void)
}
while(1){
for(uint8_t i=0; i<sizeof(rotAnglesDeg)/sizeof(rotAnglesDeg[0]); i++)
rotAnglesDeg[i]=(rotAnglesDeg[i]+3)%180;
//for(uint8_t i=0; i<sizeof(rotAnglesDeg)/sizeof(rotAnglesDeg[0]); i++)
// rotAnglesDeg[i]=(rotAnglesDeg[i]+3)%180;
build_framebuffer(framebuf);
if (send_framebuffer(framebuf, FRAMEBUF_SIZE, "127.0.0.1", 12345) != 0) {

View File

@@ -1,10 +1,10 @@
CC := gcc
CFLAGS := -Wall -Wextra
CFLAGS := -Wall -Wextra -pthread
LDLIBS := -lc -lm
TARGET := dmx2img
SRC := dmx2img.c
SRC := dmx2img.c artnet_receiver.c
# -------------------------------------------------
# Default target just type `make` to build