Commit 1a60cf5e authored by Your Name's avatar Your Name
Browse files

.

parent acbb66c3
......@@ -68,6 +68,7 @@ Reboot and login again.
$ sudo apt-get update
$ sudo apt-get install git libboost-all-dev cmake wiringpi pigpio libzmq3-dev
$ sudo apt install libasound2 libasound2-data libasound2-dev
$ cd
$ git clone --recurse-submodules https://gitlab.science.ru.nl/biophysics-labs/firmware.git
......
......@@ -3,17 +3,19 @@ CPPFLAGS+=-Wall -Wno-sign-compare -I$(LSLBASE)/include
LDFLAGS+=-L$(LSLBASE)/lib -llsl32 -lboost_system -lpthread
LDFLAGS+=-lpigpio
CXX=g++
CC=g++
CC=gcc
LDLIBS+=-lzmq -lpthread -lboost_system -lboost_regex -lboost_program_options
#INCLUDE+=RS-232
CXXFLAGS+=-Wall -std=c++11
all: zmq_trigger zmq_trigger_via_broker playtone msgqueue_pubsub zmq_trigger_subscriber
all: zmq_trigger playtone msgqueue_pubsub zmq_trigger_subscriber playsound
pcm_min playsound: LDLIBS+=-lm -lasound
playsound: playsound.c
pcm_min: pcm_min.c
playtone: playtone.cc
zmq_trigger: zmq_trigger.cc
zmq_trigger_via_broker: zmq_trigger_via_broker.cc
msgqueue_pubsub: msgqueue_pubsub.cc
zmq_trigger_subscriber: zmq_trigger_subscriber.cc
amixer -c 1 set PCM 100%
//
// Simple message queuing broker in C++
// Same as request-reply broker but using QUEUE device
//
// Olivier Chamoux <olivier.chamoux@fr.thalesgroup.com>
#include "zhelpers.hpp"
int main (int argc, char *argv[])
{
zmq::context_t context(1);
// Socket facing clients
zmq::socket_t frontend (context, ZMQ_ROUTER);
frontend.bind("tcp://*:5559");
// Socket facing services
zmq::socket_t backend (context, ZMQ_DEALER);
backend.bind("tcp://*:5560");
// Start the proxy
zmq::proxy(static_cast<void*>(frontend),
static_cast<void*>(backend),
nullptr);
return 0;
}
File added
/*
* This extra small demo sends a random samples to your speakers.
*/
#include <alsa/asoundlib.h>
static char *device = "default"; /* playback device */
snd_output_t *output = NULL;
unsigned char buffer[16*1024]; /* some random data */
int main(void)
{
int err;
unsigned int i;
snd_pcm_t *handle;
snd_pcm_sframes_t frames;
for (i = 0; i < sizeof(buffer); i++)
buffer[i] = random() & 0xff;
if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
if ((err = snd_pcm_set_params(handle,
SND_PCM_FORMAT_U8,
SND_PCM_ACCESS_RW_INTERLEAVED,
1,
48000,
1,
500000)) < 0) { /* 0.5sec */
printf("Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
for (i = 0; i < 16; i++) {
frames = snd_pcm_writei(handle, buffer, sizeof(buffer));
if (frames < 0)
frames = snd_pcm_recover(handle, frames, 0);
if (frames < 0) {
printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
break;
}
if (frames > 0 && frames < (long)sizeof(buffer))
printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames);
}
snd_pcm_close(handle);
return 0;
}
This diff is collapsed.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <pigpio.h>
#if 1
const int left = 13; // PWM1 audio to jack
const int right = 12; // PWM0 audio to jack
#else
const int left = 18; // PWM1 audio to jack
const int right = 19; // PWM0 audio to jack
#endif
int pin = 13;
int freq = 1000;
int dt = 1000;
volatile int run;
void stop(int signum)
{
run = 0;
}
int main(int argc,char *argv[])
{
int r;
r = gpioCfgClock(10, 0, 0);
if (r < 0)
{
perror("gpioCfgClock");
return 1;
}
r = gpioInitialise(); // use Broadcom pin numbering
if (r < 0)
{
perror("gpioInitialise");
return 1;
}
gpioSetSignalFunc(SIGINT, stop);
printf("Press control C to stop.\n");
if (gpioSetMode(pin, PI_ALT0) < 0) {
perror("gpioSetMode");
return 1;
}
int d;
if ((d=gpioPWM(pin, 128))<0) {
perror("gpioPWM");
return 1;
}
d=gpioGetPWMdutycycle(pin);
int f;
if ((f=gpioSetPWMfrequency(pin, freq)) < 0) {
perror("gpioSetPWMfrequency");
return 1;
}
printf("PWM freq = %d, duty cycle = %d\n", f, d);
run = 1;
while(run)
{
time_sleep(0.01);
}
printf("\ntidying up\n");
gpioSetPWMfrequency(pin, 0);
gpioPWM(pin, 0);
gpioTerminate();
r = gpioCfgClock(5, 0, 0);
gpioInitialise();
gpioTerminate();
}
#include <stdio.h>
#include <wiringPi.h>
#include <softTone.h>
#if 0
const int left = 13; // PWM1 audio to jack
const int right = 12; // PWM0 audio to jack
#else
const int left = 18; // PWM1 audio to jack
const int right = 19; // PWM0 audio to jack
#endif
int pin = right;
int freq = 1000;
int dt = 1000;
int main(int argc,char *argv[])
{
int r = wiringPiSetupGpio(); // use Broadcom pin numbering
if (r < 0)
perror("wiringPiSetupGpio");
pinMode(pin, SOFT_TONE_OUTPUT);
r = softToneCreate(pin);
if (r < 0)
perror("wiringPiSetupGpio");
softToneWrite(pin, freq);
delay(dt);
softToneStop(pin);
}
#!/bin/bash
#run this as root
cd /home/pi/firmware/lsldert
export LD_LIBRARY_PATH=../labstreaminglayer/build/install/lib
./zmq_trigger_via_broker
//
// Hello World server in C++ with azmq and boost
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//
#include <lsl_cpp.h>
#include <memory>
#include <iostream>
#include <iomanip>
#include <azmq/socket.hpp>
#include <boost/asio.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/program_options.hpp>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <pigpio.h>
using namespace std;
using namespace boost;
using namespace lsl;
const int chan_left = 13; // PWM1 audio to jack
const int chan_right = 12; // PWM0 audio to jack
int pin = chan_left;
int freq = 400;
int dt = 1000;
stream_outlet *outlet;
volatile int run=1 ;
void stop(int signum)
{
run = 0;
}
static int verbose = 0;
void parse_options(int argc, const char* argv[])
{
namespace po = boost::program_options;
// Declare the supported options.
po::options_description desc("Usage:");
desc.add_options()
("help,h", "show this message")
("channel,c", po::value<char>(), "select channel")
("verbose,v", "enable verbose output")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
exit(0);
}
if (vm.count("verbose")) {
verbose = 1;
}
if (vm.count("channel")) {
char c = vm["channel"].as<char>();
switch (c) {
case 'l': case 'L':
pin = chan_left;
break;
case 'r': case 'R':
pin = chan_right;
break;
default:
cerr << "channel must be 'L' or 'R'" << endl;
exit(1);
}
}
}
std::string str2hex(const std::string& s)
{
const static char str2hex_lookup[] = "0123456789abcdef";
unsigned int i=0,leng=s.length();
std::stringstream r;
for(i=0; i<leng; i++)
{
r<< str2hex_lookup[ s[i] >> 4 ];
r<< str2hex_lookup[ s[i] & 0x0f ];
}
return r.str();
}
void beep(int freq, int durationmsec)
{
int d=500000;
int r=gpioHardwarePWM(pin, freq, d);
if (r < 0)
{
perror("gpioHardwarePWM");
exit(1);
}
std::this_thread::sleep_for(std::chrono::milliseconds(durationmsec));
r=gpioHardwarePWM(pin, 0, 0);
if (r < 0)
{
perror("gpioHardwarePWM");
exit(1);
}
}
int main(int argc, const char* argv[])
{
parse_options(argc, argv);
// Prepare sound output
int r = gpioInitialise(); // use Broadcom pin numbering
if (r < 0)
{
perror("gpioInitialise");
return 1;
}
gpioSetSignalFunc(SIGINT, stop);
printf("Press control C to stop.\n");
if (gpioSetMode(chan_right, PI_ALT0) < 0) {
perror("gpioSetMode");
return 1;
}
if (gpioSetMode(chan_left, PI_ALT0) < 0) {
perror("gpioSetMode");
return 1;
}
// Prepare our context and socket
asio::io_service ios;
azmq::socket socket(ios, ZMQ_REP);
// socket.bind("tcp://*:5555");
socket.bind("tcp://localhost:5560");
char hostname[255];
gethostname(hostname,255);
// make a new stream_info and open an outlet with it
char info_name[255];
sprintf(info_name, "Digital Triggers");
char info_type[512];
sprintf(info_type,"Digital Triggers @ %s",hostname);
stream_info info(info_name, info_type, 1, lsl::IRREGULAR_RATE,
lsl::cf_int8,"Raspberry Pi Digital Triggers");
outlet = new stream_outlet(info);
while (run)
{
std::array<char,4096> request;
// Wait for next request from client
socket.receive(asio::buffer(request));
std::string str(request.data());
int sample = 1;
outlet->push_sample(&sample);
if (verbose)
cout << "Received: " << str << endl;
// Return result
std::string result = str;
socket.send(asio::buffer(result));
if (str == "b")
beep(freq, 500);
}
printf("\nmopping up\n");
r=gpioHardwarePWM(pin, 0, 0);
if (r < 0)
{
perror("gpioHardwarePWM");
return 1;
}
gpioTerminate();
return 0;
}
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