Skip to content

Commit

Permalink
Release Version 4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
WL-Richards committed Feb 14, 2023
1 parent d1dec19 commit cf58db6
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 2 deletions.
30 changes: 30 additions & 0 deletions auxilary/package_loom4_index.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,36 @@
}
],
"tools":[]
},
{
"name": "loom4",
"maintainer": "OPEnS Lab",
"websiteURL": "https://github.com/OPEnSLab-OSU/Loom-V4",
"email": "[email protected]",
"help":{
"online": "https://github.com/OPEnSLab-OSU/Loom-V4/issues"
},
"platforms": [
{
"name": "Loom SAMD Boards V4",
"architecture": "samd",
"version": "4.2",
"category": "contributed",
"help": {
"online": "https://github.com/OPEnSLab-OSU/Loom-V4/issues"
},
"url": "https://github.com/OPEnSLab-OSU/Loom-V4/releases/download/v4.2/loom4.zip",
"archiveFileName": "loom4.zip",
"checksum": "SHA-256:92851e0410940f9d9413dd7c940def4317efcfdbdbcd304fd3d02fbbb6839cf9",
"size": "21346647",
"boards": [
{
"name": "Loomified Adafruit Feather M0 (SAMD21)"
}
]
}
],
"tools":[]
}
]
}
143 changes: 143 additions & 0 deletions src/Radio/Loom_Freewave/Loom_Freewave.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#include "Loom_Freewave.h"

//////////////////////////////////////////////////////////////////////////////////////////////////////
Loom_Freewave::Loom_Freewave(
Manager& man,
const uint8_t address,
const uint16_t max_message_len,
const uint8_t retryCount,
const uint16_t retryTimeout
) : Radio("Freewave"), manInst(&man), serial1(Serial1), driver(serial1)
{
if(address == -1)
this->deviceAddress = manInst->get_instance_num();
else
this->deviceAddress = address;

manager = new RHReliableDatagram(driver, this->deviceAddress);
this->retryCount = retryCount;
this->retryTimeout = retryTimeout;
this->maxMessageLength = max_message_len;
manInst->registerModule(this);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////
void Loom_Freewave::initialize(){

// Start serial communication with radio
serial1.begin(115200);

// Set timeout time
printModuleName("Timeout time set to: " + String(retryTimeout));
manager->setTimeout(retryTimeout);

// Set retry attempts
printModuleName("Retry count set to: " + String(retryCount));
manager->setRetries(retryCount);

// Initialize the radio manager
if(manager->init()){
printModuleName("Radio manager successfully initialized!");
}
else{
printModuleName("Radio manager failed to initialize!");
moduleInitialized = false;
return;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////
void Loom_Freewave::package(){
if(moduleInitialized){
JsonObject json = manInst->get_data_object(getModuleName());
json["RSSI"] = getSignalStrength();
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////
void Loom_Freewave::setAddress(uint8_t addr){
deviceAddress = addr;
manager->setThisAddress(addr);
driver.sleep();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////
bool Loom_Freewave::receive(uint maxWaitTime){
bool recvStatus = false;
uint8_t fromAddress;


// Write all null bytes to the buffer
uint8_t buffer[maxMessageLength];
uint8_t len = sizeof(buffer);

printModuleName("Waiting for packet...");

// Non-blocking receive if time is set to 0
if(maxWaitTime == 0){
recvStatus = manager->recvfromAck(buffer, &len, &fromAddress);
}
else{
recvStatus = manager->recvfromAckTimeout(buffer, &len, maxWaitTime, &fromAddress);
}

// If a packet was received
if(recvStatus){
printModuleName("Packet Received!");
signalStrength = driver.lastRssi();
recvStatus = bufferToJson(buffer);
recvData = "";
serializeJson(recvDoc, recvData);
deserializeJson(manInst->getDocument(), recvData);

// Update device name
manInst->set_device_name(manInst->getDocument()["id"]["name"].as<String>());
manInst->set_instance_num(manInst->getDocument()["id"]["instance"].as<int>());

}
else{
printModuleName("No Packet Received");
}

driver.sleep();
return recvStatus;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////
bool Loom_Freewave::send(const uint8_t destinationAddress){
uint8_t buffer[maxMessageLength];

// Try to write the JSON to the buffer
if(!jsonToBuffer(buffer, manInst->getDocument().as<JsonObject>())){
printModuleName("Failed to convert JSON to MsgPack");
return false;
}

if(!manager->sendtoWait((uint8_t*)buffer, sizeof(buffer), destinationAddress)){
printModuleName("Failed to send packet to specified address!");
return false;
}

printModuleName("Successfully transmit packet!");
signalStrength = driver.lastRssi();
driver.sleep();
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////
void Loom_Freewave::power_up(){
driver.available();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////
void Loom_Freewave::power_down(){
driver.sleep();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
93 changes: 93 additions & 0 deletions src/Radio/Loom_Freewave/Loom_Freewave.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#pragma once


#include "../Radio.h"
#include "../../Loom_Manager.h"

#include <HardwareSerial.h>
#include <RH_Serial.h>
#include <RHReliableDatagram.h>

/**
* Used to communicate with LoRa type radios
*
* @author Will Richards
*/
class Loom_Freewave : public Radio{
protected:
/* These aren't used with this module */
void measure() override {};
void print_measurements() override {};


public:

/**
* Construct a new LoRa driver
* @param man Reference to the manager
* @param max_message_len The maximum possible message length we can transmit
* @param address This device's LoRa address
* @param powerLevel Transmission power level, low to high
* @param retryCount Number of attempts to make before failing
* @param retryTimeout Length of time between retransmissions (ms)
*/
Loom_Freewave(
Manager& man,
const uint8_t address = -1,
const uint16_t max_message_len = RH_SERIAL_MAX_MESSAGE_LEN,
const uint8_t retryCount = 3,
const uint16_t retryTimeout = 200
);

~Loom_Freewave(){
delete manager;
}

/**
* Receive a JSON packet from another radio, blocking until the wait time expires or a packet is received
* @param maxWaitTime The maximum time to wait before continuing execution (Set to 0 for non-blocking)
*/
bool receive(uint maxWaitTime) override;

/**
* Send the current JSON data to the specified address
* @param destinationAddress The address we want to send the data to
*/
bool send(const uint8_t destinationAddress) override;

/**
* Initialize the module
*/
void initialize() override;

/**
* Package basic data about the device
*/
void package() override;

/**
* Power up the module
*/
void power_up() override;

/**
* Power down the module
*/
void power_down() override;

/**
* Set the address of the device
*/
void setAddress(const uint8_t addr);

private:
Manager* manInst; // Instance of the manager

String recvData = "";

HardwareSerial& serial1; // Serial reference
RH_Serial driver; // Freewave Driver
RHReliableDatagram* manager; // Manager for driver


};
4 changes: 2 additions & 2 deletions src/Radio/Radio.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Radio : public Module{
*/
bool bufferToJson(uint8_t* buffer){

DeserializationError error = deserializeMsgPack(recvDoc, buffer, 251);
DeserializationError error = deserializeMsgPack(recvDoc, buffer, maxMessageLength);

// Check if an error occurred
if(error != DeserializationError::Ok){
Expand All @@ -66,7 +66,7 @@ class Radio : public Module{
* Convert the json to a message pack
*/
bool jsonToBuffer(uint8_t* buffer, JsonObjectConst json){
bool status = serializeMsgPack(json, buffer, 251);
bool status = serializeMsgPack(json, buffer, maxMessageLength);

return status;
};
Expand Down

0 comments on commit cf58db6

Please sign in to comment.