Andriy Makukha
/
football_project_wo_output
football_project_wo_output
Fork of football_project by
Diff: main.cpp
- Revision:
- 59:297133497153
- Parent:
- 54:2878d62714d6
- Parent:
- 58:fb2198ceb412
- Child:
- 60:d1fad57e8bfb
--- a/main.cpp Sat Jan 16 03:45:13 2016 +0000 +++ b/main.cpp Sat Jan 16 17:31:27 2016 +0000 @@ -80,6 +80,7 @@ void loop(); void setup(); void getRadioInput(char *ibuffer, int size); +void init_datastore(); #define BLENANO 0 // BLE Nano vs. TA New Baseboard (rev Aug-Nov 2015.) @@ -192,6 +193,7 @@ // mts::MTSSerialFlowControl pcfc( (trSwp ? SERRX : SERTX), (trSwp ? SERTX : SERRX), RTS_PIN_NUMBER, CTS_PIN_NUMBER, 384, 2688 ); //mts::MTSSerial pcfc( (trSwp ? SERRX : SERTX), (trSwp ? SERTX : SERRX), 128, 64, RTS_PIN_NUMBER, NC ); // 256, 1280 // 256, 2560 +Serial pc(SERTX,SERRX); uint8_t txPayload[TXRX_BUF_LEN] = { 0 }; @@ -448,129 +450,6 @@ } } -static uint32_t boot_cnt_data = 0; -static uint16_t data_block = 9*20 +75; // Last block -- Use for app-start count. -static uint16_t data_size = 4; -static uint16_t data_offset = 0; // 12 for 16-byte blocks. -static bool data_loaded = false; -static bool data_cleared = false; -static bool data_stored = false; -static pstorage_handle_t pstorage_id; - -static uint32_t pstorage_read_test() -{ - uint32_t err_code; - - pstorage_handle_t p_block_id; - - do - { - err_code = pstorage_block_identifier_get( &pstorage_id, data_block, &p_block_id ); - if( NRF_SUCCESS != err_code ) break; - - err_code = pstorage_load( (uint8_t *)&boot_cnt_data, &p_block_id, data_size, data_offset ); - if( NRF_SUCCESS != err_code ) break; - - } while( 0 ); - - return err_code; -} -static uint32_t pstorage_clear_test() -{ - uint32_t err_code; - - pstorage_handle_t p_block_id; - - do - { - err_code = pstorage_block_identifier_get( &pstorage_id, data_block, &p_block_id ); - if( NRF_SUCCESS != err_code ) break; - - err_code = pstorage_clear( &p_block_id, PSTORAGE_MIN_BLOCK_SIZE ); - if( NRF_SUCCESS != err_code ) break; - - } while( 0 ); - - return err_code; -} -static uint32_t pstorage_write_test() -{ - uint32_t err_code; - - pstorage_handle_t p_block_id; - - do - { - err_code = pstorage_block_identifier_get( &pstorage_id, data_block, &p_block_id ); - if( NRF_SUCCESS != err_code ) break; - - err_code = pstorage_store( &p_block_id, (uint8_t *)&boot_cnt_data, data_size, data_offset ); - - } while( 0 ); - - return err_code; -} - -static void pstorage_cb_handler( pstorage_handle_t *handle, uint8_t op_code, uint32_t result, uint8_t *p_data, uint32_t data_len ) -{ - switch( op_code ) - { - case PSTORAGE_LOAD_OP_CODE: - if( NRF_SUCCESS == result ) - { - // Load operation successful. - data_loaded = true; // Flag to signal load is done. - - } else - { - // Load operation failed. - //pcfc.printf( "\r\nWarn: pstorage load operation error: %x\r\n", result ); - } - break; - case PSTORAGE_UPDATE_OP_CODE: - case PSTORAGE_STORE_OP_CODE: - if( NRF_SUCCESS == result ) - { - // Store operation successful. - data_stored = true; // Flag to signal store is done. - - } else - { - // Store operation failed. - //pcfc.printf( "\r\nWarn: pstorage store operation error: %x\r\n", result ); - } - // Source memory can now be reused or freed. - break; - case PSTORAGE_CLEAR_OP_CODE: - if( NRF_SUCCESS == result ) - { - // Clear operation successful. - data_cleared = true; // Flag to store to the same data area. - - } else - { - // Clear operation failed. - //pcfc.printf( "\r\nWarn: pstorage clear operation error: %x\r\n", result ); - } - break; - //default: - //pcfc.printf( "\r\nWarn: pstorage unknown op: %x error: %x\r\n", op_code, result ); - } -} - -static uint32_t pstorage_setup() -{ - pstorage_module_param_t pstorage_param; - - // Setup pstorage with 9*20 +76 blocks of 4 bytes each. (or 9*20/4 + 19 for 16 byte blocks) - pstorage_param.block_size = 4; // Recommended to be >= 4 bytes. - pstorage_param.block_count = 9*20 +76; // 9 Sequences x 20 Stations + 76 blocks to fill out to 1k. - pstorage_param.cb = pstorage_cb_handler; - - return pstorage_register( &pstorage_param, &pstorage_id ); -} - - void periodicCallback( void ) { static unsigned long prevMillis = millis() +125; @@ -624,15 +503,7 @@ Ticker ticker; ////// ticker.attach( periodicCallback, 0.125 /** 0.2 **/ /** 0.5 **/ /* 1 */ ); - /* - // Thread thread( led_thread ); - */ - - /////pcfc.baud( 57600 ); - - /* - DEBUG( "Initialising the nRF51822\n\r" ); - */ + pc.baud( 57600 ); ble.init(); @@ -644,53 +515,8 @@ /* setup advertising */ setAdvData(); - /////pcfc.printf( "\r\nNano nano! I am \"%s\"\r\n", deviceName ); - - /* -#if LOOPBACK_MODE - pcfc.printf( "\r\nIn BLE Loopback mode.\r\n" ); -#endif - */ - srnd( rndHW() ); // Seed the sw RNG w/ the hw. - uint32_t p_count; - uint32_t pstorageErr = pstorage_init(); // This needs to be called, even though apparently called in bootloader--Check other stuff. - pstorage_access_status_get( &p_count ); - - -// pcfc.printf( "\r\nInitial pstorage count: %d\r\n", p_count ); - - if( NRF_SUCCESS != pstorageErr ) - { - // pcfc.printf( "\r\nWarn: pstorage init error: %x\r\n", pstorageErr ); - /// writeToPhone( "\r\nWarn: pstorage init error: %x\r\n", pstorageErr ); - - } else - { - pstorageErr = pstorage_setup(); - if( NRF_SUCCESS != pstorageErr ) - { - // pcfc.printf( "\r\nWarn: pstorage setup error: %x\r\n", pstorageErr ); - /// writeToPhone( "\r\nWarn: pstorage setup error: %x\r\n", pstorageErr ); - - } else - { - pstorageErr = pstorage_read_test(); - if( NRF_SUCCESS != pstorageErr ) - { - // pcfc.printf( "\r\nWarn: pstorage read attempt error: %x\r\n", pstorageErr ); - /// writeToPhone( "\r\nWarn: pstorage read attempt error: %x\r\n", pstorageErr ); - - } else - { - // In this test setup, we use the load callback to signal start to clear, - // then we use the clear callback to signal start to write. - } - } - } - - DeviceInformationService deviceInfo( ble, "TRX", "TrueAgility", "SN0001", "hw-rev1", "fw-rev1" ); BatteryService battService( ble ); battServiceP = &battService; @@ -718,6 +544,7 @@ setup(); radio_init(); tmr.start(); + init_datastore(); int mac_addr = ((int)macAddr[0]); mac_addr |= ((int)macAddr[1] << 8); @@ -725,29 +552,26 @@ unsigned long lastMillis = millis(); unsigned long currMillis = lastMillis +25; + int counter = 0; + // Main Loop - while( true ) // for( uint32_t loop=1; ;loop++ ) + while( true ) { -////// ble.waitForEvent(); - currMillis = millis(); - if( currMillis -lastMillis < 25 ) continue; + + if( currMillis -lastMillis < 25 ) + { + continue; + } + lastMillis = currMillis; periodicCallback(); -/**/ toPhoneChk(); // Write any pending data to phone. - - /* - while( 0 <= phone.getchar() ); // Eat input. - -// if( !(loop % 50) ) phone.printf( "Post: %d\r\n", tmr.read_ms() ); - - app_sched_execute(); // Attempt to get pstorage enqueued cmd callbacks. - */ + toPhoneChk(); #ifdef MASTER -/**/ int bytes = MIN( MAX_LEN, phoneP->readable() ); + int bytes = MIN( MAX_LEN, phoneP->readable() ); getRadioInput( phoneToDev, phoneP->read( phoneToDev, bytes, 1 ) ); #endif