Projet S5 Info - Advanced

Dependencies:   TextLCD XBeeLib mbed-rtos mbed

Fork of Coordinateur by Vincent Belanger

main.cpp

Committer:
vinbel93
Date:
2016-04-06
Revision:
6:59b0b6c1b5bb
Parent:
5:08245a3e93a8
Child:
7:bd9bc9fa66c7

File content as of revision 6:59b0b6c1b5bb:

#include "mbed.h"
#include "rtos.h"
#include "XBeeLib.h"
#include "SDFileSystem.h"
#include "wave_player.h"
 
using namespace XBeeLib;

SDFileSystem sd(p5, p6, p7, p8, "sd"); // MOSI, MISO, SCLK, SSEL
PwmOut speaker(p21);
AnalogIn GP2D12(p16);
//wave_player player(&DACout);
Serial pc(USBTX, USBRX);
float octaveMult=0.5;
osThreadId threadIDC5;
osThreadId threadIDG5;

bool capt_1;
bool capt_2;
bool capt_3;
bool capt_4;
bool capt_5;
bool capt_6;
    
// 261.63 hz do
// 293.66 Ré
// 329.63 mi
// 369.99 fa#
// 415.30 sol#
// 466.16 la#
    
const float DO =261.63;
const float RE =293.66;
const float MI =329.63;
const float FA_SHARP =369.99;
const float SOL_SHARP =415.30;
const float LA_SHARP =466.16;

const float DEMI_TON = 0.05946;

uint8_t GP2D12_value = 0;

float dutyCycle = 0.0002;
int lastCapt=-1;

static float findPeriod(float frequency, float vibrato){
    float freq = frequency * octaveMult;
    if (vibrato != 0){
        if(vibrato < 0.5){
            freq = freq + (freq *  ( (0.5 - vibrato) * 2 * DEMI_TON));
        } else {
            freq = freq - (freq *  ( (vibrato - 0.5) * 2 * DEMI_TON));
        }
    }
    
    pc.printf("freq = %f, vib = %f, freqF = %f\n\r ", frequency, vibrato, freq);
    
    return 1.0/freq;
}
 
/** Callback function, invoked at packet reception */
static void receive_cb(const RemoteXBeeZB& remote, bool broadcast, const uint8_t *const data, uint16_t len)
{
    const uint64_t remote_addr64 = remote.get_addr64();
 
    for (int i = 0; i < len; i++)
    {
        pc.printf("%02x ", data[i]);
    }
        
    capt_1 = (data[0] & 0b10000000);
    capt_2 = (data[0] & 0b01000000);
    capt_3 = (data[0] & 0b00100000);
    capt_4 = (data[0] & 0b00010000);
    capt_5 = (data[0] & 0b00001000);
    capt_6 = (data[0] & 0b00000100);
    
    GP2D12_value = data[1];
    
    pc.printf("GP2D12(v1) = %x\r\n",GP2D12_value);
    
    if(GP2D12_value < 0x50){
        GP2D12_value = 0;
    }else {
        GP2D12_value -= 0x50;
    }
    
    pc.printf("GP2D12(v2) = %x\r\n",GP2D12_value);
    float pourcentageGP2D12 = float(GP2D12_value) / float(0x70);
    
    if(pourcentageGP2D12 > 1){
        pourcentageGP2D12 = 1;
    }
    /*
    pc.printf("\r\n%i ", capt_1);
    pc.printf("%i ", capt_2);
    pc.printf("%i ", capt_3);
    pc.printf("%i ", capt_4);
    pc.printf("%i ", capt_5);
    pc.printf("%i ", capt_6);

    pc.printf("\r\n");*/
    
    pc.printf("GP2D12(v3) = %x\r\n",GP2D12_value);
    if(capt_1){
        speaker.period(findPeriod(DO, pourcentageGP2D12));
        
        speaker=dutyCycle;
        lastCapt = 1;
    } else if(capt_2) {
        speaker.period(findPeriod(RE, pourcentageGP2D12));
        
        speaker=dutyCycle;
        lastCapt = 2;
    } else if(capt_3) {
        speaker.period(findPeriod(MI, pourcentageGP2D12));
        
        speaker=dutyCycle;
        lastCapt = 3;
    pc.printf("mi \r\n");
    } else if(capt_4) {
        speaker.period(findPeriod(FA_SHARP, pourcentageGP2D12));
        
        speaker=dutyCycle;
        lastCapt = 4;
    } else if(capt_5) {
        speaker.period(findPeriod(SOL_SHARP, pourcentageGP2D12));
        
        speaker=dutyCycle;
        lastCapt = 5;
    } else if(capt_6) {
        speaker.period(findPeriod(LA_SHARP, pourcentageGP2D12));
        
        speaker=dutyCycle;
        lastCapt = 6;
    } else {
        speaker=0.0f;
    }
}

void playFile(const char* name)
{
    /*
    FILE *fp = fopen(name, "r");
    if(fp == NULL) {
        pc.printf("Could not open file for read\r\n");
    }
    else {
        player.play(fp);
    }
    fclose(fp);
    */
}
/*
void playC5(void const* args)
{
    threadIDC5 = osThreadGetId();
    while(1){
        osSignalWait(0x1, osWaitForever);
        pc.printf("Wait done C5");
        //playFile("/sd/Synth/C5.wav");        
        speaker2=0.50f;  // 50% duty cycle 
        //speaker2=1.0;
        wait(2);
        speaker2=0.0; // turn off audio
        wait(2);
    }
}

void playG5(void const* args)
{
    threadIDG5 = osThreadGetId();

    while(1){
        osSignalWait(0x2, osWaitForever);
        pc.printf("Wait done G5");
        //wait(1);
        //playFile("/sd/Synth/A5.wav");
        speaker1=0.50f;  // 50% duty cycle 
        //speaker1=1.0;
        wait(3);
        speaker1=0.0; // turn off audio
        wait(1);
    }
}
*/
int main()
{    
    XBeeZB xbee = XBeeZB(p13, p14, p8, NC, NC, 9600);

    /* Register callbacks */
    xbee.register_receive_cb(&receive_cb);

    RadioStatus const radioStatus = xbee.init();

    MBED_ASSERT(radioStatus == Success);
 
    /* Wait until the device has joined the network */
    pc.printf("Waiting for device to join the network: ");
    while (!xbee.is_joined()) {
        wait_ms(1000);
    }
    pc.printf("OK\r\n");
 
    while (true) {
        xbee.process_rx_frames();
        wait_ms(50);
    }
}