#include <Bela.h>
#include <libraries/Scope/Scope.h>
#include <cmath>
// instantiate the scope
Scope scope;
int gAudioFramesPerAnalogFrame = 0;
int gCount = 0;
/* RESISTOR NETWORK */
// 4 digital pins setting the solid state relais to set the resistor network
// arduino pins 5,4,3,2 become 3,2,1,0 on BELA/BBB
// SSR P3,
int gR_P3_V_ch_1 = 0; // digital pin 0 - check the pin diagram in the IDE
int gR_P3_GND_ch_2 = 1;
// SSR P1, P2
int gR_P1_p12_ch1 = 2;
int gR_P1_p13_ch2 = 3;
float SSR_pinpong_Interval = 0.02; // seconds
float delay_stabilize = 0.001; // seconds
int gStatus = 0;
// analogReads Pins
int A_P1 = 0;
int A_P3 = 1;
// 2 analog Reads as A0 and A1 of the resistor network
float A12 = 0.f;
float A23 = 0.f;
float A13 = 0.f;
bool setup(BelaContext *context, void *userData)
{
// tell the scope how many channels and the sample rate
scope.setup(3, context->audioSampleRate);
// Check if analog channels are enabled
if(context->analogFrames == 0 || context->analogFrames > context->audioFrames) {
rt_printf("Error: this example needs analog enabled, with 4 or 8 channels\n");
return false;
}
// pin setup
pinMode(context, 0, gR_P3_V_ch_1, OUTPUT); // dig pin 0
pinMode(context, 0, gR_P3_GND_ch_2, OUTPUT); // dig pin 1
pinMode(context, 0, gR_P1_p12_ch1, OUTPUT); // dig pin 2
pinMode(context, 0, gR_P1_p13_ch2, OUTPUT); // dig pin 3
// Useful calculations
gAudioFramesPerAnalogFrame = context->audioFrames / context->analogFrames;
rt_printf("SOG Setup Done\n");
return true;
}
void render(BelaContext *context, void *userData)
{
// if ( SSR_pinpong_Interval < delay_stabilize )
// rt_printf("stop - stabilization time frame needs to me smaller then SSR ping pong interval");
for(unsigned int n = 0; n < context->audioFrames; n++)
{
// // resistor network
if(gCount >= (int)(context->digitalSampleRate * SSR_pinpong_Interval)) //if enough samples have elapsed
{
gCount = 0; //reset the counter
rt_printf("reset cntr\n ");
// //toggle the status
if(gStatus == 0)
{
gStatus = 1;
rt_printf("gStatus PING: %i\n", gStatus);
// ==> LOW/0 means CONNECTED !
// phase 1
digitalWrite(context, n, gR_P1_p12_ch1 , 0); // A12
digitalWrite(context, n, gR_P1_p13_ch2 , 1); // not A13
digitalWrite(context, n, gR_P3_V_ch_1 , 0); //
digitalWrite(context, n, gR_P3_GND_ch_2, 1);
/*
stabilize
measure
scope / control sound
delay for stabilization
*/
}
else if (gStatus == 1)
{
gStatus = 0;
rt_printf("gStatus PONG: %i\n", gStatus);
// phase 2
// set circuit paths
digitalWrite(context, n, gR_P1_p12_ch1 , 1); // not A12
digitalWrite(context, n, gR_P1_p13_ch2 , 0); // A13
digitalWrite(context, n, gR_P3_V_ch_1 , 1); //
digitalWrite(context, n, gR_P3_GND_ch_2, 0); // to ground..
/*
stabilize
measure
scope / control sound
delay for stabilization
*/
}
}
gCount++;
// rt_printf(" gCount: %i\n", gCount);
// READ SENSORS IN ANALOG FRAMES NOT AUDIO FRAMES!
if(gAudioFramesPerAnalogFrame && !(n % gAudioFramesPerAnalogFrame))
{
// Depending on the sampling rate of the analog inputs, this will
// happen every audio frame (if it is 44100)
// or every two audio frames (if it is 22050)
// read resistance values
if (gStatus == 0) // phase 1 , add stabilize delay here as condition
{
// A12 = analogRead(context, n/gAudioFramesPerAnalogFrame, A_P1 )* (3.3 / 1023.0);
A12 = analogRead(context, n/gAudioFramesPerAnalogFrame, A_P1 ) * 1.0f; // 4
// A23 = analogRead(context, n/gAudioFramesPerAnalogFrame, A_P3 ) * (3.3 / 1023.0);
A23 = analogRead(context, n/gAudioFramesPerAnalogFrame, A_P3 ); // 5
}
else if (gStatus == 1)
{
// A13 = analogRead(context, n/gAudioFramesPerAnalogFrame, A_P3 ) * (3.3 / 1023.0);
A13 = (analogRead(context, n/gAudioFramesPerAnalogFrame, A_P3 ) * 1.0f); // 5 // -A12 ?
}
scope.log(A12, A23, A13);
// test the scope!
// chan3 not working?
// A12 = analogRead(context, n/gAudioFramesPerAnalogFrame, 0 ); // 3
// A13 = analogRead(context, n/gAudioFramesPerAnalogFrame, 3 ); // 4
// scope.log(A12,A13); // this scopes on channel 3 and 4, oscilloscope colours 3 & 4(green orange)
}
}
}
void cleanup(BelaContext *context, void *userData)
{
}