예제 #1
0
파일: radio.c 프로젝트: shunran/ble-mon
/**@brief Send data through BLE UART service with maximum throughput. */
static void ble_nus_data_transfer()
{
    uint32_t        err_code;

    if (m_data_length == 0)    //< All data is sent.
    {
        m_file_in_transit = false;
        err_code = ble_nus_string_send(&m_nus, (uint8_t *) "**END**", 7);     //< End indicator
        APP_ERROR_CHECK(err_code);
        return;
    }

    /** @note    Maximize BLE throughput
      *          https://devzone.nordicsemi.com/question/1741/dealing-large-data-packets-through-ble/
      */
    while (1)
    {
    	if (m_data_length == 0) break;
        err_code = ble_nus_string_send(&m_nus, m_nus_data, m_data_length);
        //test_logf("First nus send with len %d and err %d", m_data_length, err_code);
        if (err_code == BLE_ERROR_NO_TX_BUFFERS ||
            err_code == NRF_ERROR_INVALID_STATE ||
            err_code == BLE_ERROR_GATTS_SYS_ATTR_MISSING)
        {
            break;
        }
        else if (err_code != NRF_SUCCESS)
        {
            APP_ERROR_CHECK(err_code);
        }
        read_store_data(m_nus_data, &m_data_length);
    }
}
예제 #2
0
/*JSON{
    "type" : "staticmethod",
    "class" : "Bluetooth",
    "name" : "send_string",
    "generate" : "jswrap_nrf_bluetooth_send_string",
    "params" : [
    ["string_to_send","pin","The string to be sent to the BLE central via Nordic's UART service."],
    ["length","int","The length of string."]
  ]
}*/
void jswrap_nrf_bluetooth_send_string(Pin* string_to_send, int length)
{
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    static uint8_t index = 0;
    uint32_t       err_code;

    uint32_t i;
    for (i = 0; i < length; i++)
    {
    	data_array[index] = (uint8_t) string_to_send[i];

    	index++;

    	if ((data_array[index - 1] == '\n') || (index >= (BLE_NUS_MAX_DATA_LEN)) || (i == (length - 1)))
    	{
    		err_code = ble_nus_string_send(&m_nus, data_array, index);
    		if (err_code != NRF_ERROR_INVALID_STATE)
    		{
    			APP_ERROR_CHECK(err_code);
    		}

    		index = 0;
    	}

    }
}
예제 #3
0
/*JSON{
  "type" : "idle",
  "generate" : "jswrap_nrf_idle"
}*/
bool jswrap_nrf_idle() {
  static uint8_t buf[BLE_NUS_MAX_DATA_LEN];
  int idx = 0;
  int ch = jshGetCharToTransmit(EV_BLUETOOTH);
  while (ch>=0 && idx<BLE_NUS_MAX_DATA_LEN) {
    buf[idx++] = ch;
    ch = jshGetCharToTransmit(EV_BLUETOOTH);
  }
  if (idx>0) ble_nus_string_send(&m_nus, buf, idx);
  return idx>0;
}
예제 #4
0
bool BLEwrite(uint8_t* data, uint16_t len)  {
    uint32_t err_code = ble_nus_string_send(&m_nus, data, len);
    if (err_code != NRF_SUCCESS)  {
        //debug_log("BLEwrite error: %u\r\n",(unsigned int)err_code);
        //BLEwrite fails if we try writing before the master has received the previous packet
        //Can happen frequently while trying to send a large chunk of data
        return false;
    }
    
    ble_timeout_set(CONNECTION_TIMEOUT_MS);  // refresh connection timeout
    
    return true;
}
예제 #5
0
uint32_t bt_send_to_AG(uint8_t* buff,uint16_t temp_length,UINT8 ser_flag)
{
	//uint32_t	   err_code;
	//err_code=ble_nus_string_send(&m_nus, buff, temp_length);

   if(ser_flag==SERVICE_COS)
   {
   return ble_nus_string_send(&m_nus, buff, temp_length);
   }
	 else
	 {
	 return new_service_ble_nus_string_send(&new_m_nus, buff, temp_length);
	 }
}
예제 #6
0
/**@brief Update the game, given a location to place the disc
 */
