From 3fcd4470a418fd2a425d4362935329e9fa9b34aa Mon Sep 17 00:00:00 2001 From: Audrey 'Yo Date: Sun, 7 Sep 2014 01:11:27 -0400 Subject: [PATCH] closes #1 closes #2 This commit adds the basic protocol for send and receive in the code. The file is in arduino_src > code > Protocol Prototype. Send and receive uses 1 Serial line. The receive looks for a header 0xFC. Code still needs to be cleaned up Further testing is still needed. --- arduino_src/code/Protocol_prototype/IO_pad.c | 48 +++++++++++ arduino_src/code/Protocol_prototype/IO_pad.h | 29 +++++++ .../code/Protocol_prototype/IO_types.h | 30 +++++++ .../Protocol_prototype/Protocol_prototype.ino | 48 +++++++++++ .../code/Protocol_prototype/issues.txt | 6 ++ .../code/Protocol_prototype/lib_protocol.c | 84 +++++++++++++++++++ .../code/Protocol_prototype/lib_protocol.h | 43 ++++++++++ .../code/Protocol_prototype/lib_types.h | 38 +++++++++ 8 files changed, 326 insertions(+) create mode 100755 arduino_src/code/Protocol_prototype/IO_pad.c create mode 100755 arduino_src/code/Protocol_prototype/IO_pad.h create mode 100755 arduino_src/code/Protocol_prototype/IO_types.h create mode 100755 arduino_src/code/Protocol_prototype/Protocol_prototype.ino create mode 100755 arduino_src/code/Protocol_prototype/issues.txt create mode 100755 arduino_src/code/Protocol_prototype/lib_protocol.c create mode 100755 arduino_src/code/Protocol_prototype/lib_protocol.h create mode 100755 arduino_src/code/Protocol_prototype/lib_types.h diff --git a/arduino_src/code/Protocol_prototype/IO_pad.c b/arduino_src/code/Protocol_prototype/IO_pad.c new file mode 100755 index 00000000..a216d9de --- /dev/null +++ b/arduino_src/code/Protocol_prototype/IO_pad.c @@ -0,0 +1,48 @@ +/******************************************************************************** + * All rights are open to the public, to use, share, dump, spread for the * + * foreseeable future. This software and documentation constitutes purely * + * published and public work, and contains open source knowledge by a bunch * + * of college kids who just want to have fun. All the material and code may be * + * used, copied, duplicated, changed, disclosed... with any human's free will. * + * Have a nice day! :) * + ********************************************************************************/ + +#include +#include "IO_pad.h" +#include "lib_types.h" +/********************************************************************************* + * L O C A L P R O T O T Y P E S * + ********************************************************************************/ + + + + + +/********************************************************************************* + * G L O B A L F U N C T I O N S * + ********************************************************************************/ +Ret_E IO_set_pad( byte pad, byte value ) +{ + Ret_E success = RET_ERROR; + analogWrite(pad, value); + success = RET_OK; + return success; +} + + +Ret_E IO_set_state( byte pad, IO_state_e state) +{ + Ret_E success = RET_ERROR; + digitalWrite(pad, state); + success = RET_OK; + return success; +} + + +/********************************************************************************* + * L O C A L F U N C T I O N S * + ********************************************************************************/ + + + + diff --git a/arduino_src/code/Protocol_prototype/IO_pad.h b/arduino_src/code/Protocol_prototype/IO_pad.h new file mode 100755 index 00000000..2c901e93 --- /dev/null +++ b/arduino_src/code/Protocol_prototype/IO_pad.h @@ -0,0 +1,29 @@ +/******************************************************************************** + * All rights are open to the public, to use, share, dump, spread for the * + * foreseeable future. This software and documentation constitutes purely * + * published and public work, and contains open source knowledge by a bunch * + * of college kids who just want to have fun. All the material and code may be * + * used, copied, duplicated, changed, disclosed... with any human's free will. * + * Have a nice day! :) * + ********************************************************************************/ + + + +#ifndef IO_PAD_H_ +#define IO_PAD_H_ +#include +#include "IO_types.h" +#include "lib_types.h" + +#ifdef __cplusplus +extern "C"{ +#endif + + +Ret_E IO_set_pad( byte pad, byte value); +Ret_E IO_set_state( byte pad, IO_state_e state ); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/arduino_src/code/Protocol_prototype/IO_types.h b/arduino_src/code/Protocol_prototype/IO_types.h new file mode 100755 index 00000000..66bdaf3a --- /dev/null +++ b/arduino_src/code/Protocol_prototype/IO_types.h @@ -0,0 +1,30 @@ +/******************************************************************************** + * All rights are open to the public, to use, share, dump, spread for the * + * foreseeable future. This software and documentation constitutes purely * + * published and public work, and contains open source knowledge by a bunch * + * of college kids who just want to have fun. All the material and code may be * + * used, copied, duplicated, changed, disclosed... with any human's free will. * + * Have a nice day! :) * + ********************************************************************************/ + +#ifndef IO_TYPES_H_ +#define IO_TYPES_H_ + +#include + +#ifdef __cplusplus +extern "C"{ +#endif + +typedef enum +{ + low, + high +}IO_state_e; + + + +#ifdef __cplusplus +} // extern "C" +#endif +#endif diff --git a/arduino_src/code/Protocol_prototype/Protocol_prototype.ino b/arduino_src/code/Protocol_prototype/Protocol_prototype.ino new file mode 100755 index 00000000..b266dfcc --- /dev/null +++ b/arduino_src/code/Protocol_prototype/Protocol_prototype.ino @@ -0,0 +1,48 @@ +#include "IO_pad.h" +#include "lib_protocol.h" +#include "lib_types.h" + +void setup() { + Serial.begin(9600); +} + +void loop() { + // This is the tester + // It will continuously send a packet to the computer side + // Arduino -> Computer + byte index = 0x01; + unsigned int message = 0xDEAD; + unsigned long packet = protocol_send(index, message); + + // TODO: should have a safe fault eg. packet != NULL +// Serial.print("The packet is: "); +// Serial.println(packet,HEX); + + + /************** RECEIVE PACKET ********************/ + // if Serial is available read if there are any incoming messages + if (Serial.available()) { + // read the incoming byte: + byte incomingByte = Serial.read(); + + // say what you got: + Serial.print("I received: "); + Serial.println(incomingByte, HEX); + + if(incomingByte == HEAD) + { + unsigned long pckt = incomingByte; + for(int i = 0; i < 3; i++) + { + pckt = pckt << ONE_BYTE_SIZE; + incomingByte = Serial.read(); + pckt = pckt | incomingByte; + } + Serial.print("I received a packet! It is: "); + Serial.println(pckt, HEX); + } + // parse the byte? + + } + delay(500); +} diff --git a/arduino_src/code/Protocol_prototype/issues.txt b/arduino_src/code/Protocol_prototype/issues.txt new file mode 100755 index 00000000..8648f465 --- /dev/null +++ b/arduino_src/code/Protocol_prototype/issues.txt @@ -0,0 +1,6 @@ +## Audrey Issues/ Questions: +- The Arduino Serial.write, will it just work if I include Arduino.h? +- C++ extern header needed? +- Receiving Protocol. Similar? When is it fired? +- Init protocol -> What is expected? +- Receiving on interrupt? Serial??? hmmm \ No newline at end of file diff --git a/arduino_src/code/Protocol_prototype/lib_protocol.c b/arduino_src/code/Protocol_prototype/lib_protocol.c new file mode 100755 index 00000000..05c7a5f4 --- /dev/null +++ b/arduino_src/code/Protocol_prototype/lib_protocol.c @@ -0,0 +1,84 @@ +/******************************************************************************** + * All rights are open to the public, to use, share, dump, spread for the * + * foreseeable future. This software and documentation constitutes purely * + * published and public work, and contains open source knowledge by a bunch * + * of college kids who just want to have fun. All the material and code may be * + * used, copied, duplicated, changed, disclosed... with any human's free will. * + * Have a nice day! :) * + ********************************************************************************/ + +/* @author: Audrey Yeoh (ayeohmy@gmail.com) + * @date: 7/22/2014 + */ + +#include +#include +#include +#include +#include +#include "lib_types.h" + + +/******************************************************************************** +* D E F I N I T I O N S +********************************************************************************/ + + + +/******************************************************************************** +* L O C A L P R O T O T Y P E S +********************************************************************************/ +//static unsigned long create_msg( byte id, unsigned int msg); +static Ret_E send_msg( unsigned long message ); // TODO: need to implement actual sending of message + +/******************************************************************************** +* G L O B A L F U N C T I O N S +********************************************************************************/ +/* This will set up the head, and append all the messages ready to be sent and + * prepare the message + * And then send the message with a return state being ok if successful + */ +unsigned long protocol_send( byte id, unsigned int msg ) +{ +// Ret_E success = RET_ERROR; + // create the message +// unsigned long message = create_msg(id, msg); + + unsigned long message = HEAD; + message = message << ONE_BYTE_SIZE; + message = message | id; + message = message << TWO_BYTE_SIZE; + message = message | msg; + + // if more messages, append? << What do? + + // send the message + //success = send_msg( message ); + // return state + + return message; + +} + + +/******************************************************************************** +* L O C A L F U N C T I O N S +********************************************************************************/ + +static unsigned long create_msg( byte id, unsigned int msg) +{ + unsigned long message = HEAD; + message = message << ONE_BYTE_SIZE; + message = message | id; + message = message << TWO_BYTE_SIZE; + message = message | msg; + return message; +} + +static Ret_E send_msg( unsigned long message ) +{ + Ret_E success = RET_ERROR; + // TODO!!!!! Send message + + return success; +} diff --git a/arduino_src/code/Protocol_prototype/lib_protocol.h b/arduino_src/code/Protocol_prototype/lib_protocol.h new file mode 100755 index 00000000..b1304e6e --- /dev/null +++ b/arduino_src/code/Protocol_prototype/lib_protocol.h @@ -0,0 +1,43 @@ +/******************************************************************************** + * All rights are open to the public, to use, share, dump, spread for the * + * foreseeable future. This software and documentation constitutes purely * + * published and public work, and contains open source knowledge by a bunch * + * of college kids who just want to have fun. All the material and code may be * + * used, copied, duplicated, changed, disclosed... with any human's free will. * + * Have a nice day! :) * + ********************************************************************************/ + +/* @author: Audrey Yeoh (ayeohmy@gmail.com) + * @date: 7/22/2014 + */ + + + +#ifndef LIB_PROTOCOL_H +#define LIB_PROTOCOL_H + +#include + +#ifdef __cplusplus +extern "C"{ +#endif + +#include "lib_types.h" + + +/* This will be the main structure of our packets: + * + * Head [0xFC] ID [ - ] Message[ - -] ... ID [ - ] Message[ - -] + * [ 1 byte ] [1 byte] [ 2 bytes ] ... [1 byte] [ 2 bytes ] + */ + +Ret_E protocol_init( void ); // TODO: I'm not sure exactly what needs to be initialized yet +Ret_E protocol_run( void ); // TODO: +unsigned long protocol_send( byte id, unsigned int message); // Currently working on +unsigned long protocol_getMessage(unsigned long packet); +unsigned long protocol_getID(unsigned long packet); + +#ifdef __cplusplus +} +#endif +#endif // LIB_PROTOCOL_H diff --git a/arduino_src/code/Protocol_prototype/lib_types.h b/arduino_src/code/Protocol_prototype/lib_types.h new file mode 100755 index 00000000..89ecf4ef --- /dev/null +++ b/arduino_src/code/Protocol_prototype/lib_types.h @@ -0,0 +1,38 @@ +/******************************************************************************** + * All rights are open to the public, to use, share, dump, spread for the * + * foreseeable future. This software and documentation constitutes purely * + * published and public work, and contains open source knowledge by a bunch * + * of college kids who just want to have fun. All the material and code may be * + * used, copied, duplicated, changed, disclosed... with any human's free will. * + * Have a nice day! :) * + ********************************************************************************/ + +/* @author: Audrey Yeoh (ayeohmy@gmail.com) + * @date: 7/22/2014 + */ + +#ifndef LIB_TYPES_H +#define LIB_TYPES_H +#ifdef __cplusplus +extern "C"{ +#endif + +#include +#include + +#define HEAD 0xFC +#define ONE_BYTE_SIZE 8 +#define TWO_BYTE_SIZE 16 + +typedef enum +{ + RET_OK, + RET_ERROR +}Ret_E; + + +#ifdef __cplusplus +} +#endif + +#endif // LIB_TYPES_H