Skip to content

Commit aa55979

Browse files
author
Akito van Troyer
committed
Initial Commit
Test if the transfer protocol between java and arduino works with led blinking as well as other variable transfers.
0 parents commit aa55979

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1853
-0
lines changed

.classpath

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
5+
<classpathentry kind="lib" path="lib/jar/jssc.jar"/>
6+
<classpathentry kind="lib" path="lib/jar/lombok.jar"/>
7+
<classpathentry kind="lib" path="lib/jar/commons-lang3-3.3.2.jar"/>
8+
<classpathentry kind="output" path="bin"/>
9+
</classpath>

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>EasyTransfer-Java</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.6
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.6
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include "EasyTransfer.h"
2+
3+
//Captures address and size of struct
4+
void EasyTransfer::begin(uint8_t * ptr, uint8_t length, Stream *theStream){
5+
address = ptr;
6+
size = length;
7+
_stream = theStream;
8+
9+
//dynamic creation of rx parsing buffer in RAM
10+
rx_buffer = (uint8_t*) malloc(size);
11+
}
12+
13+
//Sends out struct in binary, with header, length info and checksum
14+
void EasyTransfer::sendData(){
15+
uint8_t CS = size;
16+
_stream->write(0x06);
17+
_stream->write(0x15);
18+
_stream->write(size);
19+
for(int i = 0; i<size; i++){
20+
CS^=*(address+i);
21+
_stream->write(*(address+i));
22+
}
23+
_stream->write(CS);
24+
}
25+
26+
boolean EasyTransfer::receiveData(){
27+
28+
//start off by looking for the header bytes. If they were already found in a previous call, skip it.
29+
if(rx_len == 0){
30+
//this size check may be redundant due to the size check below, but for now I'll leave it the way it is.
31+
if(_stream->available() >= 3){
32+
//this will block until a 0x06 is found or buffer size becomes less then 3.
33+
while(_stream->read() != 0x06) {
34+
//This will trash any preamble junk in the serial buffer
35+
//but we need to make sure there is enough in the buffer to process while we trash the rest
36+
//if the buffer becomes too empty, we will escape and try again on the next call
37+
if(_stream->available() < 3)
38+
return false;
39+
}
40+
if (_stream->read() == 0x15){
41+
rx_len = _stream->read();
42+
//make sure the binary structs on both Arduinos are the same size.
43+
if(rx_len != size){
44+
rx_len = 0;
45+
return false;
46+
}
47+
}
48+
}
49+
}
50+
51+
//we get here if we already found the header bytes, the struct size matched what we know, and now we are byte aligned.
52+
if(rx_len != 0){
53+
while(_stream->available() && rx_array_inx <= rx_len){
54+
rx_buffer[rx_array_inx++] = _stream->read();
55+
}
56+
57+
if(rx_len == (rx_array_inx-1)){
58+
//seem to have got whole message
59+
//last uint8_t is CS
60+
calc_CS = rx_len;
61+
for (int i = 0; i<rx_len; i++){
62+
calc_CS^=rx_buffer[i];
63+
}
64+
65+
if(calc_CS == rx_buffer[rx_array_inx-1]){//CS good
66+
memcpy(address,rx_buffer,size);
67+
rx_len = 0;
68+
rx_array_inx = 0;
69+
return true;
70+
}
71+
72+
else{
73+
//failed checksum, need to clear this out anyway
74+
rx_len = 0;
75+
rx_array_inx = 0;
76+
return false;
77+
}
78+
79+
}
80+
}
81+
82+
return false;
83+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/******************************************************************
2+
* EasyTransfer Arduino Library
3+
* details and example sketch:
4+
* http://www.billporter.info/easytransfer-arduino-library/
5+
*
6+
* Brought to you by:
7+
* Bill Porter
8+
* www.billporter.info
9+
*
10+
* See Readme for other info and version history
11+
*
12+
*
13+
*This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or(at your option) any later version.
14+
This program is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License for more details.
18+
<http://www.gnu.org/licenses/>
19+
*
20+
*This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
21+
*To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or
22+
*send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
23+
******************************************************************/
24+
#ifndef EasyTransfer_h
25+
#define EasyTransfer_h
26+
27+
28+
//make it a little prettier on the front end.
29+
#define details(name) (byte*)&name,sizeof(name)
30+
31+
//Not neccessary, but just in case.
32+
#if ARDUINO > 22
33+
#include "Arduino.h"
34+
#else
35+
#include "WProgram.h"
36+
#endif
37+
#include "Stream.h"
38+
//#include <NewSoftSerial.h>
39+
#include <math.h>
40+
#include <stdio.h>
41+
#include <stdint.h>
42+
#include <avr/io.h>
43+
44+
class EasyTransfer {
45+
public:
46+
void begin(uint8_t *, uint8_t, Stream *theStream);
47+
//void begin(uint8_t *, uint8_t, NewSoftSerial *theSerial);
48+
void sendData();
49+
boolean receiveData();
50+
private:
51+
Stream *_stream;
52+
//NewSoftSerial *_serial;
53+
uint8_t * address; //address of struct
54+
uint8_t size; //size of struct
55+
uint8_t * rx_buffer; //address for temporary storage and parsing buffer
56+
uint8_t rx_array_inx; //index for RX parsing buffer
57+
uint8_t rx_len;//RX packet length according to the packet
58+
uint8_t calc_CS; //calculated Chacksum
59+
};
60+
61+
62+
63+
#endif
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "SerialEventHandler.h"
2+
3+
SerialEventHandler serialEventHandler;
4+
SerialSendData data;
5+
6+
void setup(){
7+
serialEventHandler.setup();
8+
pinMode(13,OUTPUT);
9+
digitalWrite(13,LOW);
10+
}
11+
12+
void loop(){
13+
serialEventHandler.update();
14+
handleIncomingSerial();
15+
16+
//XXX Send Example
17+
/*
18+
data.b = 10;
19+
data.s = 100;
20+
data.i = 1000;
21+
data.l = 10000;
22+
data.c = 'j';
23+
data.bo = true;
24+
serialEventHandler.event(data);
25+
*/
26+
}
27+
28+
void handleIncomingSerial(){
29+
30+
static SerialReceiveData receiveData;
31+
32+
if(serialEventHandler.getIncomingEvent(&receiveData)){
33+
//XXX Receive Example
34+
35+
SerialSendData sendData;
36+
sendData.b = receiveData.b;
37+
sendData.s = receiveData.s;
38+
sendData.i = receiveData.i;
39+
sendData.l = receiveData.l;
40+
sendData.c = receiveData.c;
41+
sendData.bo = receiveData.bo;
42+
serialEventHandler.event(sendData);
43+
44+
}
45+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#ifndef EVENTQUEUE_H
2+
#define EVENTQUEUE_H
3+
4+
#include "PeripheralsAndProtocols.h"
5+
6+
template<typename T>
7+
class EventQueue {
8+
public:
9+
static const int EVQUEUE_SIZE = 50;
10+
EventQueue():queueID(0){init();};
11+
boolean isEmpty() ;
12+
boolean isFull() ;
13+
int getNumEvents() ;
14+
boolean enqueueEvent(T data) ;
15+
boolean dequeueEvent(T *data) ;
16+
boolean isLocked() ;
17+
void setQueueID(byte id) ;
18+
byte getQueueID() ;
19+
20+
private:
21+
T eventQueue[EVQUEUE_SIZE];
22+
int eventQueueHead;
23+
int eventQueueTail;
24+
int numEvents;
25+
boolean lock;
26+
byte queueID;
27+
void init();
28+
};
29+
30+
template<typename T>
31+
inline boolean EventQueue<T>::isLocked() {
32+
return this->lock;
33+
}
34+
35+
template<typename T>
36+
inline void EventQueue<T>::init() {
37+
int i;
38+
39+
this->eventQueueHead = 0;
40+
this->eventQueueTail = EVQUEUE_SIZE - 1;
41+
this->numEvents = 0;
42+
43+
// for (i = 0; i < EVQUEUE_SIZE; i++) {
44+
// eventQueue[i] = sizeof(T);
45+
// }
46+
47+
this->lock = false;
48+
}
49+
50+
template<typename T>
51+
inline boolean EventQueue<T>::isEmpty() {
52+
return (this->numEvents == 0);
53+
}
54+
55+
template<typename T>
56+
inline boolean EventQueue<T>::isFull() {
57+
return (this->eventQueueHead == this->eventQueueTail);
58+
}
59+
60+
template<typename T>
61+
inline int EventQueue<T>::getNumEvents() {
62+
return this->numEvents;
63+
}
64+
65+
template<typename T>
66+
inline boolean EventQueue<T>::enqueueEvent(T data) {
67+
this->lock = true;
68+
if (this->isFull()) {
69+
// log the queue full error
70+
#ifdef kDebug
71+
Serial.println("Queue full");
72+
#endif
73+
return false;
74+
}
75+
76+
// store the event
77+
this->eventQueue[this->eventQueueHead] = data;
78+
79+
// update queue head value
80+
this->eventQueueHead = (this->eventQueueHead + 1) % EVQUEUE_SIZE;
81+
82+
// update number of events in queue
83+
this->numEvents++;
84+
85+
this->lock = false;
86+
return true;
87+
}
88+
89+
template<typename T>
90+
inline boolean EventQueue<T>::dequeueEvent(T *data) {
91+
this->lock = true;
92+
93+
if (this->numEvents == 0) {
94+
return false;
95+
}
96+
97+
this->eventQueueTail = (this->eventQueueTail + 1) % EVQUEUE_SIZE;
98+
99+
// store event code and event parameter
100+
// into the user-supplied variables
101+
*data = this->eventQueue[this->eventQueueTail];
102+
103+
// update number of events in queue
104+
this->numEvents--;
105+
106+
this->lock = false;
107+
return true;
108+
}
109+
110+
template<typename T>
111+
void EventQueue<T>::setQueueID(byte id) {
112+
this->queueID = id;
113+
}
114+
115+
template<typename T>
116+
byte EventQueue<T>::getQueueID() {
117+
return this->queueID;
118+
}
119+
#endif //EVENTQUEUE_H
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifndef PERIPHERALS_H
2+
#define PERIPHERALS_H
3+
//--Treat this file as a config file
4+
#include "Arduino.h"
5+
//#define kDebug 1 //Uncomment this for debugging stuff
6+
7+
#define kSDA 20
8+
#define kSDL 21
9+
10+
//--Serial setup
11+
#define kSerialRate 9600
12+
13+
//Define data structures for the transfer
14+
typedef struct SerialSendData {
15+
int8_t b;
16+
int16_t s;
17+
int32_t i;
18+
int64_t l;
19+
uint16_t c;
20+
uint8_t bo;
21+
} SerialSendData;
22+
23+
typedef struct SerialReceiveData {
24+
int8_t b;
25+
int16_t s;
26+
int32_t i;
27+
int64_t l;
28+
uint16_t c;
29+
uint8_t bo;
30+
} SerialReceiveData;
31+
32+
//--Low Level AVR access------------------------------------
33+
#define cbi(port,pin) port &= ~_BV(pin)
34+
#define sbi(port,pin) port |= _BV(pin)
35+
#define toggle(port,pin) port ^= _BV(pin)
36+
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
37+
#define pin_test(pins,pin) (pins & (1<<pin))
38+
39+
#endif //PERIPHERALS_H

0 commit comments

Comments
 (0)