示例#1
0
BLEMate2::opResult BLEMate2::disconnect()
{
  String buffer;
  String EOL = String("\n\r"); // This is just handy.
  
  knownStart();
  _serialPort->print("DCN\r"); 
  _serialPort->flush();
  
  // We're going to use the internal timer to track the elapsed time since we
  //  issued the connect command. Bog-standard Arduino stuff.
  unsigned long disconnectStart = millis();
  
  buffer = "";

  // The timeout on this is 5 seconds; that may be a bit long.
  while (disconnectStart + 5000 > millis())
  {
    // Grow the current buffered data, until we receive the EOL string.    
    if (_serialPort->available() >0) buffer.concat(char(_serialPort->read()));
    
    if (buffer.endsWith(EOL))
    {
      if (buffer.startsWith("ERR")) return MODULE_ERROR;
      if (buffer.startsWith("DCN")) 
      {
        stdCmd("SCN OFF"); 
        return SUCCESS;
      }
      buffer = "";    
    }
  }
  return TIMEOUT_ERROR;
}
// Issue the "RESET" command over the serial port to the BC118. If it works, 
//  we expect to see a string that looks something like this:
//    Melody Smart v2.6.0
//    BlueCreation Copyright 2012 - 2014
//    www.bluecreation.com
//    READY
// If there is some sort of error, the module will respond with
//    ERR
// We'll buffer characters until we see an EOL (\n\r), then check the string.
BLEMate2::opResult BLEMate2::reset()
{
  String buffer;
  String EOL = String("\n\r");
  
  knownStart();
  
  // Now issue the reset command.
  _serialPort->print("RST");
  _serialPort->print("\r");
  _serialPort->flush();
  
  // We're going to use the internal timer to track the elapsed time since we
  //  issued the reset. Bog-standard Arduino stuff.
  unsigned long resetStart = millis();
  
  // This is our timeout loop. We'll give the module 6 seconds to reset.
  while ((resetStart + 6000) > millis())
  {
    // Grow the current buffered data, until we receive the EOL string.    
    if (_serialPort->available() > 0) 
    {
      char temp = _serialPort->read();
      buffer.concat(temp);
    }
    
    if (buffer.endsWith(EOL))
    {
      // If ERR or READY, we've finished the reset. Otherwise, just discard
      //  the data and wait for the next EOL.
      if (buffer.startsWith("ER")) return MODULE_ERROR;
      if (buffer.startsWith("RE")) 
      {
        stdCmd("SCN OFF"); // When we come out of reset, we *could* be
                           //  in scan mode. We don't want that; it's too
                           //  random and noisy.
        delay(500);        // Let the scanning noise complete.
        while(_serialPort->available())
        {
          _serialPort->read();
        } 
        return SUCCESS;
      }
      buffer = "";
    }    
  }
  return TIMEOUT_ERROR;
}
// Now, byte array.
BLEMate2::opResult BLEMate2::sendData(char *dataBuffer, byte dataLen)
{
  String EOL = String("\n\r");
  
  // BLE is a super low bandwidth protocol. The BC118 is only going to allow
  //  you to drop 20 bytes in central mode, or 125 bytes in peripheral mode.
  //  I don't want to burden the user with that, unduly, so I'm going to chop
  //  up their data and send it out in smaller blocks.
   
  // Thus, the first quetion is: am I in central mode, or not?
  boolean inCentralMode;
  amCentral(inCentralMode);

  // What we're now going to do is to build a String object with our buffer
  //  contents and then hit send on that buffer when it reaches a the length
  //  limited by the mode.

  byte outBufLenLimit = 20;
  if (!inCentralMode)
  {
    outBufLenLimit = 125;
  }

  byte inBufPtr = 0;
  byte outBufLen = 0;
  byte dataLeft = dataLen;

  opResult result = SUCCESS;
  while (inBufPtr < dataLen)
  {
    if (dataLeft < outBufLenLimit)
    {
      outBufLenLimit = dataLeft;
    }
    dataLeft -= outBufLenLimit;
    String outBuf;
    while (outBufLen < outBufLenLimit)
    {
      outBuf.concat(dataBuffer[inBufPtr++]);
      outBufLen++;
    }
    outBuf = "SND " + outBuf + "\r";
    result = stdCmd(outBuf);
    outBufLen = 0;
  }
  return result;
}
// One of the neat features of the BC127 is the ability to control an audio
//  player remotely. This function will activate those features, programmatically.
BC127::opResult BC127::musicCommands(audioCmds command)
{
  switch(command)
  {
    case PAUSE:
      return stdCmd("MUSIC PAUSE");
    case PLAY:
      return stdCmd("MUSIC PLAY");
    case FORWARD:
      return stdCmd("MUSIC FORWARD");
    case BACK:
      return stdCmd("MUSIC BACKWARD");
    case STOP:
      return stdCmd("MUSIC STOP");
    case UP:
      return stdCmd("VOLUME UP");
    case DOWN:
      return stdCmd("VOLUME DOWN");
    default:
      return INVALID_PARAM;
  }
}
示例#5
0
BLEMate2::opResult BLEMate2::BLENoAdvertise()
{
  return stdCmd("ADV OFF");
}
示例#6
0
// BLEAdvertise() and BLENoAdvertise() turn advertising on and off for this
//  module. Advertising must be turned on for another BLE device to detect the
//  module, and the module *must* be a peripheral for advertising to work (see
//  the "BLEPeripheral()" function.
BLEMate2::opResult BLEMate2::BLEAdvertise()
{
  return stdCmd("ADV ON");
}
BC127::opResult BC127::BLENoAdvertise()
{
  return stdCmd("ADVERTISING OFF");
}
BC127::opResult BC127::enterDataMode()
{
  return stdCmd("ENTER_DATA");
}
// Issue the "WRITE" command over the serial port to the BC118. This will
//  save the current settings to NVM, so they will be applied after a reset
//  or power cycle.
BLEMate2::opResult BLEMate2::writeConfig()
{
  return stdCmd("WRT");
}
示例#10
0
// Issue the "RESTORE" command over the serial port to the BC118. This will
//  reset the device to factory default settings, which is a good thing to do
//  once in a while.
BLEMate2::opResult BLEMate2::restore()
{
  return stdCmd("RTR");
}