示例#1
0
int mailbox::receive() {
  if(messageStateMachine(arduinoMessageState))
  {
    receiveCallback();
  }

}
 void onReceive(void(*receiveCallback)(const void* obj, const char* data), void* obj, const char* data)
 {
     os_printf("Received %d bytes!\n", strlen(data));
     if (receiveCallback != NULL) {
         if (obj != NULL)
             receiveCallback(obj, data);
     }
 }
示例#3
0
int mailbox::receive() {
 	//#define DALY 5
   unsigned int header[4] = {0}; //header 0 = '$', header[1] = checksum, header[2,3] = message length.
   SPI_Class::read(header, 1);
   int count = 0;
   while(header[0] != '$')
   {
   		count++;
   		if(count > 255)
   			break;
   }
   SPI_Class::read(header+1, 3);
//   unsigned int messageLength = 0;
   if(header[0] == '$')
   {
     inboxSize = (header[2] << 8) + header[3];
     if(inboxSize > 2048)
     {
       return; //safety check against ram murder.
     }
     unsigned int recvChecksum = header[1];
     
     inbox = (char*) realloc(inbox, inboxSize);
     //free(inbox);
     //inbox = (char*) malloc(inboxSize);

     //SPI_Class::read((unsigned int*) inbox, inboxSize);  
     for(unsigned int i = 0; i < inboxSize; i++)
     {
     	SPI_Class::read((unsigned int *) &inbox[i], 1);
     	delayMicroseconds(5); //we can potentially overwhelm the arduino with fast reads. Omitting this delay will not break the protocol but it will increase the error rate.
     }
     //compute checksum here
     unsigned int compChecksum = 0;
     for(unsigned int i = 0; i < inboxSize; i++)
     {
       compChecksum += inbox[i];
     }
     compChecksum &= 0x00FF;
     if(compChecksum != recvChecksum)
     {
		//Serial.println("bad checksum"); //you probably don't want to call the callback on a broken packer, but here it is.
     	//receiveCallback();
     	unsigned int thisByte = MB_BAD;
		SPI_Class::write(&thisByte,1);
     	return 0;
     }
     else
     {
     	unsigned int thisByte = MB_ACK;
		SPI_Class::write(&thisByte,1);
		receiveCallback();
		return 1;
     }
   }
   else
   {
     //Serial.println("bad header");
   	return 0;
   }

}