Last active
August 29, 2015 14:14
-
-
Save fernandozamoraj/530ee4e4fd0d5d413dbb to your computer and use it in GitHub Desktop.
fzj_msg_sender_arduino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
fzj_msg_sender | |
converts bytes into binary digits and sends them out | |
by Fernando Zamora | |
*/ | |
const int MAX_MESSAGE_BIT_SIZE = 128; //nice even sixteen bytes of data | |
const int MAX_MESSAGE_CHAR_SIZE = 16; //nice even sixteen bytes of data | |
const int DELAY = 1; | |
// the setup function runs once when you press reset or power the board | |
void setup() { | |
// initialize digital pin 13 as an output. | |
pinMode(13, OUTPUT); | |
Serial.begin(9600); | |
} | |
// the loop function runs over and over again forever | |
void loop() { | |
transmitMessage(); | |
} | |
void transmitMessage(){ | |
char message[] = "hello world\0"; | |
int encodedMessage[MAX_MESSAGE_BIT_SIZE]={0}; | |
encodeMessage(message, encodedMessage); | |
transmitMessage(encodedMessage); | |
} | |
void runTests(){ | |
testEncoding(); | |
testTransmission(); | |
testDecodeByte(); | |
testDecodeMessage(); | |
} | |
/*********************************************** | |
TESTS FOR TRANSMISSION | |
***********************************************/ | |
void testTransmission(){ | |
Serial.println(""); | |
Serial.println("**** TESTING TRANSMIT ****"); | |
Serial.println(" transmitting \"hello\" "); | |
Serial.println(""); | |
char message[] = "hello\0"; | |
int encodedMessage[MAX_MESSAGE_BIT_SIZE]={0}; | |
encodeMessage(message, encodedMessage); | |
transmitMessage(encodedMessage); | |
} | |
/************************************************ | |
TESTS FOR ENCODING | |
*************************************************/ | |
void testEncoding(){ | |
Serial.println(""); | |
Serial.println("**** TESTING ENCODING ****"); | |
Serial.println(" encoding \"hello\" "); | |
Serial.println(""); | |
char message[] = "hello\0fool"; | |
int encodedMessage[MAX_MESSAGE_BIT_SIZE]={0}; | |
encodeMessage(message, encodedMessage); | |
displayFullEncodedMessage(encodedMessage); | |
Serial.println(""); | |
Serial.println("*** TEST ENCODING IS COMPLETE *****"); | |
Serial.println(""); | |
delay(4000); | |
} | |
void displayFullEncodedMessage(int* encodedMessage){ | |
for(int i = 0; i < MAX_MESSAGE_BIT_SIZE; i++){ | |
if(i%8==0){ | |
Serial.println(""); | |
} | |
Serial.print(encodedMessage[i]); | |
} | |
} | |
void displayBitArray(byte val, int* bitArray){ | |
Serial.println(""); | |
Serial.println(val); | |
Serial.println((char)val); | |
for(int i = 0; i < 8; i++){ | |
Serial.print(bitArray[i]); | |
} | |
Serial.println(""); | |
} | |
void testDecodeByte(){ | |
Serial.println("**************TESTING DECODING BYTE**********"); | |
Serial.println(""); | |
for(int i = 0; i <24;i++){ | |
int bitArray[8] = {0}; | |
encodeByte(i, bitArray); | |
int result = decodeByte(bitArray); | |
Serial.println(""); | |
Serial.print("DECODED: "); | |
Serial.print(result); | |
Serial.println(""); | |
} | |
Serial.print(""); | |
Serial.println("*******************DECODING********************"); | |
} | |
void testDecodeMessage(){ | |
Serial.println(""); | |
Serial.println("**** TESTING DECODING ****"); | |
Serial.println(" encoding and then decoding \"hello\" "); | |
Serial.println(""); | |
char message[] = "decoded hello\0"; | |
int encodedMessage[MAX_MESSAGE_BIT_SIZE]={0}; | |
char decodedMessage[MAX_MESSAGE_CHAR_SIZE]={0}; | |
encodeMessage(message, encodedMessage); | |
decodeMessage(encodedMessage, decodedMessage); | |
Serial.println("DECODED MESSAGE: "); | |
Serial.println(decodedMessage); | |
Serial.println(""); | |
Serial.println("*** TEST DECODING IS COMPLETE *****"); | |
Serial.println(""); | |
delay(4000); | |
} | |
/********************************************************** | |
ENCODE API SECTION | |
***********************************************************/ | |
//converts bytes to bit array | |
//so it would take the value 5 for example | |
//and convert it to the array with values 0000 0101 | |
//it uses bitShifting so that only the last byte has to be tested | |
void encodeByte(byte byteValue, int* values){ | |
int tempValue = byteValue; | |
for(int i = 7; i > -1; i--){ | |
values[i] = tempValue & 1; | |
tempValue = tempValue >> 1; | |
} | |
} | |
/* | |
decodeByte converts the 8 element array of 1's and0's | |
and turns it into a single numeric value | |
*/ | |
int decodeByte(int* bitArray){ | |
int tempValue = 0; | |
int result = 0; | |
for(int i = 7; i > -1; i--){ | |
tempValue = bitArray[i]; | |
//shift the bit to give it the appropriate positional value | |
tempValue = tempValue << (7-i); | |
//add that value to the running total | |
result = result + tempValue; | |
} | |
return result; | |
} | |
/* | |
Encodes the string in message into the encodedMessage | |
the message is in ascii characters | |
the encoded message is an array of 0's and 1's | |
*/ | |
void encodeMessage(char* message, int* encodedMessage){ | |
//loop through all the characters in the message array | |
//e.g. h e l l o in the message "hello" | |
for(int i = 0; i < MAX_MESSAGE_CHAR_SIZE; i++){ | |
if((byte)message[i] == 0){ | |
break; | |
} | |
int bitArray[8]; //holder for the individual bits | |
encodeByte((byte)message[i], bitArray); | |
append(encodedMessage, i*8, bitArray); | |
} | |
} | |
/* | |
decodeMessage takes the array of 1's and 0's in encodedMessage | |
and converts to a string into the decodedMessage | |
*/ | |
void decodeMessage(int* encodedMessage, char* decodedMessage){ | |
for(int i = 0; i < MAX_MESSAGE_CHAR_SIZE; i++){ | |
int bitArray[8] = {0}; | |
//every 8 elements creates a single byte to decode | |
int startingIndex = i*8; | |
for(int j=0; j<8; j++){ | |
bitArray[j] = encodedMessage[startingIndex+j]; | |
} | |
//decode the byte and append it to the decoded message | |
decodedMessage[i] = (char)decodeByte(bitArray); | |
} | |
//the last element of the message is reserved for the terminating character | |
decodedMessage[MAX_MESSAGE_CHAR_SIZE-1] = '\0'; | |
} | |
//since characters are encoded one byte at a time | |
//this append function takes care of appending each 8 bit array | |
//to a full message bit array | |
void append(int* encodedMessage, int startingIndex, int* bitArray){ | |
for(int i=0; i < 8; i++){ | |
encodedMessage[startingIndex+i] = bitArray[i]; | |
} | |
} | |
/********************************************************** | |
TRANSMIT/RECEIVE API SECTION | |
***********************************************************/ | |
void transmitMessage(int* encodedMessage){ | |
int onOrOff[2] = {LOW,HIGH}; | |
//transmit start packet | |
//three bytes of alternating 0's and 1's | |
for(int i = 0; i < 3; i++){ | |
for(int j=0; j < 4; j++){ | |
digitalWrite(13, LOW); | |
delay(DELAY); | |
digitalWrite(13, HIGH); | |
delay(DELAY); | |
} | |
} | |
//end of packet off for 1 byte | |
for(int i = 0; i < 8; i++){ | |
digitalWrite(13, LOW); | |
delay(DELAY); | |
} | |
//send message | |
for(int i = 0; i < MAX_MESSAGE_BIT_SIZE; i++){ | |
digitalWrite(13, onOrOff[encodedMessage[i]]); | |
delay( DELAY ); | |
} | |
delay(DELAY*24); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
updated sketch that also decodes the messages now