void addToColumn(uint8_t col) {
	uint8_t currColor = p1_turn ? RED : BLUE, row;
	
	if(discs[col] < HEIGHT) {
		//insert the disc
		row = discs[col];
		discs[col]++;
		game[row][col] = currColor;
		
		char temp_[24];
		sprintf(temp_, "\nDisc added at %d, %d\n", col, row);
		SEGGER_RTT_WriteString(0, temp_);
		
		//Send data to Bluetooth connection
		char temp[14]; 
		uint8_t send[14];
		sprintf(temp, "Received %d %d", col, row);
		uint8_t i = 0;
		while(temp[i] != '\0') { //Accounts for 1 or 2 digit numbers
			send[i] = temp[i];
			i++;
		}
	
		uint32_t err_code = ble_nus_string_send(&m_nus, send, i);
		if(err_code != NRF_SUCCESS)
			APP_ERROR_CHECK(err_code);
		
		//check if game is over
		if(checkWin(row, col)) {
			switch(p1_turn) {
				case true:
					SEGGER_RTT_WriteString(0, "Player 1 Wins!\n");
					break;
				default:
					SEGGER_RTT_WriteString(0, "Player 2 Wins!\n");
					break;
			}
		}
		
		p1_turn = !p1_turn;
	} else
		SEGGER_RTT_WriteString(0, "\nNo more moves may be made on this column\n");
}
예제 #7
0
/**@snippet [Handling the data received over UART] */
void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    static uint8_t index = 0;
    uint32_t       err_code;

    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
            index++;

            if ((data_array[index - 1] == '\n') || (index >= (m_ble_nus_max_data_len)))
            {
                NRF_LOG_DEBUG("Ready to send data over BLE NUS\r\n");
                NRF_LOG_HEXDUMP_DEBUG(data_array, index);

                do
                {
                    err_code = ble_nus_string_send(&m_nus, data_array, index);
                    if ( (err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_BUSY) )
                    {
                        APP_ERROR_CHECK(err_code);
                    }
                } while (err_code == NRF_ERROR_BUSY);

                index = 0;
            }
            break;

        case APP_UART_COMMUNICATION_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_code);
            break;

        default:
            break;
    }
}
예제 #8
0
bool jswrap_nrf_transmit_string() {
  if (m_conn_handle == BLE_CONN_HANDLE_INVALID) {
    // If no connection, drain the output buffer
    while (jshGetCharToTransmit(EV_BLUETOOTH)>=0);
  }
  if (bleStatus & BLE_IS_SENDING) return false;
  static uint8_t buf[BLE_NUS_MAX_DATA_LEN];
  int idx = 0;
  int ch = jshGetCharToTransmit(EV_BLUETOOTH);
  while (ch>=0) {
    buf[idx++] = ch;
    if (idx>=BLE_NUS_MAX_DATA_LEN) break;
    ch = jshGetCharToTransmit(EV_BLUETOOTH);
  }
  if (idx>0) {
    if (ble_nus_string_send(&m_nus, buf, idx) == NRF_SUCCESS)
      bleStatus |= BLE_IS_SENDING;
  }
  return idx>0;
}
예제 #9
0
void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
    if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
    {
        ret_code_t err_code;
        uint16_t adc_value;
        uint8_t value[SAADC_SAMPLES_IN_BUFFER*2];
        uint8_t bytes_to_send;
     
        // set buffers
        err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAADC_SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);
						
        // print samples on hardware UART and parse data for BLE transmission
        printf("ADC event number: %d\r\n",(int)m_adc_evt_counter);
        for (int i = 0; i < SAADC_SAMPLES_IN_BUFFER; i++)
        {
            printf("%d\r\n", p_event->data.done.p_buffer[i]);

            adc_value = p_event->data.done.p_buffer[i];
            value[i*2] = adc_value;
            value[(i*2)+1] = adc_value >> 8;
        }
				
        // Send data over BLE via NUS service. Makes sure not to send more than 20 bytes.
        if((SAADC_SAMPLES_IN_BUFFER*2) <= 20) 
        {
            bytes_to_send = (SAADC_SAMPLES_IN_BUFFER*2);
        }
        else 
        {
            bytes_to_send = 20;
        }
        err_code = ble_nus_string_send(&m_nus, value, bytes_to_send);
        if (err_code != NRF_ERROR_INVALID_STATE) 
        {
            APP_ERROR_CHECK(err_code);
        }
						
        m_adc_evt_counter++;
    }
