Skip to content

Commit

Permalink
written a more complex test and example code
Browse files Browse the repository at this point in the history
  • Loading branch information
mattibal committed Mar 6, 2013
1 parent 69fadf3 commit 2749e7c
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 20 deletions.
11 changes: 11 additions & 0 deletions .project
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>meshnet</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>
6 changes: 5 additions & 1 deletion MeshNetBase/.classpath
@@ -1,6 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="src">
<attributes>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="/usr/lib/jni"/>
</attributes>
</classpathentry>
<classpathentry exported="true" kind="lib" path="lib/RXTXcomm.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
Expand Down
1 change: 0 additions & 1 deletion MeshNetBase/src/com/mattibal/meshnet/Device.java
Expand Up @@ -2,7 +2,6 @@

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.HashSet;

import com.mattibal.meshnet.devices.LedTestDevice;

Expand Down
2 changes: 1 addition & 1 deletion MeshNetBase/src/com/mattibal/meshnet/MeshNetTest.java
Expand Up @@ -40,7 +40,7 @@ public void run() {
try {

Layer3Base base = new Layer3Base();
SerialRXTXComm serial = new SerialRXTXComm("/dev/ttyACM0", base);
SerialRXTXComm serial = new SerialRXTXComm("/dev/ttyUSB0", base);
Thread.sleep(4000);
Layer3Base.NetworkSetupThread setup = base.new NetworkSetupThread();
Thread setupThread = new Thread(setup);
Expand Down
2 changes: 1 addition & 1 deletion arduino lib/MeshNet/MeshNet.cpp
Expand Up @@ -316,7 +316,7 @@ typedef struct{
unsigned char data[40]; // TODO lunghezza array messa a caso
} __attribute__((packed)) dataToBaseLayer4;

// Send a layer4 packet to base (the first byte of the message must be the command type)
// Send a layer4 packet to base
void sendCommand(uint8_t command, void* data, uint8_t dataLen){
if(toBaseInterface != -1){
dataToBaseLayer4 message;
Expand Down
48 changes: 36 additions & 12 deletions arduino sketches/MeshNet_Serial/MeshNet_Serial.ino
Expand Up @@ -32,32 +32,63 @@ int sendPacket(unsigned char* message, uint8_t len, uint8_t interface, uint8_t m

/** LAYER 7 CODE */


struct setLedStateRx {
uint8_t ledState;
} __attribute__((packed));

void onSetLedStateRx(struct setLedStateRx* data){
if(data->ledState == 1){
digitalWrite(4, HIGH);
delay(10);
digitalWrite(4, LOW);
digitalWrite(13, HIGH);
//delay(10);
//digitalWrite(4, LOW);
} else {
digitalWrite(13, LOW);
}
}


void onSetLedPwmStateRx(uint8_t level){
analogWrite(3, level);
}

struct sendAnalogReadPacket {
uint16_t analogValue;
} __attribute__((packed));

void sendAnalogRead(int pin){
uint8_t command;
if(pin==A0){
command = 3;
} else {
digitalWrite(4, LOW);
command = 4;
}
struct sendAnalogReadPacket packet;
packet.analogValue = analogRead(pin);
sendCommand(command, (void*) &packet, sizeof(packet));
}

void onCommandReceived(uint8_t command, void* data, uint8_t dataLen){
if(command==1 && dataLen >= sizeof(struct setLedStateRx)){
onSetLedStateRx((struct setLedStateRx*)data);
}
if(command==2 && dataLen >= sizeof(uint8_t)){
onSetLedPwmStateRx(*((uint8_t *) data));
}
if(command==3){
sendAnalogRead(A0);
}
if(command==4){
sendAnalogRead(A1);
}
}


void setup(){

Serial.begin(9600);

pinMode(4, OUTPUT); // for the LED
pinMode(13, OUTPUT); // for the LED

int rfin;
int r=0;
Expand All @@ -75,13 +106,6 @@ void setup(){

void loop(){

// Non chiedermi perchè, ma senza questo non funziona!!
/*while (Serial.available() <= 0) {
Serial.print('A'); // send a capital A
delay(300);
}*/

serialReceive();

delay(500);
}
37 changes: 33 additions & 4 deletions arduino sketches/MeshNet_Serial_RF24/MeshNet_Serial_RF24.ino
Expand Up @@ -32,7 +32,7 @@
const uint32_t deviceType = 123;

// DEVICE UNIQUE ID
uint32_t deviceUniqueId = 394932;
uint32_t deviceUniqueId = 384932;


/** LAYER 2 DEPENDENT CODE **/
Expand Down Expand Up @@ -65,7 +65,7 @@ int sendPacket(unsigned char* message, uint8_t len, uint8_t interface, uint8_t m

/** LAYER 7 CODE */

struct setLedStateRx {
/*struct setLedStateRx {
uint8_t ledState;
} __attribute__((packed));
Expand All @@ -77,11 +77,40 @@ void onSetLedStateRx(struct setLedStateRx* data){
} else {
digitalWrite(4, LOW);
}
}*/

void onSetLedPwmStateRx(uint8_t level){
analogWrite(3, level);
}

struct sendAnalogReadPacket {
uint16_t analogValue;
} __attribute__((packed));

void sendAnalogRead(int pin){
uint8_t command;
if(pin==A0){
command = 3;
} else {
command = 4;
}
struct sendAnalogReadPacket packet;
packet.analogValue = analogRead(pin);
sendCommand(command, (void*) &packet, sizeof(packet));
}

void onCommandReceived(uint8_t command, void* data, uint8_t dataLen){
if(command==1 && dataLen >= sizeof(struct setLedStateRx)){
/*if(command==1 && dataLen >= sizeof(struct setLedStateRx)){
onSetLedStateRx((struct setLedStateRx*)data);
}*/
if(command==2 && dataLen >= sizeof(uint8_t)){
onSetLedPwmStateRx(*((uint8_t *) data));
}
if(command==3){
sendAnalogRead(A0);
}
if(command==4){
sendAnalogRead(A1);
}
}

Expand All @@ -91,7 +120,7 @@ void setup(){

Serial.begin(9600);

pinMode(4, OUTPUT); // for the LED
pinMode(3, OUTPUT); // for the LED

int rfin;
int r=0;
Expand Down

0 comments on commit 2749e7c

Please sign in to comment.