Commit 808591d0 authored by Max-Ferdinand Suffel's avatar Max-Ferdinand Suffel
Browse files

added Teensy code, added further comments

parent 4c7f6ca3
/*
Optimized serial receiving:
https://www.pjrc.com/teensy/benchmark_usb_serial_receive.html
*/
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
AudioPlayQueue queue;
AudioOutputI2S speaker;
AudioConnection conn(queue, 0, speaker, 1);
AudioControlSGTL5000 audioShield;
#define RED_LED_PIN_1 3
#define RED_LED_PIN_2 21
#define RED_LED_PIN_3 20
#define BLUE_LED_PIN_1 4
#define BLUE_LED_PIN_2 5
#define PCM_BUFFER_SIZE (2*AUDIO_BLOCK_SAMPLES) // AUDIO_BLOCK_SAMPLES=128 --> PCM_BUFFER_SIZE=256
#define SYNTHESIS_BUFFER_SIZE 4096
#define NUM_PCM_BUFFERS (SYNTHESIS_BUFFER_SIZE/PCM_BUFFER_SIZE)
void setup() {
Serial.begin(115200);
while(!Serial); // wait for serial connection being established
pinMode(RED_LED_PIN_1, OUTPUT);
pinMode(RED_LED_PIN_2, OUTPUT);
pinMode(RED_LED_PIN_3, OUTPUT);
pinMode(BLUE_LED_PIN_1, OUTPUT);
pinMode(BLUE_LED_PIN_2, OUTPUT);
AudioMemory(32); // TODO: tune the memory consumption
audioShield.enable();
audioShield.volume(0.5);
audioShield.muteHeadphone();
audioShield.unmuteLineout();
}
void loop() {
// Wait for incoming data.
while (!Serial.available());
// Obtain header.
byte header = Serial.read();
// Process header.
if (header == 0x00) { // Audio data
char pcmBuffer[PCM_BUFFER_SIZE];
for (uint8_t i = 0; i < NUM_PCM_BUFFERS; ++i) {
uint16_t count = 0;
uint16_t n;
while (count < PCM_BUFFER_SIZE) {
n = Serial.readBytes(pcmBuffer + count, PCM_BUFFER_SIZE - count);
if (n == 0) {
while (!Serial.available()); // Wait for incoming data.
}
count = count + n;
}
// Obtain a fresh audio buffer.
int16_t *audioBuffer = queue.getBuffer();
if (audioBuffer != NULL) {
// Copy the raw PCM audio data into the audio buffer.
memcpy(audioBuffer, pcmBuffer, PCM_BUFFER_SIZE);
// Transmit the buffer to the audio shield for playback.
queue.playBuffer();
}
}
} else if (header == 0x01 || header == 0x03) { // Light data
while (!Serial.available()); // Wait for incoming data.
byte brightness = Serial.read();
if (header == 0x01) {
analogWrite(RED_LED_PIN_1, brightness);
analogWrite(RED_LED_PIN_2, brightness);
analogWrite(RED_LED_PIN_3, brightness);
} else {
analogWrite(BLUE_LED_PIN_1, brightness);
analogWrite(BLUE_LED_PIN_2, brightness);
}
}
}
How to run
```
$ nodemon server.js
$ python -m http.server # or python -m SimpleHTTPServer
```
Now go to `localhost:8000`.
\ No newline at end of file
/*
EEG Service:
Connects to a Neurosky MindWave headset, acquires the EEG data,
and streams it via a Websocket to clients for further processing.
Protocols (partially) implemented:
http://developer.neurosky.com/docs/doku.php?id=thinkgear_communications_protocol
http://developer.neurosky.com/docs/lib/exe/fetch.php?media=app_notes:mindwave_rf_external.pdf
*/
// DEBUG switch. // DEBUG switch.
var DEBUG = false; var DEBUG = false;
......
...@@ -78,7 +78,7 @@ function sendLightData(socket, color, brightness) { ...@@ -78,7 +78,7 @@ function sendLightData(socket, color, brightness) {
if (color == "red") { if (color == "red") {
outputBuffer = new Uint8Array([0x01, brightness]); outputBuffer = new Uint8Array([0x01, brightness]);
} else if (color == "blue") { } else if (color == "blue") {
outputBuffer = new Uint8Array([0x02, brightness]); outputBuffer = new Uint8Array([0x03, brightness]);
} else { } else {
throw new Error('Color must be \'red\' or \'blue\''); throw new Error('Color must be \'red\' or \'blue\'');
} }
...@@ -88,10 +88,13 @@ function sendLightData(socket, color, brightness) { ...@@ -88,10 +88,13 @@ function sendLightData(socket, color, brightness) {
// Utilities for sending audio data over a web socket. // Utilities for sending audio data over a web socket.
//
// See for more information:
// http://stackoverflow.com/questions/27611607/web-audio-api-javascript-created-wav-file-incorrect-length-and-silent
function float32To16bitPCM(input) { function float32To16bitPCM(input) {
var output = new Int16Array(input.length); var output = new Int16Array(input.length);
for (var i = 0; i < input.length; ++i) { for (var i = 0; i < input.length; ++i) {
var sample = input[i]; //saturation: Math.max(-1, Math.min(1, buffer[l])); var sample = input[i];
output[i] = (sample < 0) ? (sample * 0x8000) : (sample * 0x7FFF); output[i] = (sample < 0) ? (sample * 0x8000) : (sample * 0x7FFF);
} }
return output.buffer; return output.buffer;
......
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
app.listen(80);
function handler (req, res) {
res.writeHead(200);
res.write('hi');
res.end();
}
var this_socket = null;
var data = require('./data/record.json');
var i = 0;
function sendData() {
this_socket.emit('brain', data[i] );
i++;
}
io.on('connection', function (socket) {
i = 0;
this_socket = socket;
setInterval(sendData, 500);
});
\ No newline at end of file
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