Ejemplo n.º 1
0
/* This function will attempt a connection to the stored remote address
   The first time you connect the the RN-42 HID, the master device will
   need to initiate the connection. The first time a connection is made
   the bluetooth address of the master device will be stored on the RN-42.
   If no remote address is stored, a connection will not be made. */
uint8_t makeyMateClass::connect()
{
  freshStart();  // Get the module disconnected, and out of command mode
  
  while (!enterCommandMode())
  {  // Enter command mode
    delay(BLUETOOTH_RESPONSE_DELAY);
  }
  delay(BLUETOOTH_RESPONSE_DELAY);
  bluetooth.flush();
  
  /* get the remote address and print it in the serial monitor */
  bluetooth.print("GR");  // Get the remote address
  bluetooth.write('\r');
  delay(BLUETOOTH_RESPONSE_DELAY);
  if (bluetooth.peek() == 'N')  // Might say "No remote address stored */
  {  // (bluetooth address is hex values only, so won'te start with 'N'.
    Serial.println("Can't connect. No paired device!");
    bluetooth.flush();
    bluetooth.print("---");  // exit command mode
    bluetooth.write('\r');
    return 0;  // No connect is attempted
  }
  else if (bluetooth.available() == 0)  
  { // If we can't communicate with the module at all, print error
    Serial.println("ERROR!");
    return 0;  // return error
  }
  /* otherwise print the address we're trying to connect to */
  Serial.print("Attempting to connect to: ");
  while (bluetooth.available())
    Serial.write(bluetooth.read());
    
  /* Attempt to connect */
  bluetooth.print("C");  // The connect command
  bluetooth.write('\r');
  delay(BLUETOOTH_RESPONSE_DELAY);
  while (bluetooth.available())
    Serial.write(bluetooth.read());  // Should print "TRYING"
  
  return 1;
}
/* echoMode() will set up an echo interface betwen bluetooth and the Arduino hardware serial
	This is mostly useful if you're having trouble connecting from the BlueSMiRF to MetaWatch.
	
	Anything sent to the Arduino's hardware serial (e.g. entered into the Serial Monitor)
	will be echoed out to the software serial (bluetooth module). ANything sent to the bluetooth
	module will be echoed out of the Arduino's hardware serial (e.g. visible in the Serial monitor).
	
	It's not recommended to try to send message packets to the watch using this echo mode (unless
	you're some kind of genius CRC calculator).
	
	To exit echo mode, type ~~~.
*/
void SFE_MetaWatch::echoMode()
{
	int c;
	int tildeCount = 0;
	Serial.println("Echo mode. Press ~~~ to escape.");

	while(tildeCount < 3)
	{
		if (bt.available())
		{
			c = bt.peek();
			Serial.write(bt.read());
			if (c == '~') tildeCount++;
		}
		if (Serial.available())
		{
			c = Serial.peek();
			bt.write(Serial.read());
			if (c == '~') tildeCount++;
		}
	}
	Serial.println("Exiting echo mode...");
}
Ejemplo n.º 3
0
// * Receiving the lenght of bytes from Serial port
void HerkulexClass::readData(int size)
{
	int i = 0;
    int beginsave=0;
    int Time_Counter=0;
    switch (port)
	{
	case SSerial:

        while((SwSerial.available() < size) & (Time_Counter < TIME_OUT)){
        		Time_Counter++;
        		delayMicroseconds(1000);  //wait 1 millisecond for 10 times
		}
        	
		while (SwSerial.available() > 0){
			byte inchar = (byte)SwSerial.read();
			if ( (inchar == 0xFF) & ((byte)SwSerial.peek() == 0xFF) ){
					beginsave=1; 
					i=0; 				 // if found new header, begin again
			}
			if (beginsave==1 && i<size) {
				   dataEx[i] = inchar;
				   i++;
			}
		}
		SwSerial.flush();
		break;
	
	#if defined (__AVR_ATmega1280__) || defined (__AVR_ATmega128__) || defined (__AVR_ATmega2560__)
	case HSerial1:
		while((Serial1.available() < size) & (Time_Counter < TIME_OUT)){
				Time_Counter++;
				delayMicroseconds(1000);
		}
		while (Serial1.available() > 0){
			byte inchar = (byte)Serial1.read();
			//printHexByte(inchar);
			if ( (inchar == 0xFF) & ((byte)Serial1.peek() == 0xFF) ){
				beginsave=1;
				i=0;
			}
            if (beginsave==1 && i<size) {
                       dataEx[i] = inchar;
                       i++;
			}
		}
		break;
	
	case HSerial2:
	    while((Serial2.available() < size) & (Time_Counter < TIME_OUT)){
        		Time_Counter++;
        		delayMicroseconds(1000);
		}
        	
		while (Serial2.available() > 0){
			byte inchar = (byte)Serial2.read();
			if ( (inchar == 0xFF) & ((byte)Serial2.peek() == 0xFF) ){
					beginsave=1;
					i=0; 					
			}
			if (beginsave==1 && i<size) {
				   dataEx[i] = inchar;
				   i++;
			}
		}
		break;

	case HSerial3:
		while((Serial3.available() < size) & (Time_Counter < TIME_OUT)){
			Time_Counter++;
			delayMicroseconds(1000);
		}
		
		while (Serial3.available() > 0){
			byte inchar = (byte)Serial3.read();
			if ( (inchar == 0xFF) & ((byte)Serial3.peek() == 0xFF) ){
					beginsave=1;
					i=0; 
			}
			if (beginsave==1 && i<size) {
				   dataEx[i] = inchar;
				   i++;
			}
		}
		break;
	#elif (__AVR_ATmega32U4__)
	case HSerial1:
		while((Serial1.available() < size) & (Time_Counter < TIME_OUT)){
			Time_Counter++;
			delayMicroseconds(1000);
		}
		while (Serial1.available() > 0){
			byte inchar = (byte)Serial1.read();
			//printHexByte(inchar);
			if ( (inchar == 0xFF) & ((byte)Serial1.peek() == 0xFF) ){
				beginsave=1;
				i=0;
			}
            if (beginsave==1 && i<size) {
                       dataEx[i] = inchar;
                       i++;
			}
		}
		break;
	#endif
	}
}