From b89ecb366de641ac38d803426554839d10435813 Mon Sep 17 00:00:00 2001
From: William Fish <wfish001@gold.ac.uk>
Date: Fri, 9 Mar 2018 02:26:21 +0000
Subject: [PATCH] adding prototype build

---
 .../strandtest/serialLED/serialLED.ino        |  89 ++++++----
 LEDprogress/src/ofApp.cpp                     |  75 +++-----
 LEDprogress/src/ofApp.h                       |   1 -
 Prototype/src/Visualizer.cpp                  | 167 ++++++++++++++++++
 Prototype/src/Visualizer.h                    |  43 +++++
 Prototype/src/main.cpp                        |  13 ++
 Prototype/src/particle.cpp                    |  73 ++++++++
 Prototype/src/particle.h                      |  36 ++++
 8 files changed, 404 insertions(+), 93 deletions(-)
 create mode 100644 Prototype/src/Visualizer.cpp
 create mode 100644 Prototype/src/Visualizer.h
 create mode 100644 Prototype/src/main.cpp
 create mode 100644 Prototype/src/particle.cpp
 create mode 100644 Prototype/src/particle.h

diff --git a/LEDprogress/Arduino_LED/strandtest/serialLED/serialLED.ino b/LEDprogress/Arduino_LED/strandtest/serialLED/serialLED.ino
index 69e6d16..ade65d1 100644
--- a/LEDprogress/Arduino_LED/strandtest/serialLED/serialLED.ino
+++ b/LEDprogress/Arduino_LED/strandtest/serialLED/serialLED.ino
@@ -5,22 +5,12 @@
 
 #define PIN 6
 #define stripLength 30
-// Parameter 1 = number of pixels in strip
-// Parameter 2 = Arduino pin number (most are valid)
-// Parameter 3 = pixel type flags, add together as needed:
-//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
-//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
-//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
-//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
-//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
+
 Adafruit_NeoPixel strip = Adafruit_NeoPixel(stripLength, PIN, NEO_GRB + NEO_KHZ800);
 
-// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
-// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
-// and minimize distance between Arduino and first pixel.  Avoid connecting
-// on a live circuit...if you must, connect GND first.
-  int incomingByte = 0;
 
+  int incomingByte = 0;
+ char bytes [4];
 void setup() {
   // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
   #if defined (__AVR_ATtiny85__)
@@ -35,36 +25,57 @@ Serial.begin(57600);
 }
 
 void loop() {
-  // Some example procedures showing how to display to the pixels:
-  int r = 0;
-  int r2 = 0;
-  int ledIndex = 0;
-  char bytes [4]; //buffer of chars, store incoming bytes in 
-
- // mouseControl(ledIndex, strip.Color(r,60,60));
-//for(int x = 0; x <= 30; x++){  
-  //strip.setPixelColor(x, strip.Color(5,5,5));
-  //strip.show();
-//}
-      if (Serial.available() > 0) {
-                // read the incoming byte:
-               // Serial.readBytes(bytes,4);
-            
-
-              // mouseControl(0, strip.Color(bytes[0],60,60));
-              // mouseControl(bytes[1],strip.Color(180,60,60));
-            Serial.readBytes(bytes,4); //store data incoming bytes into [bytes] buffer, returns amount of bytes given
-              //incomingByte = Serial.read();
-              //for(int i = 0; i <= 30; i++){ 
-            
+
+  //buffer of chars, store incoming bytes in 
+
+     
+              
+      
+          
+               incomingByte = Serial.read();
+              for(int i = 0; i <= 30; i++){ 
+                if(incomingByte==1){
                 
-                 strip.setPixelColor(bytes[3], strip.Color(bytes[0],bytes[1],bytes[2])); //index control of LEDS(VERY BUGGY)
-       
+                 strip.setPixelColor(i, strip.Color(200,0,0));
+                  strip.show();
+                }else {
+                  strip.setPixelColor(i, strip.Color(5,5,5));
                   strip.show();
+                  
+                       }
+                }
+
+              
+              //
+
+              //Serial.readBytes(bytes,4);
+              //strip.setPixelColor(bytes[3], strip.Color(bytes[0],bytes[1],bytes[2])); //index control of LEDS(VERY BUGGY)
+             //strip.show();
+
+          //Serial.readBytes(bytes,4);
+         //rgbControl(bytes[3],bytes[0],bytes[1],bytes[2]); 
+        
                 
-     //}
-   }
+
+   
+}
+
+void rgbControl(uint32_t bytes_index,uint32_t bytes_r,uint32_t bytes_g,uint32_t bytes_b){
+  //RGB INDEX CONTROL
+
+          //Serial.readBytes(bytes,4); //store data incoming bytes into [bytes] buffer, returns amount of bytes given
+               strip.setPixelColor(bytes_index, strip.Color(bytes_r,bytes_g,bytes_b));              
+                    if(bytes_r==0 && bytes_g==0 && bytes_b==0){ //clears all the LEDS as they're very bright and annoying
+                          for(int i = 0; i <= 9; i++){ 
+                               strip.setPixelColor(i,0,0,0);
+                               strip.show();
+                                                     }
+                    }
+                 strip.show();
+   
 }
