Lizzy project

Dependencies:   aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT

source/main.cpp

Committer:
dbartolovic
Date:
2018-03-14
Revision:
7:ac8277568115
Parent:
6:55607d577a42
Child:
8:7ba4f82de9b6

File content as of revision 7:ac8277568115:

/* 
 * aconno.de
 * Made by Jurica Resetar
 * All right reserved 
 *
 */

#include "main.h"

static advertising_packet advertisementPacket;
static GapAdvertisingData adv_data = GapAdvertisingData();

void disableI2C(){
    NRF_TWI0->ENABLE = 0;
    NRF_TWI0->PSELSCL = 0xFFFFFFFF;
    NRF_TWI1->ENABLE = 0;
    NRF_TWI1->PSELSCL = 0xFFFFFFFF;
    NRF_TWIM0->ENABLE = 0;
    NRF_TWIM0->PSEL.SCL = 0x80000000;
    NRF_TWIM0->PSEL.SDA = 0x80000000;
    NRF_TWIM1->ENABLE = 0;
    NRF_TWIM0->PSEL.SCL = 0x80000000;
    NRF_TWIM0->PSEL.SDA = 0x80000000;
    DigitalOut foo(I2C_DATA);
    DigitalOut bar(I2C_CLK);
    foo = 1;
    bar = 1;
}

void bleF(BLE *ble)
{
    while(true)
    {

        advertisementPacket.header = APPLICATION_ID;
        advertisementPacket.type = 0x00;
        advertisementPacket.gyroscope[0] = (int16_t)0;
        advertisementPacket.gyroscope[1] = (int16_t)0;
        advertisementPacket.gyroscope[2] = (int16_t)0;
        advertisementPacket.magnetometer[0] = (int16_t)0;
        advertisementPacket.magnetometer[1] = (int16_t)0;
        advertisementPacket.magnetometer[2] = (int16_t)0;
        
        
        advertisementPacket.accelerometer[0] = (int16_t)mems.readXAxis();
        advertisementPacket.accelerometer[1] = (int16_t)mems.readYAxis();
        advertisementPacket.accelerometer[2] = (int16_t)mems.readZAxis();
        
        //advertisementPacket.acc_lsb_value = 0xC000;
        advertisementPacket.acc_lsb_value = 0x0100;
        
        
        printf("%6d\t", advertisementPacket.accelerometer[0]);
        printf("%6d\t", advertisementPacket.accelerometer[1]);
        printf("%6d\r\n", advertisementPacket.accelerometer[2]);

        //updatePayload(ble, &advertisementPacket);
        
        /* setup advertising */
        
        
        /*
        GapAdvertisingData advetisementData = GapAdvertisingData();
        advetisementData = ble->getAdvertisingData();
        advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertising_packet));
        ble->setAdvertisingData(advetisementData);
        */
        adv_data = ble->getAdvertisingData();
        adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
        ble->setAdvertisingData(adv_data);
        
        
        //printf("Ble advertisement is ON.\r\n");
        
        #if DEBUG_LED
            redLed = 0;
        #endif
        ble->gap().startAdvertising();
        //wait_ms(1000);
        
        wait_ms(MEASURE_INTERVAL_MS);
        //wait_ms(1000);
        
        //printf("Ble advertisement is OFF.\r\n\r\n");
        #if DEBUG_LED
            redLed = 1;
        #endif
        //ble->gap().stopAdvertising();
        /*
        GapAdvertisingData advetisementData = GapAdvertisingData();
        advetisementData = ble->getAdvertisingData();
        advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertising_packet));
        ble->setAdvertisingData(advetisementData);
        */
        //wait_ms(100);
    }
}

void led_tick();

void buzz_tick()
{
    static int start = 1;
    
    if (start)
    {
        buzzer.enable(BUZZER_FREQUENCY_HZ);
        buzzer.enableChannel(0, BUZZER_PIN);
        buzzer.setDuty(0,0.5f);
        start = 0;
    }
    else
    {
        buzzer.enable(0);
        buzzer.setDuty(0, 0);
        buzzer.disable();
        start = 1;
        led_tick();
        test_ticker.detach();
        test_ticker.attach(led_tick, 0.5);
    }
}


void led_tick()
{
    static int count = 0;
    
    switch(count)
    {
        case 0:
            redLed = 0;
            break;
            
        case 1:
            redLed = 1;
            greenLed = 0;
            break;
            
        case 2:
            greenLed = 1;
            blueLed = 0;
            break;
        
        default:
            blueLed = 1;
            count = -1;
            buzz_tick();
            test_ticker.detach();
            test_ticker.attach(buzz_tick, BUZZ_TIME_S);
    }
    
    count++;
}

int main(){
    
    test_ticker.attach(led_tick, 0.5);
    
    printf("Main program started.\r\n");
    
    NRF_NVMC->CONFIG = 0x00000002;      // Erase enable UICR
    NRF_NVMC->ERASEUICR = 0x00000001;   // Erase all
    NRF_NVMC->CONFIG = 0x00000001;      // Write enable UICR
    NRF_UICR->NFCPINS = 0xFFFFFFFE;     // Change NFC to GPIO function
    
    redLed = 0;
    AccVcc = 1;
    
    greenLed = 1;
    blueLed = 1;
    wait_ms(POWER_UP_DELAY_MS);

    /*
    INT1.rise(int1Handler);
    NRF_GPIO->PIN_CNF[INT_PIN1] &= 0xFFFFFFF3;  // NO PullUps
    */
    
    BLE &ble = BLE::Instance();
    ble.init(bleInitComplete);
    
    //mems.setMode(LOW_POWER);
    mems.setMode(HIGH_RES);
    mems.enableAxes(X_axis);
    mems.enableAxes(Y_axis);
    mems.enableAxes(Z_axis);    
    mems.setODR(ODR_50Hz);
    //mems.setScale(_16g);
    mems.setScale(_2g);
    mems.int1Setup(0x40);       // IntActivity 1 driven to INT1 pad
    mems.int1Latch(0x01);       // Enable int1 latch
    mems.int1Threshold(INT1_THRESHOLD); 
    mems.int1Duration(INT1_DUR);            
    mems.int1Config(0x2A);      // Enable XHigh, YHigh and ZHigh
    
    bleT.start(callback(bleF, &ble));   // Start bleT
    
    
    while(1){
        //disableI2C();
        Thread::wait(0xFFFFFFFF);
    }
}