Skip to content

Instantly share code, notes, and snippets.

@sp5wwp
Last active April 15, 2024 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sp5wwp/c53602549f8ccde0c6e30d593aa6bb5b to your computer and use it in GitHub Desktop.
Save sp5wwp/c53602549f8ccde0c6e30d593aa6bb5b to your computer and use it in GitHub Desktop.
ZeroMQ baseband publisher
/*
to source IQ baseband samples from SX1255 over I2S, run this:
arecord -t raw -f S32_LE -r 192000 -c 2 --device="hw:0,1" | ./zmq-pub
note: you need to enable I2S overlay (acting as a slave)
this one works good: https://github.com/AkiyukiOkayasu/RaspberryPi_I2S_Slave
note: setting the rate to 192k isn't really effective, the actual rate is 125k (governed by the I2S master - SX1255)
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <zmq.h>
#define SAMP_FLOAT
void *zmq_ctx;
void *bsb_downlink;
int rcvd;
uint8_t inp[100000];
int main(void)
{
zmq_ctx = zmq_ctx_new();
bsb_downlink = zmq_socket(zmq_ctx, ZMQ_PUB);
printf("ZeroMQ ");
if(zmq_bind(bsb_downlink, "tcp://*:17017")==0)
printf("OK\n");
else
printf("ERROR\n");
while(1)
{
ioctl(STDIN_FILENO, FIONREAD, &rcvd);
if(rcvd>0)
{
if(read(STDIN_FILENO, inp, rcvd)==rcvd)
{
#if defined(SAMP_INT32) //signed 32-bit ints
zmq_send(bsb_downlink, (char*)inp, rcvd, 0);
#elif defined(SAMP_FLOAT) //32-bit floats
float val[100000];
memset(val, 0, sizeof(val));
for(int j=0; j<rcvd; j+=8)
{
int32_t i=0, q=0;
memcpy((uint8_t*)&i, &inp[0+j], 4);
memcpy((uint8_t*)&q, &inp[4+j], 4);
val[(j/8)*2] =i/(float)0x7FFFFFFFL;
val[(j/8)*2+1]=q/(float)0x7FFFFFFFL;
}
zmq_send(bsb_downlink, (char*)val, rcvd, 0);
#endif
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment