size_t Adafruit_BLE_UART::write(uint8_t buffer)
{
  /* Blocking delay waiting for available credit(s) */
  while (0 == aci_state.data_credit_available)
  {
    pollACI();
    delay(10);
  }

#ifdef BLE_RW_DEBUG
  Serial.print(F("\tWriting one byte 0x")); Serial.println(buffer, HEX);
#endif
  if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX))
  {
    lib_aci_send_data(PIPE_UART_OVER_BTLE_UART_TX_TX, &buffer, 1);
    aci_state.data_credit_available--;

    delay(35); // required delay between sends
    return 1;
  }

  pollACI();

  return 0;
}
size_t Adafruit_BLE_UART::write(uint8_t * buffer, uint8_t len)
{
  uint8_t bytesThisPass, sent = 0;

#ifdef BLE_RW_DEBUG
  Serial.print(F("\tWriting out to BTLE:"));
  for (uint8_t i=0; i<len; i++) {
    Serial.print(F(" 0x")); Serial.print(buffer[i], HEX);
  }
  Serial.println();
#endif

  while(len) { // Parcelize into chunks
    bytesThisPass = len;
    if(bytesThisPass > ACI_PIPE_TX_DATA_MAX_LEN)
       bytesThisPass = ACI_PIPE_TX_DATA_MAX_LEN;

    if(!lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX))
    {
      pollACI();
      continue;
    }

    lib_aci_send_data(PIPE_UART_OVER_BTLE_UART_TX_TX, &buffer[sent],
      bytesThisPass);
    aci_state.data_credit_available--;

    delay(35); // required delay between sends

    if(!(len -= bytesThisPass)) break;
    sent += bytesThisPass;
  }

  return sent;
}
bool GoosciBleGatt::sendData(const char *buffer, int size) {
  const uint8_t max_packet_size = BTLE_BUFFER_SIZE - 2;
  /* Force size/max_packet_size to round up */
  uint8_t num_packets = (size + max_packet_size - 1) / max_packet_size;
  bool send_complete = true;
  for (uint8_t ii = 0; ii < num_packets; ii++) {
    
    bool is_last_packet = ((num_packets - 1) == ii);
    /* There are 3 possibilities for current_packet_size
       1) It is the last packet and the remaining data is smaller than our allocated buffer (size % max_packet_size)
       2) It is the last packet and the remaining data is equal to our allocated buffer (max_packet_size)
       3) It is not the last packet (max_packet_size)
    */
    uint8_t current_packet_size = (is_last_packet ? ((size % max_packet_size == 0) ? max_packet_size : (size % max_packet_size)) : max_packet_size);
    packet[0] = current_packet_size;
    packet[1] = is_last_packet;
    memcpy((void*)(packet + 2), buffer + ii * max_packet_size, current_packet_size);
    
    /* If send fails then we give up */
    if (!(send_complete = lib_aci_send_data(PIPE_GOOSCI_SENSOR_VALUE_TX, packet, current_packet_size + 2)))
      break;
      
    aci_state.data_credit_available--;
    while (!is_last_packet && !isReadyToSend())
      pollACI();
  }
  
  return send_complete;
}
Ejemplo n.º 4
0
size_t Adafruit_BLE_UART::write(uint8_t buffer)
{
  bool    status = false;
  size_t  sent   = 0;

  #ifdef BLE_RW_DEBUG
    Serial.print(F("\tWriting one byte 0x")); Serial.println(buffer, HEX);
  #endif
  if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX))
  {
    // Get back whether we actually sent this bit or not
    status = lib_aci_send_data(PIPE_UART_OVER_BTLE_UART_TX_TX, &buffer, 1);
    
    // Validate that we actually sent the bit then
    // move the counter
    if ( status )
    {
      aci_state.data_credit_available--;  
      sent = 1;
    }
    
    delay(BLE_W_DELAY); // required delay between sends
    
    return sent;
  }

  pollACI();
  
  return sent;
}
/**************************************************************************
 *
 *   Configures the nRF8001 and starts advertising the service
 *
 *   @param[in]  advTimeout
 *               The advertising timeout in seconds
 *               (0 = infinite advertising).
 *   @param[in]  advInterval
 *               The delay between advertising packets in 0.625ms units
 *               (1600 = 1 second).
 *
 **************************************************************************/
bool GoosciBleGatt::begin(int advTimeout, int advInterval) {
  // Store the advertising timeout and interval
  // TODO(nmai): check for valid ranges.
  adTimeout = advTimeout;
  adInterval = advInterval;

  // Setup the service data from nRFGo Studio (services.h)
  if (NULL != services_pipe_type_mapping) {
    aci_state.aci_setup_info.services_pipe_type_mapping =
        &services_pipe_type_mapping[0];
  } else {
    aci_state.aci_setup_info.services_pipe_type_mapping = NULL;
  }

  aci_state.aci_setup_info.number_of_pipes = NUMBER_OF_PIPES;
  aci_state.aci_setup_info.setup_msgs = (hal_aci_data_t *)setup_msgs;
  aci_state.aci_setup_info.num_setup_msgs = NB_SETUP_MESSAGES;

  // Setup the nRF8001 pins.
  aci_state.aci_pins.board_name = BOARD_DEFAULT;
  aci_state.aci_pins.reqn_pin = _REQ;
  aci_state.aci_pins.rdyn_pin = _RDY;
  aci_state.aci_pins.mosi_pin = MOSI;
  aci_state.aci_pins.miso_pin = MISO;
  aci_state.aci_pins.sck_pin = SCK;

  // SPI_CLOCK_DIV8  = 2MHz SPI speed.
  aci_state.aci_pins.spi_clock_divider = SPI_CLOCK_DIV8;

  // The Active pin is optional and can be marked UNUSED.
  aci_state.aci_pins.reset_pin = _RST;
  aci_state.aci_pins.active_pin = UNUSED;
  aci_state.aci_pins.optional_chip_sel_pin = UNUSED;

  // Interrupts still not available in Chipkit.
  aci_state.aci_pins.interface_is_interrupt = false;
  aci_state.aci_pins.interrupt_number = 1;

  // The second parameter is for turning debug printing on
  // for the ACI Commands and Events so they be printed on the Serial
  lib_aci_init(&aci_state, false);

  // Get the device address
  lib_aci_get_address();
  // Wait for the get address response
  addrReceived = false;
  while (!addrReceived) {
    pollACI();
  }

  return true;
}
Ejemplo n.º 6
0
void  RCTelemetry_BLE::sendRSSI(uint8_t rssi)
{
	if (lib_aci_is_pipe_available(&aci_state, PIPE_RC_TELEMETRY_RSSI_TX)) {
		lib_aci_send_data(PIPE_RC_TELEMETRY_RSSI_TX, &rssi, 1);
		aci_state.data_credit_available--;

		delay(35); // required delay between sends
		return;
	}

	pollACI();

	// TODO: how to handle when pipe is not available? retry? cache for later? warning on overriding cache?
}
Ejemplo n.º 7
0
Archivo: main.c Proyecto: linkinlzm/MCU
int main(void)
{
    WDTCTL = WDTPW + WDTHOLD;             // Stop watchdog timer

    initializeClocks();
    InitializeButton();
    InitializeLeds();

    //This will reset the nRF8001. ACI Device Started Event is generated by the nRF8001 device
    //as soon as the reset is complete
    hal_aci_tl_init();

    // Reset nRF8001
    resetDevice();
    _BIS_SR(GIE);

    begin_BLE(&aci_state);

    // Main application loop
    for (;;)
    {
        //_BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/interrupt
        _nop();                   // For debugger
        //Not entirely sure if any of this if statement needs to be here...
        if(rdynFlag == 1)
        {
            rdynFlag = 0;

            m_rdy_line_handle();
        }
        pollACI(&aci_state, &aci_data, &aci_cmd);
        state = getState();
        if ((getState() == ACI_EVT_CONNECTED) && (!ctr) && lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX)) {
            //make sure pipe is available
//#define UART
#ifdef UART
            write("80 98 37 998", 12, &aci_state, &aci_data, &aci_cmd);
#else
            write(data, 6, &aci_state, &aci_data, &aci_cmd);
#endif
            //lib_aci_get_battery_level();
        }
        ctr++;
    }
}