Mercurial > hg > beaglert
changeset 24:ad5cd8dd99b3 bbb_network
UDP communication in place, pre-alpha
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Fri, 08 May 2015 11:12:13 +0100 |
parents | 182ae9367104 |
children | 98aed580452a |
files | .cproject core/RTAudio.cpp core/RTAudioCommandLine.cpp core/client.cpp include/RTAudio.h include/RTAudioSettings.h include/client.h include/render.h projects/basic/main.cpp projects/basic/render.cpp projects/basic_network/main.cpp projects/basic_network/render.cpp resources/udp-client.c resources/udp-server.c |
diffstat | 14 files changed, 536 insertions(+), 42 deletions(-) [+] |
line wrap: on
line diff
--- a/.cproject Tue May 05 17:28:00 2015 +0100 +++ b/.cproject Fri May 08 11:12:13 2015 +0100 @@ -38,6 +38,7 @@ <option id="gnu.cpp.compiler.option.other.other.1516989263" name="Other flags" superClass="gnu.cpp.compiler.option.other.other" useByScannerDiscovery="false" value="-c -fmessage-length=0 -Wpointer-arith -Wunused-result -D_GNU_SOURCE -D_REENTRANT -D__XENO__" valueType="string"/> <option id="gnu.cpp.compiler.option.warnings.allwarn.1392851614" name="All warnings (-Wall)" superClass="gnu.cpp.compiler.option.warnings.allwarn" useByScannerDiscovery="false" value="true" valueType="boolean"/> <option id="gnu.cpp.compiler.option.dialect.std.904910934" name="Language standard" superClass="gnu.cpp.compiler.option.dialect.std" useByScannerDiscovery="true" value="gnu.cpp.compiler.dialect.default" valueType="enumerated"/> + <option id="gnu.cpp.compiler.option.optimization.flags.1529774472" name="Other optimization flags" superClass="gnu.cpp.compiler.option.optimization.flags" value="" valueType="string"/> <inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.1166892316" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/> </tool> <tool command="arm-linux-gnueabihf-gcc" id="cdt.managedbuild.tool.gnu.c.compiler.exe.debug.917207395" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.exe.debug"> @@ -52,6 +53,7 @@ </option> <option id="gnu.c.compiler.option.misc.other.835792865" name="Other flags" superClass="gnu.c.compiler.option.misc.other" useByScannerDiscovery="false" value="-c -fmessage-length=0 -Wpointer-arith -Wunused-result -D_GNU_SOURCE -D_REENTRANT -D__XENO__ -std=gnu99" valueType="string"/> <option id="gnu.c.compiler.option.warnings.allwarn.1145989346" name="All warnings (-Wall)" superClass="gnu.c.compiler.option.warnings.allwarn" useByScannerDiscovery="false" value="true" valueType="boolean"/> + <option id="gnu.c.compiler.option.optimization.flags.458386628" name="Other optimization flags" superClass="gnu.c.compiler.option.optimization.flags" value="" valueType="string"/> <inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.308014221" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/> </tool> <tool command="arm-linux-gnueabihf-g++ " id="cdt.managedbuild.tool.gnu.c.linker.exe.debug.214461086" name="GCC C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.exe.debug"/> @@ -112,14 +114,14 @@ </extensions> </storageModule> <storageModule moduleId="cdtBuildSystem" version="4.0.0"> - <configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release,org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.exe.release.1521194538" name="Release" parent="cdt.managedbuild.config.gnu.exe.release" postannouncebuildStep="Stopping process on BBB and copying new binary" postbuildStep="ssh root@192.168.7.2 "kill -s 2 \`pidof ${BuildArtifactFileName}\` 2>/dev/null"; scp ${BuildArtifactFilePrefix}${BuildArtifactFileName} root@192.168.7.2:~/beaglert/ ; echo 'done copying' | wall"> + <configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release,org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.exe.release.1521194538" name="Release" parent="cdt.managedbuild.config.gnu.exe.release" postannouncebuildStep="Stopping process on BBB and copying new binary" postbuildStep="ssh root@192.168.7.2 "kill -s 2 \`pidof ${BuildArtifactFileName}\` 2>/dev/null"; scp ${BuildArtifactFilePrefix}${BuildArtifactFileName} root@192.168.7.2:~/beaglert/ && echo 'done copying' | wall || echo 'error' | wall"> <folderInfo id="cdt.managedbuild.config.gnu.exe.release.1521194538." name="/" resourcePath=""> <toolChain id="cdt.managedbuild.toolchain.gnu.exe.release.1612059942" name="Linux GCC" superClass="cdt.managedbuild.toolchain.gnu.exe.release"> <targetPlatform id="cdt.managedbuild.target.gnu.platform.exe.release.908983575" name="Debug Platform" superClass="cdt.managedbuild.target.gnu.platform.exe.release"/> <builder buildPath="${workspace_loc:/BBB_audio+input/Release}" id="cdt.managedbuild.target.gnu.builder.exe.release.511190290" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="Gnu Make Builder" superClass="cdt.managedbuild.target.gnu.builder.exe.release"/> <tool command="arm-linux-gnueabihf-g++ " id="cdt.managedbuild.tool.gnu.archiver.base.810674388" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.base"/> <tool command="arm-linux-gnueabihf-g++ " commandLinePattern="${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}" id="cdt.managedbuild.tool.gnu.cpp.compiler.exe.release.163790048" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.exe.release"> - <option id="gnu.cpp.compiler.exe.release.option.optimization.level.2031085570" name="Optimization Level" superClass="gnu.cpp.compiler.exe.release.option.optimization.level" value="gnu.cpp.compiler.optimization.level.more" valueType="enumerated"/> + <option id="gnu.cpp.compiler.exe.release.option.optimization.level.2031085570" name="Optimization Level" superClass="gnu.cpp.compiler.exe.release.option.optimization.level" value="gnu.cpp.compiler.optimization.level.most" valueType="enumerated"/> <option id="gnu.cpp.compiler.exe.release.option.debugging.level.701035863" name="Debug Level" superClass="gnu.cpp.compiler.exe.release.option.debugging.level" value="gnu.cpp.compiler.debugging.level.none" valueType="enumerated"/> <option id="gnu.cpp.compiler.option.include.paths.823255770" name="Include paths (-I)" superClass="gnu.cpp.compiler.option.include.paths" valueType="includePath"> <listOptionValue builtIn="false" value="/usr/xenomai/include"/> @@ -129,10 +131,11 @@ <listOptionValue builtIn="false" value="/usr/arm-linux-gnueabihf/include/ne10"/> </option> <option id="gnu.cpp.compiler.option.dialect.std.776548502" name="Language standard" superClass="gnu.cpp.compiler.option.dialect.std" value="gnu.cpp.compiler.dialect.default" valueType="enumerated"/> + <option id="gnu.cpp.compiler.option.optimization.flags.536899313" name="Other optimization flags" superClass="gnu.cpp.compiler.option.optimization.flags" value="" valueType="string"/> <inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.601059736" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/> </tool> <tool command="arm-linux-gnueabihf-gcc" commandLinePattern="${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}" id="cdt.managedbuild.tool.gnu.c.compiler.exe.release.1302828968" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.exe.release"> - <option defaultValue="gnu.c.optimization.level.most" id="gnu.c.compiler.exe.release.option.optimization.level.1118673208" name="Optimization Level" superClass="gnu.c.compiler.exe.release.option.optimization.level" value="gnu.c.optimization.level.more" valueType="enumerated"/> + <option defaultValue="gnu.c.optimization.level.most" id="gnu.c.compiler.exe.release.option.optimization.level.1118673208" name="Optimization Level" superClass="gnu.c.compiler.exe.release.option.optimization.level" value="gnu.c.optimization.level.most" valueType="enumerated"/> <option id="gnu.c.compiler.exe.release.option.debugging.level.1329456558" name="Debug Level" superClass="gnu.c.compiler.exe.release.option.debugging.level" value="gnu.c.debugging.level.none" valueType="enumerated"/> <option id="gnu.c.compiler.option.include.paths.1269113471" name="Include paths (-I)" superClass="gnu.c.compiler.option.include.paths" valueType="includePath"> <listOptionValue builtIn="false" value="/usr/xenomai/include"/> @@ -142,6 +145,7 @@ <listOptionValue builtIn="false" value="/usr/arm-linux-gnueabihf/include/ne10"/> </option> <option id="gnu.c.compiler.option.include.files.40806287" name="Include files (-include)" superClass="gnu.c.compiler.option.include.files"/> + <option id="gnu.c.compiler.option.optimization.flags.1921373993" name="Other optimization flags" superClass="gnu.c.compiler.option.optimization.flags" value="" valueType="string"/> <inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.574072828" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/> </tool> <tool command="arm-linux-gnueabihf-g++ " id="cdt.managedbuild.tool.gnu.c.linker.exe.release.281634187" name="GCC C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.exe.release"/> @@ -177,7 +181,7 @@ <sourceEntries> <entry excluding="audio_routines_old.S" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="core"/> <entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="include"/> - <entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="projects/analogDigitalDemo"/> + <entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="projects/basic_network"/> <entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="source"/> </sourceEntries> </configuration>
--- a/core/RTAudio.cpp Tue May 05 17:28:00 2015 +0100 +++ b/core/RTAudio.cpp Fri May 08 11:12:13 2015 +0100 @@ -31,6 +31,7 @@ #include "../include/I2c_Codec.h" #include "../include/render.h" #include "../include/GPIOcontrol.h" +#include "../include/client.h" using namespace std; @@ -81,6 +82,7 @@ // // Returns 0 on success. + int BeagleRT_initAudio(RTAudioSettings *settings, void *userData) { rt_print_auto_init(1); @@ -184,7 +186,7 @@ settings->useAnalog ? settings->periodSize : 0, /* analog period size */ audioPeriodSize, analogSampleRate, audioSampleRate, - userData)) { + userData, settings)) { cout << "Couldn't initialise audio rendering\n"; return 1; }
--- a/core/RTAudioCommandLine.cpp Tue May 05 17:28:00 2015 +0100 +++ b/core/RTAudioCommandLine.cpp Fri May 08 11:12:13 2015 +0100 @@ -10,6 +10,7 @@ #include <cstring> #include <getopt.h> #include "../include/RTAudio.h" + #ifndef OPT_PRU_FILE #define OPT_PRU_FILE -1 #endif @@ -22,15 +23,19 @@ {"use-analog", 1, NULL, 'm'}, {"use-digital-gpio", 1, NULL, 'g'}, {"analog-channels", 1, NULL, 'C'}, - {"digital-gpio-channels", 1, NULL, 'G'}, + {"digital-channels", 1, NULL, 'G'}, {"mute-speaker", 1, NULL, 'M'}, {"dac-level", 1, NULL, 'D'}, {"adc-level", 1, NULL, 'A'}, {"hp-level", 1, NULL, 'H'}, + {"receive-port", 1, NULL, 'r'}, + {"transmit-port", 1, NULL, 't'}, + {"server-name",1,NULL,'s'}, {"pru-file",1,NULL,OPT_PRU_FILE}, {NULL, 0, NULL, 0} }; -const char gDefaultShortOptions[] = "p:vm:M:C:D:A:H:g:G:"; + +const char gDefaultShortOptions[] = "p:vm:M:C:D:A:H:g:G:r:t:s:"; // This function sets the default settings for the RTAudioSettings structure void BeagleRT_defaultSettings(RTAudioSettings *settings) @@ -48,6 +53,9 @@ settings->verbose = 0; settings->pruFilename[0]='\0'; settings->codecI2CAddress = CODEC_I2C_ADDRESS; + settings->receivePort=9998; + settings->transmitPort=9999; + strcpy(settings->serverName, "127.0.0.1"); settings->ampMutePin = kAmplifierMutePin; } @@ -167,7 +175,20 @@ case 'H': settings->headphoneLevel = atof(optarg); break; - case OPT_PRU_FILE: + case 'r': + settings->receivePort = atoi(optarg); + break; + case 't': + settings->transmitPort = atoi(optarg); + break; + case 's': + if(strlen(optarg)<MAX_SERVERNAME_LENGTH) + strcpy(settings->serverName, optarg); + else + std::cerr << "Warning: server name is too long (>" << MAX_SERVERNAME_LENGTH << " characters)." + " Using default severName Instead ( " << settings->serverName << " ).\n"; + break; + case 'OPT_PRU_FILE': if(strlen(optarg)<MAX_PRU_FILENAME_LENGTH) strcpy(settings->pruFilename, optarg); else @@ -190,10 +211,13 @@ std::cerr << " --hp-level [-H] dBs: Set the headphone output level (0dB max; -63.5dB min)\n"; std::cerr << " --mute-speaker [-M] val: Set whether to mute the speaker initially (default: no)\n"; std::cerr << " --use-analog [-m] val: Set whether to use ADC/DAC analog (default: yes)\n"; - std::cerr << " --use-gpio-analog [-g] val: Set whether to use GPIO analog (default: yes)\n"; + std::cerr << " --use-digital [-g] val: Set whether to use digital GPIO channels (default: yes)\n"; std::cerr << " --analog-channels [-C] val: Set the number of ADC/DAC channels (default: 8)\n"; - std::cerr << " --analog-gpio-channels [-G] val: Set the number of GPIO channels (default: 16)\n"; - std::cerr << " --pru-file val: Set an optional external file to use for the PRU binary code\n"; + std::cerr << " --digital-channels [-G] val: Set the number of digital GPIO channels (default: 16)\n"; + std::cerr << " --receive-port [-r] val: Set the receive port (default: 9998)\n"; + std::cerr << " --transmit-port [-t] val: Set the transmit port (default: 9999)\n"; + std::cerr << " --server-name [-s] val: Set the destination server name (default: '127.0.0.1')\n"; + std::cerr << " --pru-file val: Set an optional external file to use for the PRU binary code\n"; std::cerr << " --verbose [-v]: Enable verbose logging information\n"; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/client.cpp Fri May 08 11:12:13 2015 +0100 @@ -0,0 +1,129 @@ +///* UDP client in the internet domain */ + +#include <ctype.h> +#include <sys/time.h> +#include <fcntl.h> +#include "../include/client.h" +#include <unistd.h> +#include <rtdk.h> + +#define BUFF_LEN 1024 +#define MAX_VAR_STRING 10 + +struct sockaddr_in outServer, inServer; +int outSock, inSock, n, length; +socklen_t fromlen; +struct sockaddr_in from; +char inBuffer[1024]; +char variableString[MAX_VAR_STRING]; + +int setupSockets(int receivePort, int transmitPort, char const*serverName){ + //setup transmitter + printf("receivePort: %d; transmitPort: %d; serverName: %s\n",receivePort, transmitPort, serverName); + outSock= socket(AF_INET, SOCK_DGRAM, 0); + outServer.sin_port = htons(transmitPort); + if (outSock < 0){ + error("Opening out socket"); + return -1; + } + outServer.sin_family = AF_INET; + inet_pton(AF_INET,serverName,&outServer.sin_addr); + + //setup receiver + inSock=socket(AF_INET, SOCK_DGRAM, 0); + if (inSock < 0){ + return -1; + error("Opening in socket"); + } + length = sizeof(inServer); + inServer.sin_family=AF_INET; + inServer.sin_addr.s_addr=INADDR_ANY; + inServer.sin_port=htons(receivePort); + if (bind(inSock,(struct sockaddr *)&inServer,length)<0) + error("binding"); + fromlen = sizeof(struct sockaddr_in); + return 0; +} +int sendMessage(networkData message) +{ + unsigned int length; + char buffer[BUFF_LEN]; + length=sizeof(struct sockaddr_in); + int k=0; + k=sprintf(buffer+k, "%8d;",*message.counter); + for(int j=0; j<message.numVariables; j++){ + k+=sprintf(buffer+k, "%.3f;",*message.variables[j]); + if(k>BUFF_LEN - 20) //safety margin + continue; + } + sprintf(buffer+k,"\n"); + // printf(buffer); + n=sendto(outSock,buffer, + strlen(buffer),0,(const struct sockaddr *)&outServer,length); + if (n < 0) error("Sendto"); + return 0; +} +/* +int receiveMessage() +{ + int n = recvfrom(inSock,inBuffer,1024,0,(struct sockaddr *)&from,&fromlen); + if (n < 0) error("recvfrom"); + printf("Received a datagram: "); + printf(inBuffer); + } +*/ + + +int receiveMessage(networkData message){ + struct timeval stTimeOut; + fd_set stReadFDS; + FD_ZERO(&stReadFDS); + // Timeout of one second + stTimeOut.tv_sec = 0; + stTimeOut.tv_usec = 1000; + FD_SET(inSock, &stReadFDS); + + int t = select(inSock+1, &stReadFDS, NULL, NULL, &stTimeOut); + if (t == -1) { + rt_fprintf(stderr, "Call to select() failed"); + return -1; + } + else if (t != 0) { + if (FD_ISSET(inSock, &stReadFDS)) { +// printf("There is data pending to be read..."); // Read data with recv() + int n = recvfrom(inSock,inBuffer,1024,0,(struct sockaddr *)&from,&fromlen); + if (n < 0){ + rt_fprintf(stderr,"Error while receiving"); + return -1; + } + printf("Received a datagram: "); + printf(inBuffer); + //the worst parser ever + int previousN=0; + int currentVariable=0; + for(int n=0; inBuffer[n]!=0 && currentVariable<message.numVariables && n-previousN<MAX_VAR_STRING;n++){ //scan the string + if(inBuffer[n]==';'||n==0){ // if you find a separator or you are at the end of the string, parse the variable + int j=0; + inBuffer[n]=0; //set the semicolon to 0 ... + while( (variableString[j++]=inBuffer[previousN++]) ); // ... so that this will stop when it gets there + rt_printf("variable %d: %s\n", currentVariable, variableString); + *(message.variables[currentVariable])=atof(variableString); + n++; //increment to step after the semicolon + previousN=n; + currentVariable++; + } + } + } + } + return 0; +} + +void closeSockets(){ + close(outSock); + close(inSock); +} +void error(const char *msg) +{ + perror(msg); + exit(0); +}
--- a/include/RTAudio.h Tue May 05 17:28:00 2015 +0100 +++ b/include/RTAudio.h Fri May 08 11:12:13 2015 +0100 @@ -13,7 +13,7 @@ #ifndef RTAUDIO_H_ #define RTAUDIO_H_ - +#include "RTAudioSettings.h" #include "render.h" // Useful constants @@ -29,33 +29,11 @@ #define DEFAULT_DAC_LEVEL 0.0 #define DEFAULT_ADC_LEVEL -6.0 #define DEFAULT_HP_LEVEL -6.0 -#define MAX_PRU_FILENAME_LENGTH 256 enum { kAmplifierMutePin = 61 // P8-26 controls amplifier mute }; -// Structure which contains initialisation parameters for the -// real-time audio system -typedef struct { - // These items might be adjusted by the user: - int periodSize; // Number of (analog) frames per period; audio is twice this - int beginMuted; // Whether to begin with the speakers muted - float dacLevel; // Level for the audio DAC output - float adcLevel; // Level for the audio ADC input - float headphoneLevel; // Level for the headphone output - int useAnalog; // Whether to use the analog - int useDigital; // Whether to use the 16 programmable GPIOs - int numAnalogChannels; // How many channels for the ADC and DAC - int numDigitalChannels; // How many channels for the GPIOs - int verbose; // Whether to use verbose logging - char pruFilename[MAX_PRU_FILENAME_LENGTH]; //the external .bin file to load. If empty will use PRU code from pru_rtaudio_bin.h - // These items are hardware-dependent and should only be changed - // to run on different hardware - int codecI2CAddress; // Where the codec can be found on the I2C bus - int ampMutePin; // Pin where amplifier mute can be found -} RTAudioSettings; - typedef void* AuxiliaryTask; // Opaque data type to keep track of aux tasks // Flag that indicates when the audio will stop; can be read or
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/RTAudioSettings.h Fri May 08 11:12:13 2015 +0100 @@ -0,0 +1,40 @@ +/* + * RTaudioSettings.h + * + * Created on: 6 May 2015 + * Author: unmanaged + */ + +#ifndef RTAUDIOSETTINGS_H_ +#define RTAUDIOSETTINGS_H_ + +#define MAX_PRU_FILENAME_LENGTH 256 +#define MAX_SERVERNAME_LENGTH 256 +// Structure which contains initialisation parameters for the +// real-time audio system +typedef struct { + // These items might be adjusted by the user: + int periodSize; // Number of (analog) frames per period; audio is twice this + int beginMuted; // Whether to begin with the speakers muted + float dacLevel; // Level for the audio DAC output + float adcLevel; // Level for the audio ADC input + float headphoneLevel; // Level for the headphone output + int useAnalog; // Whether to use the analog + int useDigital; // Whether to use the 16 programmable GPIOs + int numAnalogChannels; // How many channels for the ADC and DAC + int numDigitalChannels; // How many channels for the GPIOs + int verbose; // Whether to use verbose logging + char pruFilename[MAX_PRU_FILENAME_LENGTH]; //the external .bin file to load. If empty will use PRU code from pru_rtaudio_bin.h + // These items are hardware-dependent and should only be changed + // to run on different hardware + int codecI2CAddress; // Where the codec can be found on the I2C bus + int ampMutePin; // Pin where amplifier mute can be found + int receivePort; //port where the UDP server will listen + int transmitPort; //port where the UDP client will transmit + char serverName[MAX_SERVERNAME_LENGTH]; +} RTAudioSettings; + + + + +#endif /* RTAUDIOSETTINGS_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/client.h Fri May 08 11:12:13 2015 +0100 @@ -0,0 +1,22 @@ +/* UDP client in the internet domain */ +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <netdb.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> + +struct networkData{ + int *counter; + float *variables[16]; + int numVariables; +}; + +void error(const char *); +int setupSockets(int receivePort, int transmitPort, char const*serverName); +int sendMessage(networkData message); +int receiveMessage(networkData message); +void closeSockets();
--- a/include/render.h Tue May 05 17:28:00 2015 +0100 +++ b/include/render.h Fri May 08 11:12:13 2015 +0100 @@ -10,8 +10,10 @@ // uint types #include <stdint.h> -#include "../include/Utilities.h" -#include "../include/digital_gpio_mapping.h" +#include "Utilities.h" +#include "digital_gpio_mapping.h" +#include "RTAudioSettings.h" + // Mappings from pin numbers on PCB to actual DAC channels // This gives the DAC and ADC connectors the same effective pinout #define DAC_PIN0 6 @@ -41,7 +43,7 @@ int numAnalogFramesPerPeriod, int numAudioFramesPerPeriod, float analogSampleRate, float audioSampleRate, - void *userData); + void *userData, RTAudioSettings *settings); void render(int numAnalogFrames, int numAudioFrames, int numDigitalFrames, float *audioIn, float *audioOut, float *analogIn, float *analogOut, uint32_t *digital);
--- a/projects/basic/main.cpp Tue May 05 17:28:00 2015 +0100 +++ b/projects/basic/main.cpp Fri May 08 11:12:13 2015 +0100 @@ -4,7 +4,7 @@ * Created on: Oct 24, 2014 * Author: parallels */ - +#include <unistd.h> #include <iostream> #include <cstdlib> #include <libgen.h>
--- a/projects/basic/render.cpp Tue May 05 17:28:00 2015 +0100 +++ b/projects/basic/render.cpp Fri May 08 11:12:13 2015 +0100 @@ -22,11 +22,11 @@ // // Return true on success; returning false halts the program. -bool initialise_render(int numMatrixChannels, int numAudioChannels, +bool initialise_render(int numMatrixChannels, int numDigitalChannels, int numAudioChannels, int numMatrixFramesPerPeriod, int numAudioFramesPerPeriod, float matrixSampleRate, float audioSampleRate, - void *userData) + void *userData, RTAudioSettings* settings) { // Retrieve a parameter passed in from the initAudio() call gFrequency = *(float *)userData; @@ -42,8 +42,8 @@ // ADCs and DACs (if available). If only audio is available, numMatrixFrames // will be 0. -void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut, - uint16_t *matrixIn, uint16_t *matrixOut) +void render(int numAnalogFrames, int numAudioFrames, int numDigitalFrames, float *audioIn, float *audioOut, + float *analogIn, float *analogOut, uint32_t *digital) { for(int n = 0; n < numAudioFrames; n++) { float out = 0.8f * sinf(gPhase);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projects/basic_network/main.cpp Fri May 08 11:12:13 2015 +0100 @@ -0,0 +1,98 @@ +/* + * main.cpp + * + * Created on: Oct 24, 2014 + * Author: parallels + */ + +#include <iostream> +#include <cstdlib> +#include <libgen.h> +#include <signal.h> +#include <getopt.h> +#include "../../include/RTAudio.h" +#include <unistd.h> + +using namespace std; + +// Handle Ctrl-C by requesting that the audio rendering stop +void interrupt_handler(int var) +{ + gShouldStop = true; +} + +// Print usage information +void usage(const char * processName) +{ + cerr << "Usage: " << processName << " [options]" << endl; + + BeagleRT_usage(); + + cerr << " --frequency [-f] frequency: Set the frequency of the oscillator\n"; + cerr << " --help [-h]: Print this menu\n"; +} + +int main(int argc, char *argv[]) +{ + RTAudioSettings settings; // Standard audio settings + float frequency = 440.0; // Frequency of oscillator + + struct option customOptions[] = + { + {"help", 0, NULL, 'h'}, + {"frequency", 1, NULL, 'f'}, + {NULL, 0, NULL, 0} + }; + + // Set default settings + BeagleRT_defaultSettings(&settings); + + // Parse command-line arguments + while (1) { + int c; + if ((c = BeagleRT_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0) + break; + switch (c) { + case 'h': + usage(basename(argv[0])); + exit(0); + case 'f': + frequency = atof(optarg); + break; + case '?': + default: + usage(basename(argv[0])); + exit(1); + } + } + + // Initialise the PRU audio device + if(BeagleRT_initAudio(&settings, &frequency) != 0) { + cout << "Error: unable to initialise audio" << endl; + return -1; + } + + // Start the audio device running + if(BeagleRT_startAudio()) { + cout << "Error: unable to start real-time audio" << endl; + return -1; + } + + // Set up interrupt handler to catch Control-C and SIGTERM + signal(SIGINT, interrupt_handler); + signal(SIGTERM, interrupt_handler); + + // Run until told to stop + while(!gShouldStop) { + usleep(100000); + } + + // Stop the audio device + BeagleRT_stopAudio(); + + // Clean up any resources allocated for audio + BeagleRT_cleanupAudio(); + + // All done! + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projects/basic_network/render.cpp Fri May 08 11:12:13 2015 +0100 @@ -0,0 +1,89 @@ +/* + * render.cpp + * + * Created on: Oct 24, 2014 + * Author: parallels + */ + +#include "../../include/RTAudioSettings.h" +#include "../../include/render.h" +#include <cmath> +#include "../../include/client.h" +#include "../../include/RTAudio.h" // to schedule lower prio parallel process +#include <rtdk.h> +float gFrequency; +float gPhase; +float gInverseSampleRate; +int gCount=0; +networkData networkObject; +AuxiliaryTask transmitReceiveDataTask; + +void transmitReceiveData(){ + printf("transmitAndReceiveData\n"); + while(!gShouldStop){ + //sendMessage(networkObject); + //receiveMessage(networkObject); + usleep(10000); + } + closeSockets(); +} + +// initialise_render() is called once before the audio rendering starts. +// Use it to perform any initialisation and allocation which is dependent +// on the period size or sample rate. +// +// userData holds an opaque pointer to a data structure that was passed +// in from the call to initAudio(). +// +// Return true on success; returning false halts the program. +bool initialise_render(int numMatrixChannels, int numDigitalChannels, int numAudioChannels, + int numMatrixFramesPerPeriod, + int numAudioFramesPerPeriod, + float matrixSampleRate, float audioSampleRate, + void *userData, RTAudioSettings *settings) +{ + // Retrieve a parameter passed in from the initAudio() call + gFrequency = *(float *)userData; + + gInverseSampleRate = 1.0 / audioSampleRate; + gPhase = 0.0; + + networkObject.counter=&gCount; + networkObject.variables[0]=&gFrequency; + networkObject.variables[1]=&gPhase; + networkObject.numVariables=2; + setupSockets(settings->receivePort, settings->transmitPort, settings->serverName); + transmitReceiveDataTask=createAuxiliaryTaskLoop(*transmitReceiveData, 50, "transmit-receive-data"); + //scheduleAuxiliaryTask(transmitReceiveDataTask); //here it does not work + return true; +} + +// render() is called regularly at the highest priority by the audio engine. +// Input and output are given from the audio hardware and the other +// ADCs and DACs (if available). If only audio is available, numMatrixFrames +// will be 0. + +void render(int numAnalogFrames, int numAudioFrames, int numDigitalFrames, float *audioIn, float *audioOut, + float *analogIn, float *analogOut, uint32_t *digital) +{ + for(int n = 0; n < numAudioFrames; n++) { + float out = 0.7f * sinf(gPhase); + gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; + if(gPhase > 2.0 * M_PI) + gPhase -= 2.0 * M_PI; + + for(int channel = 0; channel < gNumAudioChannels; channel++) + audioOut[n * gNumAudioChannels + channel] = out; + if(gCount==0){ + scheduleAuxiliaryTask(transmitReceiveDataTask); + } + gCount++; + } +} + +// cleanup_render() is called once at the end, after the audio has stopped. +// Release any resources that were allocated in initialise_render(). + +void cleanup_render() +{ +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/udp-client.c Fri May 08 11:12:13 2015 +0100 @@ -0,0 +1,52 @@ +/* UDP client in the internet domain */ +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <netdb.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> + +void error(const char *); +int main(int argc, char *argv[]) +{ + int sock, n; + unsigned int length; + struct sockaddr_in server, from; + struct hostent *hp; + char buffer[256]; + + if (argc != 3) { printf("Usage: server port\n"); + exit(1); + } + + server.sin_family = AF_INET; + hp = gethostbyname(argv[1]); + if (hp==0) error("Unknown host"); + + bcopy((char *)hp->h_addr, + (char *)&server.sin_addr, + hp->h_length); + server.sin_port = htons(atoi(argv[2])); + length=sizeof(struct sockaddr_in); + while (1){ + sock= socket(AF_INET, SOCK_DGRAM, 0); + if (sock < 0) error("socket"); + bzero(buffer,256); + printf("Please enter the message: "); + fgets(buffer,255,stdin); + n=sendto(sock,buffer, + strlen(buffer),0,(const struct sockaddr *)&server,length); + if (n < 0) error("Sendto"); + } + close(sock); + return 0; +} + +void error(const char *msg) +{ + perror(msg); + exit(0); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/udp-server.c Fri May 08 11:12:13 2015 +0100 @@ -0,0 +1,54 @@ +/* Creates a datagram server. The port + number is passed as an argument. This + server runs forever */ + +#include <sys/types.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <string.h> +#include <netdb.h> +#include <stdio.h> + +void error(const char *msg) +{ + perror(msg); + exit(0); +} + +int main(int argc, char *argv[]) +{ + int sock, length, n; + socklen_t fromlen; + struct sockaddr_in server; + struct sockaddr_in from; + char buf[1024]; + + if (argc < 2) { + fprintf(stderr, "ERROR, no port provided\n"); + exit(0); + } + + sock=socket(AF_INET, SOCK_DGRAM, 0); + if (sock < 0) error("Opening socket"); + length = sizeof(server); + bzero(&server,length); + server.sin_family=AF_INET; + server.sin_addr.s_addr=INADDR_ANY; + server.sin_port=htons(atoi(argv[1])); + if (bind(sock,(struct sockaddr *)&server,length)<0) + error("binding"); + fromlen = sizeof(struct sockaddr_in); + while (1) { + n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen); + if (n < 0) error("recvfrom"); + write(1,"Received a datagram: ",21); + write(1,buf,n); + n = sendto(sock,"Got your message\n",17, + 0,(struct sockaddr *)&from,fromlen); + if (n < 0) error("sendto"); + } + return 0; + } +