asyncburp: boilerplate

This commit is contained in:
tth 2020-10-30 22:21:20 +01:00
parent 18b0fceff4
commit 36f712dc98
6 changed files with 240 additions and 5 deletions

1
.gitignore vendored
View File

@ -21,5 +21,6 @@ tools/udp-dumper
tools/*.o tools/*.o
specific/joy2laser specific/joy2laser
specific/asyncburp
specific/*.o specific/*.o

View File

@ -2,8 +2,9 @@
OPTS = -Wall -g -DDEBUG_LEVEL=0 OPTS = -Wall -g -DDEBUG_LEVEL=0
all: joy2laser asyncburp
all: joy2laser # --------------------------------------------
laserblast.o: laserblast.c Makefile laserblast.o: laserblast.c Makefile
gcc -c ${OPTS} $< gcc -c ${OPTS} $<
@ -11,4 +12,17 @@ laserblast.o: laserblast.c Makefile
joy2laser: joy2laser.c Makefile laserblast.o joy2laser: joy2laser.c Makefile laserblast.o
gcc ${OPTS} $< laserblast.o ../functions/libpocosc.a -llo -o $@ gcc ${OPTS} $< laserblast.o ../functions/libpocosc.a -llo -o $@
# --------------------------------------------
asyncburp.o: asyncburp.c burpmsg.h Makefile
gcc -c ${OPTS} $<
burpmsg.o: burpmsg.c burpmsg.h Makefile
gcc -c ${OPTS} $<
asyncburp: asyncburp.o burpmsg.o
gcc ${OPTS} $< burpmsg.o -llo -o $@
# --------------------------------------------

96
specific/asyncburp.c Normal file
View File

@ -0,0 +1,96 @@
/*
* Joystick to Laser
* async version, with continuous refresh
* post Sonoptic 2020
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <lo/lo.h> /* OSC library */
#include "laserblast.h"
#include "burpmsg.h"
/* ------------------------------------------------------------------- */
#define LOCAL_PORT "9000"
#define REMOTE_HOST "localhost" /* just loling */
#define REMOTE_PORT "9999"
/* ------------------------------------------------------------------- */
int verbosity; /* global variable */
/* ------------------------------------------------------------------- */
static int bloub(void)
{
BurpMsg message;
message.magic = BURP_MAGIC;
display_burp_msg(&message, NULL);
return 0;
}
/* ------------------------------------------------------------------- */
static int help(int krkrkr)
{
puts("HELP ME !");
puts("\t-p NNN\t\tlocal listening port");
puts("\t-R a.b.c.d\tremote host");
puts("\t-P NNN\t\tremote port");
puts("\t-L N\t\tlaser number");
puts("\t-S N\t\tscene number");
puts("\t-v\t\tincrease verbosity");
return 1;
}
/* ------------------------------------------------------------------- */
int main(int argc, char *argv[])
{
char *local_port = LOCAL_PORT;
char *remote_host = REMOTE_HOST;
char *remote_port = REMOTE_PORT;
int lasernumber, scenenumber;
int opt;
fprintf(stderr, "%s compiled %s at %s\n",argv[0], __DATE__, __TIME__);
/* set some default values */
lasernumber = scenenumber = 0;
/* parsing command line options */
while ((opt = getopt(argc, argv, "hp:vL:S:R:P:")) != -1) {
switch (opt) {
case 'h': if (help(0)) exit(1); break;
case 'p': local_port = optarg; break;
case 'R': remote_host = optarg; break;
case 'P': remote_port = optarg; break;
case 'L': lasernumber = atoi(optarg); break;
case 'S': scenenumber = atoi(optarg); break;
case 'v': verbosity++; break;
}
}
if (verbosity) {
fprintf(stderr, "-------------: %s\n", argv[0]);
fprintf(stderr, "pid : %d\n", getpid());
fprintf(stderr, "local port : %s\n", local_port);
fprintf(stderr, "remote : %s %s\n", remote_host, remote_port);
fprintf(stderr, "scn/laser : %d %d\n", scenenumber, lasernumber);
}
bloub();
return 2;
}
/* ------------------------------------------------------------------- */

