From 2749e7c39c0cdf28cb9ab83e8fc7a1ed663a91d5 Mon Sep 17 00:00:00 2001 From: mattibal Date: Wed, 6 Mar 2013 18:45:39 +0100 Subject: [PATCH] written a more complex test and example code --- .project | 11 +++++ MeshNetBase/.classpath | 6 ++- .../src/com/mattibal/meshnet/Device.java | 1 - .../src/com/mattibal/meshnet/MeshNetTest.java | 2 +- arduino lib/MeshNet/MeshNet.cpp | 2 +- .../MeshNet_Serial/MeshNet_Serial.ino | 48 ++++++++++++++----- .../MeshNet_Serial_RF24.ino | 37 ++++++++++++-- 7 files changed, 87 insertions(+), 20 deletions(-) create mode 100644 .project diff --git a/.project b/.project new file mode 100644 index 0000000..e695879 --- /dev/null +++ b/.project @@ -0,0 +1,11 @@ + + + meshnet + + + + + + + + diff --git a/MeshNetBase/.classpath b/MeshNetBase/.classpath index 4bb1afe..bba4bef 100644 --- a/MeshNetBase/.classpath +++ b/MeshNetBase/.classpath @@ -1,6 +1,10 @@ - + + + + + diff --git a/MeshNetBase/src/com/mattibal/meshnet/Device.java b/MeshNetBase/src/com/mattibal/meshnet/Device.java index 52ea82f..326625c 100644 --- a/MeshNetBase/src/com/mattibal/meshnet/Device.java +++ b/MeshNetBase/src/com/mattibal/meshnet/Device.java @@ -2,7 +2,6 @@ import java.nio.ByteBuffer; import java.util.HashMap; -import java.util.HashSet; import com.mattibal.meshnet.devices.LedTestDevice; diff --git a/MeshNetBase/src/com/mattibal/meshnet/MeshNetTest.java b/MeshNetBase/src/com/mattibal/meshnet/MeshNetTest.java index e5e055d..79da0df 100644 --- a/MeshNetBase/src/com/mattibal/meshnet/MeshNetTest.java +++ b/MeshNetBase/src/com/mattibal/meshnet/MeshNetTest.java @@ -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); diff --git a/arduino lib/MeshNet/MeshNet.cpp b/arduino lib/MeshNet/MeshNet.cpp index cbf3613..3d8bff1 100644 --- a/arduino lib/MeshNet/MeshNet.cpp +++ b/arduino lib/MeshNet/MeshNet.cpp @@ -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; diff --git a/arduino sketches/MeshNet_Serial/MeshNet_Serial.ino b/arduino sketches/MeshNet_Serial/MeshNet_Serial.ino index d4237ea..2e20877 100644 --- a/arduino sketches/MeshNet_Serial/MeshNet_Serial.ino +++ b/arduino sketches/MeshNet_Serial/MeshNet_Serial.ino @@ -32,24 +32,55 @@ 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); + } } @@ -57,7 +88,7 @@ void setup(){ Serial.begin(9600); - pinMode(4, OUTPUT); // for the LED + pinMode(13, OUTPUT); // for the LED int rfin; int r=0; @@ -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); } diff --git a/arduino sketches/MeshNet_Serial_RF24/MeshNet_Serial_RF24.ino b/arduino sketches/MeshNet_Serial_RF24/MeshNet_Serial_RF24.ino index 46888ac..ab664f2 100644 --- a/arduino sketches/MeshNet_Serial_RF24/MeshNet_Serial_RF24.ino +++ b/arduino sketches/MeshNet_Serial_RF24/MeshNet_Serial_RF24.ino @@ -32,7 +32,7 @@ const uint32_t deviceType = 123; // DEVICE UNIQUE ID -uint32_t deviceUniqueId = 394932; +uint32_t deviceUniqueId = 384932; /** LAYER 2 DEPENDENT CODE **/ @@ -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)); @@ -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); } } @@ -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;