예제 #10
0
/**
 * @brief ADC interrupt handler.
 * Prints ADC results on hardware UART and over BLE via the NUS service.
 */
static void adc_event_handler(nrf_drv_adc_evt_t const * p_event)
{
    uint8_t adc_result[ADC_BUFFER_SIZE*2];
	
    if (p_event->type == NRF_DRV_ADC_EVT_DONE)
    {
        adc_event_counter++;
        NRF_LOG_PRINTF("  adc event counter: %d\r\n", adc_event_counter);
        for (uint32_t i = 0; i < p_event->data.done.size; i++)
        {
            NRF_LOG_PRINTF("ADC value channel %d: %d\r\n", (i % number_of_adc_channels), p_event->data.done.p_buffer[i]);
            adc_result[(i*2)] = p_event->data.done.p_buffer[i] >> 8;
            adc_result[(i*2)+1] = p_event->data.done.p_buffer[i];
        }
        if(ADC_BUFFER_SIZE <= 10)
        {
            ble_nus_string_send(&m_nus, &adc_result[0], ADC_BUFFER_SIZE*2);
        }	
        APP_ERROR_CHECK(nrf_drv_adc_buffer_convert(adc_buffer,ADC_BUFFER_SIZE));
        LEDS_INVERT(BSP_LED_3_MASK);
    }
예제 #11
0
// Function for handling app_uart events.
void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    static uint8_t index = 0;
    uint32_t       err_code;

    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
            index++;

            if ((data_array[index - 1] == '\n') || 
                (index >= (BLE_NUS_MAX_DATA_LEN)))
            {
                err_code = ble_nus_string_send(&m_nus, data_array, index);
                if (err_code != NRF_ERROR_INVALID_STATE)
                {
                    APP_ERROR_CHECK(err_code);
                }
                
                index = 0;
            }
            break;

        case APP_UART_COMMUNICATION_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_code);
            break;

        default:
            break;
    }
}
예제 #12
0
void bta_bleUartTransmit(uint8_t * p_data, uint16_t length){
	uint32_t err_code=ble_nus_string_send(&m_nus, p_data, length);
  APP_ERROR_CHECK(err_code);
}
예제 #13
0
파일: radio.c 프로젝트: shunran/ble-mon
/**@snippet [Handling the data received over BLE] */
static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
{
	uint32_t        err_code;
	 if (length==1) //< Control Command
	    {
	        switch(p_data[0])
	        {
	        /** Radio transmit power in dBm
	         * (accepted values are -40, -30, -20, -16, -12, -8, -4, 0, and 4 dBm). */
            case '1':
            	err_code = ble_nus_string_send(p_nus, (uint8_t *) "Power set to -40", 16);
            	APP_ERROR_CHECK(err_code);
            	gap_tx_power = -40;
            	err_code = sd_ble_gap_tx_power_set(gap_tx_power);
            	APP_ERROR_CHECK(err_code);
                break;
            case '2':
            	err_code = ble_nus_string_send(p_nus, (uint8_t *) "Power set to -20", 16);
            	APP_ERROR_CHECK(err_code);
            	gap_tx_power = -20;
            	err_code = sd_ble_gap_tx_power_set(gap_tx_power);
            	APP_ERROR_CHECK(err_code);
            	break;
            case '3':
            	err_code = ble_nus_string_send(p_nus, (uint8_t *) "Power set to 0", 14);
            	APP_ERROR_CHECK(err_code);
            	gap_tx_power = 0;
            	err_code = sd_ble_gap_tx_power_set(gap_tx_power);
            	APP_ERROR_CHECK(err_code);
            	break;
            case 'c':
            	;
            	storage_clear();
            	err_code = ble_nus_string_send(p_nus, (uint8_t *) "Storage cleared.", 16);
            	APP_ERROR_CHECK(err_code);
            	break;
            case 'l':
            	;
            	char str_l[17];
            	sprintf(str_l, "Toggled lock to %d", storage_toggle_lock());
            	ble_nus_string_send(p_nus, (uint8_t *) str_l, 18);
            	break;
            case 'r':
            	store_read_init();
            	read_store_data(m_nus_data, &m_data_length);
            	nrf_delay_ms(MAX_CONN_INTERVAL * 2);
            	m_file_in_transit = true;
            	err_code = ble_nus_string_send(p_nus, (uint8_t *) "***DATA TRANSFER***", 19);
            	APP_ERROR_CHECK(err_code);

            	break;
            case 't':
            	;
            	char str_t[20];
            	uint8_t c;
            	uint32_t ts = NRF_RTC1->COUNTER;
            	c = sprintf(str_t, "ts:%lu epoch:%d", ts, timer_epoch);
            	err_code = ble_nus_string_send(p_nus, (uint8_t *) str_t, c);
            	 APP_ERROR_CHECK(err_code);
            	break;
	        }
	    }
	__LOG("NUSCON:%s", p_data);
}
예제 #14
0
int main(void)
{
    uint32_t err_code;
    bool erase_bonds;
    uint8_t  start_string[] = START_STRING;
    
    // Initialize.
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
    uart_init();
    buttons_leds_init(&erase_bonds);
    ble_stack_init();
    gap_params_init();
    services_init();
    advertising_init();
    conn_params_init();
    
    printf("%s",start_string);

    err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
    shiftRegInit();
    // Enter main loop.
    while (1)
    {
		if(!(nrf_gpio_pin_read(13))) {
			ble_nus_string_send(&m_nus, solution, sizeof(solution));
		}
		if(!(nrf_gpio_pin_read(14)) && knappSjekk) {
			solution[6] = 1;
			ble_nus_string_send(&m_nus, solution, sizeof(solution));
			knappSjekk = false;
		}
		if (nrf_gpio_pin_read(14)) {
			solution[6] = 0;
			knappSjekk = true;
		}
//		if(!(nrf_gpio_pin_read(14))) {
//			
//			nrf_gpio_pin_toggle(18);
//			nrf_delay_ms(150);
//			nrf_gpio_pin_toggle(18);
//			nrf_delay_ms(150);
//		}
		if(nrf_gpio_pin_read(btn1)) {
			btn1_counter = testFunksjon(btn1, btn1_counter);
			solution[0] = btn1_counter;
		}
		else if(nrf_gpio_pin_read(btn2)) {
			btn2_counter = testFunksjon(btn2, btn2_counter);
			solution[1] = btn2_counter;		
		}
		else if(nrf_gpio_pin_read(btn3)) {
			btn3_counter = testFunksjon(btn3, btn3_counter);
			solution[2] = btn3_counter;			
		}
		else if(nrf_gpio_pin_read(btn4)) {
			btn4_counter = testFunksjon(btn4, btn4_counter);
			solution[3] = btn4_counter;			
		}
		else if(nrf_gpio_pin_read(btn5)) {
			btn5_counter = testFunksjon(btn5, btn5_counter);
			solution[4] = btn5_counter;			
		}
		else if(nrf_gpio_pin_read(btn6)) {
			btn6_counter = testFunksjon(btn6, btn6_counter);
			solution[5] = btn6_counter;
		}
		shiftRegWrite(ledMatriseA[btn1_counter]+ledMatriseB[btn2_counter], ledMatriseA[btn3_counter]+ledMatriseB[btn4_counter], ledMatriseA[btn5_counter]+ledMatriseB[btn6_counter]);
		
		

        //power_manage();
    }
}