95
specific/burpmsg.c Normal file
View File

@ -0,0 +1,95 @@
/*
* BURPMSG.C
*/
#include <stdio.h>
#include <stdint.h>
#include <sys/select.h>
#include <unistd.h>
#include "burpmsg.h"
/* ------------------------------------------------------------------- */
int display_burp_msg(BurpMsg *header, void *data)
{
char *txt_type;
if (BURP_MAGIC != header->magic) {
fprintf(stderr, "burpmsg at %p : invalid magic %08X\n",
header, header->magic);
return -1;
}
txt_type = "wtf?"; /* nice default value */
switch(header->msgtype) {
case BURP_RESET:
txt_type = "reset";
break;
case BURP_SETSIZE:
txt_type = "setsize";
break;
case BURP_LASER:
txt_type = "laser";
break;
case BURP_SCENE:
txt_type = "scene";
break;
default:
fprintf(stderr, "burpmsg at %p : invalid msgtype %d\n",
header, header->msgtype);
return -1;
}
fprintf(stderr, "%4d %12s %9d %3d\n",
header->msgtype, txt_type, header->serial, header->szdata);
return 0;
}
/* ------------------------------------------------------------------- */
void la_grande_boucle( int fromfifo )
{
struct timeval tv;
fd_set fds;
int resval, foo;
BurpMsg message;
for(;;) { /* start of infinite loop */
FD_ZERO(&fds);
FD_SET(fromfifo, &fds);
tv.tv_sec = 1;
tv.tv_usec = 0;
resval = select(1, &fds, NULL, NULL, &tv);
if (-1 == resval) {
perror("select()");
/* ABORTING HERE ? */
continue;
}
if (resval) {
/* message available */
foo = read(fromfifo, &message, sizeof(BurpMsg));
if (sizeof(BurpMsg) != foo) {
fprintf(stderr, "err: %d read, %lu expected\n",
foo, sizeof(BurpMsg));
}
display_burp_msg(&message, NULL);
}
else {
/* got a time out */
fprintf(stderr, "TIMEOUT\n");
}
}
/* NOT REACHED */
}
/* ------------------------------------------------------------------- */

30
specific/burpmsg.h Normal file
View File

@ -0,0 +1,30 @@
/*
* BURPMSG.H
*/
/* ------------------------------------------------------------------- */
typedef struct {
uint32_t magic;
uint32_t serial;
uint16_t msgtype;
uint16_t szdata;
} BurpMsg;
#define BURP_MAGIC 0x98769876
/* message type */
#define BURP_RESET 1
#define BURP_SETSIZE 2
#define BURP_LASER 3
#define BURP_SCENE 4
#define BURP_COLOR 5
/* ------------------------------------------------------------------- */
int display_burp_msg(BurpMsg *header, void *data);
/* ------------------------------------------------------------------- */

View File

@ -24,6 +24,9 @@ static int x_scale, y_scale;
extern int verbosity; extern int verbosity;
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */
/*
* - prepare OSC transmission
*/
int blast_init(char *host, char *port, int scene, int laser) int blast_init(char *host, char *port, int scene, int laser)
{ {
#if DEBUG_LEVEL #if DEBUG_LEVEL
@ -31,7 +34,6 @@ fprintf(stderr, ">>> %s ( '%s:%p' %d %d )\n", __func__, host, port,
scene, laser); scene, laser);
#endif #endif
/* prepare OSC transmission */
lo_addr = lo_address_new(host, port); lo_addr = lo_address_new(host, port);
if (verbosity) { if (verbosity) {
fprintf(stderr, "%s is sending to %s:%s\n", __FILE__, host, port); fprintf(stderr, "%s is sending to %s:%s\n", __FILE__, host, port);
@ -118,6 +120,3 @@ return 0;
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */