void setupMain() {
    Serial.println("PoP board starting 8/14 10:19pm");
    print_mac();

    bool success = readFromEEPROM(sizeof(Platform), (char*) &platform);
    if (success) {
        Serial.print("Read from EEPROM ");
        Serial.print(platform.identifier);
        Serial.println();
    }
    char  platformData[10];
    const int platformSerialLength = 10;
    int bytesRead = Serial.readBytes((char *) platformData, platformSerialLength);
    Serial.print(bytesRead);
    Serial.println(" bytes read");
    Serial.print(sizeof(Platform));
    Serial.println(" platform size");
    if (bytesRead == platformSerialLength) {
        bool success = platform.initialize(platformData, platformSerialLength);
        if (success) {
            writeToEEPROM(sizeof(Platform), (char*) &platform);
            Serial.println("success, wrote to EEPROM");
        }
        else
            Serial.println("Fail parsing data from central");
        Serial.print(platform.y);
    }
    initializeAccelerometer(constants.PULSE_THSX,constants.PULSE_THSY,
                            constants.PULSE_THSZ);
    
    info = new RNInfo(constants.LEDs, platform);
    controller = new RNController(*info);

    info->printf("Ready, platform %d, wire %d\n", info->identifier, info->wirePosition);
    initializeComm(*info);
#ifndef FULL_STRIP
    debugTriadPositions();
#endif

    createWatchdog(constants.watchdogTimeout);


}
예제 #2
0
파일: main.c 프로젝트: azhn/smartbike
int main(void) {
    uint32_t err_code;
    uint8_t gpio_input_count;
    // // Initialization
    volatile uint32_t time_cnt = 0;
    uint32_t i;
    i=0;
    b8=false; b9=false; b10=false; b21=false; b22=false, leftSignal = false; rightSignal = false;
    checkForReturn = false, returning = false;
    led_init(LED_0);
    led_init(LED_1);
    led_init(LED_2);

    //initializeAccelerometer();
    initializeAccelerometer();

    // since active high, pins need to be set to have a pull-down resistor,
    //      otherwise they will be floating
    static gpio_input_cfg_t cfgs[] = {  {BUTTON_PIN, GPIO_ACTIVE_LOW, NRF_GPIO_PIN_NOPULL, &pin_handler},
                                        {BUTTON2_PIN, GPIO_ACTIVE_LOW, NRF_GPIO_PIN_NOPULL, &pin_handler},
                            
    //                                     {PIN1, GPIO_ACTIVE_HIGH, NRF_GPIO_PIN_NOPULL, &pin_handler},
    //                                     {PIN2, GPIO_ACTIVE_HIGH, NRF_GPIO_PIN_NOPULL, &pin_handler},
                                        {ACCEL_PIN, GPIO_ACTIVE_HIGH, NRF_GPIO_PIN_NOPULL, &pin_handler}};
    // // It seems only 4 pins can be registered per channel
    gpio_input_count = 3;

    /* SET OUTPUT WITH DRIVER */
    /* uint8_t output_pins[] = {PIN1,PIN2,PIN3};
     * gpio_output_init(output_pins, 3);
     */

    /* SET OUTPUT OLD WAY */
    /* nrf_gpio_cfg_output(OUTPUT_PIN);
     */

    
    /* SET INPUT WITH DRIVER */
    err_code = gpio_input_init(cfgs, gpio_input_count);
    if (err_code) {
        led_on(LED_1);
    }
    gpio_input_enable_all();

    /* SET INPUT OLD WAY */
    /* nrf_gpio_cfg_input(BUTTON_PIN, NRF_GPIO_PIN_NOPULL); //Configure pin 21 0 as input
     * nrf_gpiote_event_configure(GPIOTE_CHANNEL_0, BUTTON_PIN, NRF_GPIOTE_POLARITY_LOTOHI); 
     * nrf_drv_gpiote_in_event_enable(BUTTON_PIN, true);
     */ 

    //NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_IN0_Enabled; //Set GPIOTE interrupt register on channel 0
    NVIC_EnableIRQ(GPIOTE_IRQn); //Enable interrupts


//accelDataReady = true;
    // reset accelerometer data ready interrupt
    readAxisX();
    while (true) {

        /*******************************************************************
        Working Accel turn light
        ********************************************************************/
        /*
        if (b21 == true) { //button was toggled
            
            led_on(LED_0);
            // need to sample the accelerometer value
            sampled_accel_x = readAxisX();
            //accelDataReady = false;
            
            checkForReturn = true;
            
            b21 = false;

            for(time_cnt = 0; time_cnt < 1000000; time_cnt++){
                //do nothing, jsut to delay
            }
        }

        // if(checkForReturn){
        while(checkForReturn){

            if(abs(readAxisX() - sampled_accel_x) <= ACCEL_THRESH){
                led_off(LED_0);
                checkForReturn = false;
            }
            // accelDataReady = false;
        }
        // reset the data ready interrupt by reading an axis reg
        readAxisX();
        */
        /*******************************************************************
        ********************************************************************/

        if (b21 == true) { //button was toggled
            // need to sample the accelerometer value
            //  1) add new sample to sample value
            //sampled_accel_x = readAxisX();
            //sampled_accel_x = 0;
            
            // turn on LED due to button press (signal light)
            
            // check for return to ~0
            
            
            // turn off button called
            b21 = false;
            
            if (checkForReturn && !leftSignal) {
                led_off(LED_1);
            }
            leftSignal = true;
            checkForReturn = true;
            led_on(LED_0);
            // for(time_cnt = 0; time_cnt < 1000000; time_cnt++){
            //     //do nothing, jsut to delay
            // }
        }

        if (b22 == true) { //button was toggled
            // need to sample the accelerometer value
            //  1) add new sample to sample value
            //sampled_accel_x = readAxisX();
            //sampled_accel_x = 0;
            
            // turn on LED due to button press (signal light)
            
            // check for return to ~0
            
            // turn off button called
            b22 = false;
            if (checkForReturn && leftSignal) {
                led_off(LED_0);
            }
            leftSignal = false;
            checkForReturn = true;

            led_on(LED_1);

            // for(time_cnt = 0; time_cnt < 1000000; time_cnt++){
            //     //do nothing, jsut to delay
            // }
        }

         if(checkForReturn && accelDataReady){
            int16_t curr_x_val = readAxisX();
            // add current sample to total
            // if( leftSignal && (curr_x_val < 0)){
            //     curr_x_total += curr_x_val;    
            // } else if( !leftSignal && (curr_x_val > 0)){
            //     curr_x_total += curr_x_val;
            // }
            
        
            // beginning of turn - check if tilt is outisde zero-thresh range
            // if(!returning && (abs( curr_x_val - sampled_accel_x ) > ACCEL_OUT_THRESH) ){
            if(!returning && (abs( curr_x_val - sampled_accel_x ) > ACCEL_OUT_THRESH) ){
                if( 
                (leftSignal && (curr_x_val < sampled_accel_x) ) ||
                ( !leftSignal && (curr_x_val > sampled_accel_x) ) ){
                    led_on(LED_2);
                    thresh_out_count++;  
                } 

                thresh_out_count++;
                if(thresh_out_count > ACCEL_TILT_THRESH){
                    returning = true;
                    thresh_out_count = 0;
                    led_off(LED_2);

                }
            }


            //check for return into zero-thresh
            if(returning && (abs( curr_x_val - sampled_accel_x ) <= ACCEL_IN_THRESH) ){
                if( 
                (leftSignal && (curr_x_val < sampled_accel_x) ) ||
                ( !leftSignal && (curr_x_val > sampled_accel_x) ) ){ 
                    led_on(LED_1);
                    thresh_in_count++;
                } 

                if( thresh_in_count > ACCEL_TILT_THRESH){
                    returning = false;
                    thresh_in_count = 0;
                    checkForReturn = false;
                    led_off(LED_0);
                    led_off(LED_1);
                }
            }

            // reset accelDataReady
            accelDataReady = false;
            
        }
        // reset the data ready interrupt by reading an axis reg
        readAxisX();









            // while(check_accel_x() == false){
            //     led_toggle(LED_1);
            //     for(time_cnt = 0; time_cnt < 1000000; time_cnt++){
            //     //do nothing, jsut to delay
            //     }
            // }
            // if(check_accel_x()){
                // checkForReturn = false;

            // }
        // }
        // if (b22 == true) {
        //     b22 = false;
        //     led_toggle(LED_1);
        // }

        // if (b8 == true) {
        //     b8 = false;
        //     led_toggle(LED_2);
        // }
        // if (b9 == true) {
        //     b9 = false;
        //     for (i=0;i<1000;++i) {
        //         if (i%100 == 0) {
        //             led_toggle(LED_0); 
        //         }
        //     }
        //     led_off(LED_0); 
        // }
        // if (b10 == true) {
        //     b9 = false;
        //     for (i=0;i<1000;++i) {
        //         if (i%100 == 0) {
        //             led_toggle(LED_1); 
        //         }
        //     }
        //     led_off(LED_1); 
        // }

    }
}