Commit 52bfa2dd authored by Your Name's avatar Your Name
Browse files

.

parent f9e9b06a
No preview for this file type
LSLBASE=../labstreaminglayer/build/install
CPPFLAGS+=-O2 -Wall -Wno-sign-compare -I$(LSLBASE)/include
CPPFLAGS+=-Wall -Wno-sign-compare -I$(LSLBASE)/include
LDFLAGS+=-L$(LSLBASE)/lib -llsl32 -lboost_system -lpthread
LDFLAGS+=-lpigpio
CXX=g++
......
// Hello World server
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
int main (void)
{
// Socket to talk to clients
void *context = zmq_ctx_new ();
void *responder = zmq_socket (context, ZMQ_REP);
int rc = zmq_bind (responder, "tcp://*:5555");
assert (rc == 0);
while (1) {
char buffer [10];
int n = zmq_recv (responder, buffer, 10, 0);
printf ("Received Hello (%d bytes)\n",n);
sleep (1); // Do some 'work'
zmq_send (responder, "World", 5, 0);
}
return 0;
}
// Hello World server
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
int main (void)
{
// Socket to talk to clients
void *context = zmq_ctx_new ();
void *responder = zmq_socket (context, ZMQ_REP);
int rc = zmq_bind (responder, "tcp://*:5555");
assert (rc == 0);
printf("go!\n");
while (1) {
zmq_msg_t msg;
int rc = zmq_msg_init (&msg);
int n = zmq_msg_recv (&msg, responder, 0);
printf ("Received Hello (%d bytes)\n",zmq_msg_size(&msg));
sleep (1); // Do some 'work'
zmq_send (responder, "World", 5, 0);
}
return 0;
}
// Hello World client
#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
int main (void)
{
printf ("Connecting to hello world server\n");
void *context = zmq_ctx_new ();
void *requester = zmq_socket (context, ZMQ_REQ);
zmq_connect (requester, "tcp://raspi4:5555");
getchar();
int request_nbr;
for (request_nbr = 0; request_nbr != 10; request_nbr++) {
char buffer [10];
printf ("Sending Hello %d\n", request_nbr);
zmq_send (requester, "Hello", 5, 0);
zmq_recv (requester, buffer, 10, 0);
printf ("Received World %d\n", request_nbr);
}
zmq_close (requester);
zmq_ctx_destroy (context);
return 0;
}
No preview for this file type
......@@ -8,8 +8,7 @@
// makes a difference at which point in the code you set
// the real time scheduling options
#include <zmq.h>
#include "zhelpers.hpp"
#include <zmq.hpp>
#include <lsl_cpp.h>
#include <iostream>
#include <mutex>
......@@ -149,24 +148,34 @@ int main (int argc, const char *argv[])
// Shunt messages out to our own subscribers
while (1) {
while (1) {
int part = 0;
int more = 1;
do {
zmq::message_t message;
int more;
size_t more_size = sizeof (more);
// Process all parts of the message
frontend.recv(&message);
int msgsize = message.size();
part++;
frontend.getsockopt( ZMQ_RCVMORE, &more, &more_size);
backend.send(message, more? ZMQ_SNDMORE: 0);
if (!more) {
std::string str((char*)message.data());
if (part==1) {
std::string str((char*)message.data(), msgsize);
outlet->push_sample(&str);
if (verbose)
cout << "Received: " << str << endl;
break; // Last message part
if (verbose) {
cout << "size=" << msgsize << endl;
cout << "Received: " << str << endl;
}
}
else {
if (verbose) {
std::string str((char*)message.data(), msgsize);
cout << "size=" << msgsize << endl;
cout << "Received another message part..." << str << endl;
}
}
}
} while (more);
}
return 0;
}
This diff is collapsed.
......@@ -209,61 +209,98 @@ int rtpriority(int n)
return sched_setscheduler (0, sched_policy, &sched) ;
}
int main(int argc, const char* argv[])
{
parse_options(argc, argv);
if (rtpriority(sched_priority) < 0) {
perror("rtpriority");
return 1;
}
int initialize_gpio()
{
// Prepare sound output
int r = gpioInitialise(); // use Broadcom pin numbering
if (r < 0)
{
perror("gpioInitialise");
return 1;
return r;
}
printf("Press control C to stop.\n");
gpioSetSignalFunc(SIGINT, stop);
gpioSetSignalFunc(SIGTERM, stop);
gpioSetSignalFunc(SIGQUIT, stop);
gpioSetSignalFunc(SIGABRT, stop);
printf("Press control C to stop.\n");
if (gpioSetMode(chan_right, PI_ALT0) < 0) {
if ((r = gpioSetMode(chan_right, PI_ALT0)) < 0) {
perror("gpioSetMode");
return 1;
return r;
}
if (gpioSetMode(chan_left, PI_ALT0) < 0) {
if ((r = gpioSetMode(chan_left, PI_ALT0)) < 0) {
perror("gpioSetMode");
return 1;
return r;
}
if (gpioSetMode(doutpin, PI_OUTPUT) < 0) {
if ((r = gpioSetMode(doutpin, PI_OUTPUT)) < 0) {
perror("gpioSetMode");
return 1;
return r;
}
if (gpioSetPullUpDown(doutpin, PI_PUD_UP) < 0) {
if ((r = gpioSetPullUpDown(doutpin, PI_PUD_UP)) < 0) {
perror("gpioSetMode");
return 1;
return r;
}
if (gpioSetMode(dinpin, PI_OUTPUT) < 0) {
if ((r = gpioSetMode(dinpin, PI_OUTPUT)) < 0) {
perror("gpioSetMode");
return 1;
return r;
}
if (gpioSetPullUpDown(dinpin, PI_PUD_UP) < 0) {
if ((r = gpioSetPullUpDown(dinpin, PI_PUD_UP)) < 0) {
perror("gpioSetMode");
return 1;
return r;
}
if (gpioSetISRFunc(dinpin, FALLING_EDGE, 10000, gpioISR) < 0)
if ((r = gpioSetISRFunc(dinpin, FALLING_EDGE, 10000, gpioISR)) < 0)
{
perror("gpioSetISRFunc");
return r;
}
return 0;
}
int zmq_recv_multi(zmq::socket_t &socket, zmq::message_t parts[], int nmax)
{
int64_t more;
size_t more_size = sizeof more;
int n = 0;
do {
if (n < nmax) {
socket.recv(&parts[n]);
if (verbose)
cout << "recv part " << n << endl;
n++;
}
else {
zmq::message_t discard;
socket.recv(&discard);
if (verbose)
cout << "recv part " << n << " (discarded)." << endl;
}
socket.getsockopt(ZMQ_RCVMORE, &more, &more_size);
} while (more);
return n;
}
int main(int argc, const char* argv[])
{
parse_options(argc, argv);
if (rtpriority(sched_priority) < 0) {
perror("rtpriority");
return 1;
}
if(initialize_gpio() < 0)
{
return 1;
}
......@@ -295,19 +332,50 @@ int main(int argc, const char* argv[])
// try {
while (run)
{
zmq::message_t request;
const int maxmsg=3;
zmq::message_t msg[maxmsg];
zmq::message_t& request=msg[0];
int nmsg = 0;
if (verbose)
cout << "polling" << endl;
#if 0
srep.recv(&request);
nmsg=1;
const char* rep = "OK\0";
srep.send(rep, 3);
#else
zmq::poll(items, 2, -1);
if (verbose)
cout << "done" << endl;
zmq::poll (&items [0], 2, -1);
if (items [0].revents & ZMQ_POLLIN) {
//nmsg=zmq_recv_multi(srep, msg, maxmsg);
nmsg=1;
srep.recv(&request);
srep.send(request);
const char* rep = "OK\0";
srep.send(rep, 3);
if (verbose)
cout << "sent response back" << endl;
}
else
if (items [1].revents & ZMQ_POLLIN) {
ssub.recv(&request);
nmsg=zmq_recv_multi(ssub, msg, maxmsg);
//ssub.recv(&request);
}
else
cout << "zmq::poll: unexpected event" << endl;
#endif
std::string str((char*)request.data());
if (verbose)
cout << "Received: " << str << endl;
if (verbose) {
cout << "Received: " << str << " (" << request.size() << " bytes)" << endl;
if (nmsg > 1) {
for (int i=1; i<nmsg; i++)
cout << " ...one more message, " << msg[i].size() << " bytes" << endl;
}
}
std::istringstream s(str);
std::string cmd;
......@@ -376,7 +444,7 @@ int main(int argc, const char* argv[])
printf("\nmopping up\n");
r=gpioHardwarePWM(pwmpin, 0, 0);
int r=gpioHardwarePWM(pwmpin, 0, 0);
if (r < 0)
{
perror("gpioHardwarePWM");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment