-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from CapibaraZero/develop/0.3.0
Add SubGHZ
- Loading branch information
Showing
34 changed files
with
1,256 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,7 @@ build | |
|
||
# Seeed-Studio libs | ||
lib/PN532 | ||
lib/PN532_I2C | ||
lib/PN532_I2C | ||
|
||
# MacOS useless file | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
#include "SubGHZ.hpp" | ||
#include <Arduino.h> | ||
#include <vector> | ||
#include "../../include/pins.h" | ||
|
||
SX1276 radio = new Module(SX1276_NSS, RADIOLIB_NC, RADIOLIB_NC, SX1276_DIO1); | ||
|
||
// this function is called when a new bit is received | ||
void IRAM_ATTR readBit(void) { | ||
// read the data bit | ||
radio.readBit(SX1276_DIO2); | ||
} | ||
|
||
SubGHZ::SubGHZ(uint8_t sck, uint8_t miso, uint8_t mosi, uint8_t csn, | ||
uint8_t gdo0, uint8_t gdo2) { | ||
if (initialized) return; | ||
int state = radio.beginFSK(); | ||
if (state == RADIOLIB_ERR_NONE) { | ||
Serial.println(F("success!")); | ||
} else { | ||
Serial.print(F("failed, code ")); | ||
Serial.println(state); | ||
} | ||
_gdo0 = gdo0; | ||
_gdo2 = gdo2; | ||
initialized = true; | ||
} | ||
|
||
SubGHZ::~SubGHZ() {} | ||
|
||
void SubGHZ::error_check(const char *operation, int16_t status_code) { | ||
if (status_code != RADIOLIB_ERR_NONE) { | ||
Serial.print(operation); | ||
Serial.print(" : "); | ||
Serial.print(F("failed, code ")); | ||
Serial.println(status_code); | ||
} | ||
} | ||
|
||
void SubGHZ::init_receive() { | ||
if (_modulation == 2) // LoRa doesn't require anything | ||
return; | ||
radio.setCRC(false); | ||
radio.setAFC(true); | ||
radio.setDirectSyncWord(0x555512AD, 0); | ||
radio.setDirectAction(readBit); | ||
radio.receiveDirect(); | ||
} | ||
|
||
void SubGHZ::stop_receive() { radio.clearDio1Action(); } | ||
|
||
void SubGHZ::set_freq_mod(float freq, float bw, float deviation) { | ||
int state = radio.setFrequency(freq); | ||
error_check("Set frequency", state); | ||
if (_modulation != 2) { | ||
state = radio.setRxBandwidth(bw); | ||
error_check("Set bandwidth", state); | ||
state = radio.setFrequencyDeviation(deviation); | ||
error_check("Set deviation", state); | ||
_deviation = deviation; | ||
} | ||
_frequency = freq; | ||
_bw = bw; | ||
} | ||
|
||
SignalStrength SubGHZ::scan_frequency(float freq, float bw, float deviation) { | ||
set_freq_mod(freq, bw, deviation); | ||
return SignalStrength{.rssi = radio.getRSSI(false, false)}; | ||
} | ||
|
||
SignalStrength SubGHZ::scan_frequency() { | ||
return SignalStrength{.rssi = radio.getRSSI(false, false)}; | ||
} | ||
|
||
#define DEFAULT_SAMPLING_RATE 50 | ||
|
||
#define READ_N_BYTE() for (int j = 7; j > -1; j--) | ||
|
||
void read_lora_packet(std::vector<byte> *orig_signal) { | ||
// TODO: Add DIO0 and use it as interrupt | ||
|
||
// you can also receive data as byte array | ||
size_t len = radio.getPacketLength(); | ||
byte byteArr[len]; | ||
int state = radio.receive(byteArr, len); | ||
|
||
if (state == RADIOLIB_ERR_NONE) { | ||
// packet was successfully received | ||
Serial.println(F("Packet arrived!")); | ||
memcpy(orig_signal->data(), byteArr, len); | ||
} else if (state == RADIOLIB_ERR_RX_TIMEOUT) { | ||
// timeout occurred while waiting for a packet | ||
Serial.println(F("timeout!")); | ||
|
||
} else if (state == RADIOLIB_ERR_CRC_MISMATCH) { | ||
// packet was received, but is malformed | ||
Serial.println(F("CRC error!")); | ||
|
||
} else { | ||
// some other error occurred | ||
Serial.print(F("failed, code ")); | ||
Serial.println(state); | ||
} | ||
} | ||
|
||
void SubGHZ::rec_signal(std::vector<byte> *orig_signal) { | ||
if (_modulation == 2) { // LoRa modulation | ||
while (true) { | ||
read_lora_packet(orig_signal); | ||
} | ||
} | ||
if (radio.available()) { | ||
Serial.println("[SX1276] Received packet in direct mode!"); | ||
while (radio.available()) { | ||
// read a byte | ||
byte signal = radio.read(); | ||
orig_signal->push_back(signal); | ||
} | ||
} | ||
} | ||
|
||
void SubGHZ::send_signal(float freq, Modulation modulation, byte *payload, | ||
size_t length) { | ||
set_modulation(modulation.mode); | ||
set_freq_mod(freq, modulation.bandwidth, modulation.deviation); | ||
int state = radio.transmit(payload, length); | ||
if (state == RADIOLIB_ERR_NONE) { | ||
Serial.println(F("Transimt successfully!")); | ||
|
||
} else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) { | ||
// the supplied packet was longer than 256 bytes | ||
Serial.println(F("too long!")); | ||
} | ||
else { | ||
// some other error occurred | ||
Serial.print(F("failed, code ")); | ||
Serial.println(state); | ||
} | ||
}; | ||
|
||
void SubGHZ::set_modulation(int mod) { | ||
int state = RADIOLIB_ERR_NONE; | ||
/* Module was on LoRA*/ | ||
if (_modulation == 2 && mod != 2) { | ||
state = radio.beginFSK(); | ||
error_check("Begin FSK", state); | ||
} | ||
|
||
_modulation = mod; | ||
|
||
if (!mod) | ||
state = radio.setOOK(true); | ||
else if (mod == 1) | ||
state = radio.setOOK(false); | ||
else { | ||
state = radio.begin(); | ||
error_check("Begin LoRa", state); | ||
return; | ||
} | ||
error_check("Set modulation", state); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#ifndef SUBGHZ_LIBRARY_H | ||
#define SUBGHZ_LIBRARY_H | ||
|
||
#include <Arduino.h> | ||
#include <vector> | ||
#include <RadioLib.h> | ||
|
||
// enum ModulationMode { ASK, FSK }; | ||
|
||
typedef struct Modulation { | ||
int mode; | ||
float deviation; | ||
float bandwidth; | ||
} Modulation; | ||
|
||
typedef struct Signal { | ||
int modulation; | ||
float frequency; | ||
uint8_t *payload; | ||
} Signal; | ||
|
||
typedef struct SignalStrength { | ||
float rssi; | ||
}SignalStrength; | ||
|
||
class SubGHZ { | ||
private: | ||
byte jammer_payload[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}; | ||
int _gdo0 = -1; | ||
int _gdo2 = -1; | ||
float _frequency; | ||
float _bw; | ||
float _deviation; | ||
int _modulation; | ||
bool initialized = false; | ||
void error_check(const char *operation, int16_t status_code); | ||
public: | ||
SubGHZ(uint8_t sck, uint8_t miso, uint8_t mosi, uint8_t csn, uint8_t gdo0, | ||
uint8_t gdo2); | ||
~SubGHZ(); | ||
// Getter functions | ||
float get_frequency() { return _frequency; }; | ||
float get_bw() { return _bw; }; | ||
float get_deviation() { return _deviation; }; | ||
int get_modulation() { return _modulation; }; | ||
void init_receive(); | ||
void stop_receive(); | ||
void set_freq_mod(float freq, float bw, float deviation); | ||
SignalStrength scan_frequency(float freq, float bw = 200.0F, | ||
float deviation = 47.0F); | ||
SignalStrength scan_frequency(); | ||
void rec_signal(std::vector<byte> *orig_signal); | ||
void send_signal(float freq, Modulation modulation, byte *payload, size_t length); | ||
/// @brief Set modulation to ASK or FSK | ||
/// @param mod 0 for ASK, 1 for FSK and 2 for LoRA | ||
void set_modulation(int mod); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.