+
+
 void mouseControl(uint32_t i,uint32_t c){
   
    strip.setPixelColor(i, c);
diff --git a/LEDprogress/src/ofApp.cpp b/LEDprogress/src/ofApp.cpp
index 542eaab..5b25f3e 100644
--- a/LEDprogress/src/ofApp.cpp
+++ b/LEDprogress/src/ofApp.cpp
@@ -2,28 +2,18 @@
 
 //--------------------------------------------------------------
 void ofApp::setup(){
-	ofSetVerticalSync(true);
-	
-	bSendSerialMessage = false;
-	ofBackground(255);	
-	//ofSetLogLevel(OF_LOG_VERBOSE);
-	
-	font.load("DIN.otf", 64);
-	
-	serial.listDevices();
-	vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();
-	int baud = 57600;
+
+	ofBackground(0);
+
+	//serial.listDevices();   //debugging
+	//vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();
 
 	serial.setup("COM5", 57600); // windows example
 
 
-	
-	nTimesRead = 0;
-	nBytesRead = 0;
-	readTime = 0;
-	memset(bytesReadString, 0, 4);
+	//memset(bytesReadString, 0, 4); 
 
-	serial.writeBytes(&array[0], 4);
+	//serial.writeBytes(&array[0], 4); //dont think i need this
 
 	
 }
@@ -37,12 +27,13 @@ void ofApp::update(){
 		
 
 		
-		
+		//MOUSE CONTROL, NEEDS FIXING
 		//unsigned char array[2] = { ofMap(mouseX, 0, ofGetWidth(), 0, 200) , ofMap(mouseY, 0, ofGetHeight(), 0, 200) };
 		//unsigned char array[3] = { ofMap(mouseX, 0, ofGetWidth(), 0, 200) , ofMap(mouseY, 0, ofGetHeight(), 0, 200), greenIncrease };
 		//array[2] = ofMap(mouseX, 0, ofGetWidth(), 0, 200);
 		//array[0] = 5;
 		//serial.writeByte('a');
+		//END OF MOUSE CONTROL
 
 		//bSendSerialMessage = false;
 		//serial.writeBytes(&array[0], 3);
@@ -61,7 +52,7 @@ void ofApp::draw(){
 
 //--------------------------------------------------------------
 void ofApp::keyPressed(int key) {
-
+	//KEY PRESS FOR FAST ACTIVATION, USE WITH 'readbyte' in IDE
 	//if (key == '1') {
 	//	//bSendSerialMessage = true;
 	//	serial.writeByte(1);
@@ -70,6 +61,9 @@ void ofApp::keyPressed(int key) {
 	//else {
 	//	serial.writeByte('b');
 	//}
+	//END
+
+	//FOR RGB INDEX CONTROL
 	if (key == 'r') {
 		array[0] = 200;
 		array[1] = 0;
@@ -88,43 +82,18 @@ void ofApp::keyPressed(int key) {
 		array[2] = 200;
 		serial.writeBytes(&array[0], 4);
 	}
-	//else {
-	//	array[0] = 255;
-	//	array[1] = 0;
-	//	array[2] = 0;
-	//	serial.writeBytes(&array[0], 4);
-
-	//}
-
-
-
-else {
-	array[3] = key - 49;
-	serial.writeBytes(&array[0], 4);
+	else if (key == 'x'){
+		array[0] = 0;
+		array[1] = 0;
+		array[2] = 0;
+		serial.writeBytes(&array[0], 4);
+	} 
+	else {
+		array[3] = key - 49;
+		serial.writeBytes(&array[0], 4);
 }
 
 
-
-
-
-	//if (key == 'l') {
-	//	bSendSerialMessage = true;
-	//	/*unsigned char array[3] = { 10 };
-
-	//std:cout << "turn leds off" << std::endl;*/
-	//	unsigned char myByte = '225';
-	//	bool byteWasWritten = serial.writeByte(myByte);
-	//	if (!byteWasWritten) {
-	//	std::cout << "byte was not written to serial port" << std::endl;
-	//}
-	//else {
-	//		std::cout << "byte written to serial port" << std::endl;
-	//	}
-	//}
-	//else {
-	//	unsigned char myByte2 = '200';
-	//	
-	//}
 }
 
 //--------------------------------------------------------------
diff --git a/LEDprogress/src/ofApp.h b/LEDprogress/src/ofApp.h
index ddfa576..f3f5070 100644
--- a/LEDprogress/src/ofApp.h
+++ b/LEDprogress/src/ofApp.h
@@ -21,7 +21,6 @@ class ofApp : public ofBaseApp{
 		void dragEvent(ofDragInfo dragInfo);
 		void gotMessage(ofMessage msg);
 
-		ofTrueTypeFont		font;
 
 		bool		bSendSerialMessage;			// a flag for sending serial
 		char		bytesRead[3];				// data from serial, we will be trying to read 3
diff --git a/Prototype/src/Visualizer.cpp b/Prototype/src/Visualizer.cpp
new file mode 100644
index 0000000..e976e12
--- /dev/null
+++ b/Prototype/src/Visualizer.cpp
@@ -0,0 +1,167 @@
+//
+//  Visualizer.cpp
+//  Visualizer 2
+//
+//  Created by james carty on 08/03/2018.
+//
+
+#include "Visualizer.h"
+
+
+void Visualizer::setup() {
+    
+    // Setup the audio component
+    song.loadSound("C:/users/Billy/Desktop/fftLED/bin/data/beat.wav");
+    song.setVolume(1.0f);
+    fftSmoothed = new float[8192];
+    for (int i=0; i < 8192; i++) {
+        fftSmoothed[i] = 0;
+    }
+    nBandsToGet = 256;
+    
+    ofEnableSmoothing();
+    ofSetFrameRate(60); //speed up frame rate
+
+
+	//serial + serial debugging
+	//serial.listDevices(); 
+	//vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();
+	serial.setup("COM5",57600);
+
+
+    // Allocate drawing buffer 
+    int w = ofGetWidth();
+    int h = ofGetHeight();
+    fbo.allocate(w, h, GL_RGB32F_ARB);
+    
+    // Fill buffer with white color
+    fbo.begin();
+    ofBackground(255,255,255);
+    fbo.end();
+    
+    // Create particles
+    
+    Particle mid1;
+    Param mid1Param;
+    mid1Param.sensitivity = 1.4; 
+    mid1Param.emitterRad = 60;
+    mid1Param.direction = 1;
+    mid1Param.sections = 16;
+    mid1Param.nBands = nBandsToGet;
+    mid1Param.color = ofColor(200, 200, 100); //reduced colour gradient
+    mid1Param.numParticles = 2048;
+    mid1Param.rotationSpeed = -40;
+    mid1.setup(mid1Param);
+    
+    particles.push_back(mid1);
+    
+    // Start the song
+	song.setLoop(true);
+    song.play();
+}
+
+//-------------------------------------------
+void Visualizer::update() {
+    
+    ofSoundUpdate();
+    
+    // grab the fft
+    curAvgVolume = 0;
+    float *val = ofSoundGetSpectrum(nBandsToGet);
+
+	//send a byte over a volume threshold to create a 'pulse' in the LEDS, second byte turns them back off
+	if (*val>=1.7) {
+		serial.writeByte(1);
+		serial.writeByte(2);
+	}
+
+
+    for (int i=0; i<nBandsToGet; i++) {
+        fftSmoothed[i] *= 0.94f;
+        
+        if (fftSmoothed[i] < val[i]) {
+            fftSmoothed[i] = val[i];
+        }
+        curAvgVolume += fftSmoothed[i];
+    }
+    curAvgVolume /= nBandsToGet;
+
+
+
+	// Update the particles
+    for (int i=0; i<particles.size(); i++) {
+        particles[i].update(fftSmoothed, i * (nBandsToGet / particles.size()));
+    }
+}
+
+//--------------------------------------------------------------
+void Visualizer::draw() {
+    
+    float volume = ofMap(curAvgVolume, 0, 1, 40, 210);
+
+    ofColor inColor = ofColor(volume, volume+10, volume);
+    ofColor outColor = ofColor(volume, volume-40, volume);
+    
+    fbo.begin();
+    ofRect(0, 0, ofGetWidth(), ofGetHeight());
+    ofBackgroundGradient(inColor, outColor, OF_GRADIENT_CIRCULAR);
+    
+    for (int i=0; i<particles.size(); i++) {
+        ofPushMatrix();
+        ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
+        ofRotate(ofGetElapsedTimef() * particles[i].param.rotationSpeed);
+        ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2);
+        particles[i].draw();
+        ofPopMatrix();
+    }
+    
+    fbo.end();
+    
+    ofSetColor(200,255,155);
+    fbo.draw(0, 0);
+}
+
+//--------------------------------------------------------------
+void Visualizer::keyPressed(int key) {
+	
+}
+
+//--------------------------------------------------------------
+void Visualizer::keyReleased(int key) {
+    
+}
+
+//--------------------------------------------------------------
+void Visualizer::mouseMoved(int x, int y ) {
+    
+}
+
+//--------------------------------------------------------------
+void Visualizer::mouseDragged(int x, int y, int button) {
+    
+}
+
+//--------------------------------------------------------------
+void Visualizer::mousePressed(int x, int y, int button) {
+    
+}
+
+//--------------------------------------------------------------
+void Visualizer::mouseReleased(int x, int y, int button) {
+    
+}
+
+//--------------------------------------------------------------
+void Visualizer::windowResized(int w, int h) {
+    
+}
+
+//--------------------------------------------------------------
+void Visualizer::gotMessage(ofMessage msg) {
+    
+}
+
+//--------------------------------------------------------------
+void Visualizer::dragEvent(ofDragInfo dragInfo) {
+    
+}
diff --git a/Prototype/src/Visualizer.h b/Prototype/src/Visualizer.h
new file mode 100644
index 0000000..90b8b62
--- /dev/null
+++ b/Prototype/src/Visualizer.h
@@ -0,0 +1,43 @@
+//
+//  Visualizer.h
+//  Visualizer 2
+//
+//  Created by james carty
+//
+
+
+#pragma once
+
+#include "ofMain.h"
+#include "Particle.h"
+
+class Visualizer : public ofBaseApp{
+public:
+    void setup();
+    void update();
+    void draw();
+    
+    void keyPressed  (int key);
+    void keyReleased(int key);
+    void mouseMoved(int x, int y );
+    void mouseDragged(int x, int y, int button);
+    void mousePressed(int x, int y, int button);
+    void mouseReleased(int x, int y, int button);
+    void windowResized(int w, int h);
+    void dragEvent(ofDragInfo dragInfo);
+    void gotMessage(ofMessage msg);
+    
+    vector<Particle> particles;
+    ofFbo fbo;
+    
+    // Audio
+    ofSoundPlayer song;
+    float *fftSmoothed;
+    int nBandsToGet;
+    float curAvgVolume;
+
+	//LEDS
+	ofSerial	serial;  //create serial 
+
+	unsigned char array[4] = { 0,0,0,0 }; //create an array to be used as a buffer, stores value for index,r,g,b
+};
diff --git a/Prototype/src/main.cpp b/Prototype/src/main.cpp
new file mode 100644
index 0000000..e071cf9
--- /dev/null
+++ b/Prototype/src/main.cpp
@@ -0,0 +1,13 @@
+#include "ofMain.h"
+#include "Visualizer.h"
+
+//========================================================================
+int main( ){
+	ofSetupOpenGL(1024,768,OF_WINDOW);			// <-------- setup the GL context
+
+	// this kicks off the running of my app
+	// can be OF_WINDOW or OF_FULLSCREEN
+	// pass in width and height too:
+	ofRunApp(new Visualizer());
+
+}
diff --git a/Prototype/src/particle.cpp b/Prototype/src/particle.cpp
new file mode 100644
index 0000000..80411f4
--- /dev/null
+++ b/Prototype/src/particle.cpp
@@ -0,0 +1,73 @@
+//
+//  particle.cpp
+//  Visualizer 2
+//
+//  Created by james carty on
+//
+
+#include "particle.h"
+
+ofPoint getVelocity(float length, float angle) {
+    ofPoint p;
+    p.x = length * cos(angle);
+    p.y = length * sin(angle);
+    return p;
+}
+
+Particle::Particle() {
+    emitterCenter = ofPoint (ofGetWidth() / 2, ofGetHeight() / 2);
+}
+
+void Particle::setup(Param p) {
+    param = p;
+    frequencies = new float[param.numParticles];
+    
+    for (int i=0; i<param.numParticles; i++) {
+        frequencies[i] = 0;
+    }
+    
+    for (int i=0; i<param.numParticles; i++) {
+        ofPoint pnt, p;
+        float angle = i * (M_TWO_PI / param.numParticles);
+        pnt.x = cos(angle) * param.emitterRad;
+        pnt.y = sin(angle) * param.emitterRad;
+        p.x = 0;
+        p.y = 0;
+        
+        pos.push_back(emitterCenter + pnt);
+        vel.push_back(p);
+    }
+}
+
+void Particle::update(float *freq, int start) {
+    float curFreqRange = 0;
+    int curParticle = 0;
+    int size = 256/8;
+    int skips = param.numParticles / size;
+    
+    for (int i=0; i < param.numParticles; i++) {
+        for (int j=0; j<size && i<param.numParticles; j++) {
+            float angle = i * (M_TWO_PI / param.numParticles);
+            vel[i] = getVelocity(param.direction * param.sensitivity * freq[j+start], angle);
+            frequencies[i] = freq[j+start];
+            i++;
+        }
+        i--;
+    }
+}
+
+void Particle::draw() {
+    for (int i=0; i<param.numParticles; i++) {
+        ofColor color = param.color;
+        float size = ofMap(frequencies[i], 0, 1, 0.5, 12);
+        float hue = ofMap(frequencies[i], 0, 1, 100, 300);
+        
+        if (param.sensitivity < 1) {
+            size *= param.sensitivity;
+        }
+        
+        color.setHue(hue);
+        ofSetColor(color);
+        ofCircle(pos[i] + (pos[i] * vel[i]), size);
+    }
+}
diff --git a/Prototype/src/particle.h b/Prototype/src/particle.h
new file mode 100644
index 0000000..85f6575
--- /dev/null
+++ b/Prototype/src/particle.h
@@ -0,0 +1,36 @@
+//
+//  particle.h
+//  Visualizer 2
+//
+//  Created by james carty
+//
+
+#pragma once
+
+#include "ofMain.h"
+
+struct Param {
+    float emitterRad;
+    float sensitivity;
+    int direction;
+    int sections;
+    int nBands;
+    int numParticles;
+    int rotationSpeed;
+    ofColor color;
+};
+
+class Particle {
+public:
+    Particle();
+    void setup(Param param);
+    void update(float* freq, int start);
+    void draw();
+    
+    vector<ofPoint> pos;
+    vector<ofPoint> vel;
+    int numParticles;
+    float *frequencies;
+    ofPoint emitterCenter;
+    Param param;
+};
-- 
GitLab