Alwyn Nixon-Lloyd
/
ribbonWithDDS
This uses a ribbon controller and a pressure sensor to control an onboard DDS generating tones
main.cpp
- Committer:
- zarquin
- Date:
- 2011-05-09
- Revision:
- 0:88f7ab41bdce
File content as of revision 0:88f7ab41bdce:
#include "mbed.h" DigitalOut myled(LED1); DigitalIn pushButton(p23); DigitalIn rotaryA(p21); DigitalIn rotaryB(p22); AnalogIn pressure(p20); AnalogIn pos1(p17); AnalogIn top(p16); AnalogIn bottom(p15); float wiper_f; float top_f; float bottom_f; float vol_f; float tmp_f; int tmp_i; float pressureAvg[4]; float wiperAvg[4]; AnalogOut signal(p18); Ticker sampleClock; // 16 entry sinewave table. char sineWave[]={127,175,216,244,255,244,216,175,127,78,37,9,0,9,36,78}; char sawWave[]={0,16,31,47,64,79,95,111,127,143,159,175,191,207,223,255}; unsigned int counter; unsigned int counterSum; unsigned int counterLimit = 2147483647; //31 bits 2^31 - 1 char temp; float byteToFloat(char in){ float i = in; return (i/255.0)*0.5; } void sample2(){ } void sample(){ //useing a 31 bit counter. counter = counter + counterSum; if(counter > counterLimit){ //wrap the counting. counter = counter - counterLimit; } //Fout = CounterSum*Fclk/MaxCount temp = counter>>27; //shift the top 4 bits down. //float x = temp; //signal.write(x/16.0); //write the output out signal.write(vol_f*byteToFloat(sineWave[temp]) +0.5); } int read_encoder() { int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0}; static char old_AB = 0; /**/ old_AB <<= 2; char x = rotaryA.read(); x<<=1; x = x+rotaryB.read(); //remember previous state old_AB |= ( x & 0x03 ); //add current state return ( enc_states[( old_AB & 0x0f )]); } int main() { //setup variables. tmp_i = 0; counter = 0; counterSum = 302365697; counterSum = 18897856; //counterSum = 1181116; //18897856.1 vol_f = 0.0; //hook up sample() to the ticker. run the counter at 50kHz sampleClock.attach_us(sample,20); while(1){ wiper_f = pos1.read(); bottom_f = bottom.read(); if(wiper_f >= bottom_f){ // wiper is being pressed, play a note top_f = top.read(); tmp_f = 1.0 - (wiper_f - bottom_f)/(top_f - bottom_f); vol_f = 1.0 - pressure.read(); //ribbon is about 3 octaves worth. //if the top is 440Hz = 18897856.1 //44Hz = 1889785.61 //55Hz is 2362232.013 //low A tmp_i = (int)(16535624*tmp_f); counterSum = 2362232+ tmp_i; }else{ vol_f = 0.0; } } }