reduced size with config
This commit is contained in:
98
collar.cpp
98
collar.cpp
@@ -28,12 +28,10 @@ void os_getDevEui (u1_t* buf) { }
|
||||
void os_getDevKey (u1_t* buf) { }
|
||||
|
||||
static osjob_t sendjob;
|
||||
static osjob_t gpsjob;
|
||||
|
||||
// Schedule TX every this many seconds (might become longer due to duty
|
||||
// cycle limitations).
|
||||
const unsigned TX_INTERVAL = 10;
|
||||
const unsigned GPS_INTERVAL = 1;
|
||||
|
||||
// Pin mapping for Adafruit Feather M0 LoRa
|
||||
const lmic_pinmap lmic_pins = {
|
||||
@@ -50,6 +48,11 @@ const lmic_pinmap lmic_pins = {
|
||||
***************************************************/
|
||||
TinyGPSPlus gps;
|
||||
|
||||
|
||||
uint8_t general_int;
|
||||
#define isr general_int
|
||||
#define timeout general_int
|
||||
#define n_poly general_int
|
||||
/****************************************************
|
||||
* Track each pair of X and Y coordinates
|
||||
* - arrays are used by the pnpoly function
|
||||
@@ -57,28 +60,7 @@ TinyGPSPlus gps;
|
||||
const uint8_t N_POLY_MAX=10;
|
||||
float polyx[N_POLY_MAX];
|
||||
float polyy[N_POLY_MAX];
|
||||
int n_poly=0;
|
||||
|
||||
/****************************************************
|
||||
* Add a coordinate to the arrays
|
||||
* - stores a total of N_POLY_MAX pairs
|
||||
***************************************************/
|
||||
const int push_vert(const float x, const float y) {
|
||||
if(n_poly>N_POLY_MAX)
|
||||
return 0;
|
||||
polyx[n_poly]=x;
|
||||
polyy[n_poly]=y;
|
||||
n_poly++;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/****************************************************
|
||||
* 'Clear' pairs of coordinates
|
||||
***************************************************/
|
||||
void clear_verts() {
|
||||
n_poly=0;
|
||||
}
|
||||
|
||||
//int n_poly=0;
|
||||
/****************************************************
|
||||
* Check a pair of coordinates against two lists
|
||||
* of vertices
|
||||
@@ -112,31 +94,28 @@ const int check_bounds(const float x, const float y) {
|
||||
***************************************************/
|
||||
void import_protobuf(const uint8_t *protobuffer, const uint32_t size) {
|
||||
if(size != 122) {
|
||||
Serial.println("Failed to decode");
|
||||
Serial.println("nmd");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.println("Recieved valid protobuf data?");
|
||||
|
||||
uint32_t isr;
|
||||
isr = 0;
|
||||
isr = protobuffer[1];
|
||||
|
||||
if(isr>N_POLY_MAX) isr = N_POLY_MAX;
|
||||
|
||||
clear_verts();
|
||||
|
||||
if(isr>N_POLY_MAX) isr = 0;
|
||||
const uint8_t *ptr = protobuffer + 5;
|
||||
for(uint32_t i=0;i<isr;i++) {
|
||||
float x,y;
|
||||
memcpy(&x, ptr + i*12, 4);
|
||||
memcpy(&y, ptr + i*12+5, 4);
|
||||
|
||||
push_vert(x, y);
|
||||
for(uint8_t i=0;i<isr && i<N_POLY_MAX;i++) {
|
||||
memcpy(&polyx[i], ptr + i*12, 4);
|
||||
memcpy(&polyy[i], ptr + i*12+5, 4);
|
||||
Serial.print((int)polyx[i]);
|
||||
Serial.print(' ');
|
||||
Serial.print((int)polyy[i]);
|
||||
Serial.print('\n');
|
||||
}
|
||||
// n_poly = isr; - n_poly is isr
|
||||
}
|
||||
|
||||
uint8_t is_sending = 0;
|
||||
static volatile uint8_t is_sending = 0;
|
||||
void onEvent (ev_t ev) {
|
||||
// Serial.print(os_getTime());
|
||||
// Serial.print(": ");
|
||||
@@ -165,47 +144,61 @@ void onEvent (ev_t ev) {
|
||||
#define FIELD_TWO_FLOAT 0x15
|
||||
#define FIELD_TWO_VARIANT 0x10
|
||||
|
||||
const char oob[] = "OUT OF BOUNDS";
|
||||
const char inb[] = "IN BOUNDS";
|
||||
uint8_t buffer[] = {
|
||||
TYPE_STRING,
|
||||
PROTO_LEN,
|
||||
FIELD_ONE_FLOAT, 0x00, 0x00, 0x48, 0x43,
|
||||
FIELD_TWO_FLOAT, 0x00, 0x00, 0xc8, 0x42,
|
||||
FIELD_TWO_VARIANT, 0};
|
||||
FIELD_TWO_VARIANT, 0, 0};
|
||||
|
||||
void do_send(osjob_t* j){
|
||||
// Check if there is not a current TX/RX job running
|
||||
if (LMIC.opmode & OP_TXRXPEND) {
|
||||
Serial.println(F("OP_TXRXPEND, not sending"));
|
||||
//Serial.println(F("OP_TXRXPEND, not sending"));
|
||||
} else {
|
||||
// prepare upstream data transmission at the next possible time.
|
||||
// transmit on port 1 (the first parameter); you can use any value from 1 to 223 (others are reserved).
|
||||
// don't request an ack (the last parameter, if not zero, requests an ack from the network).
|
||||
// Remember, acks consume a lot of network resources; don't ask for an ack unless you really need it.
|
||||
const float latitude = gps.location.lat();
|
||||
const float latitude = gps.location.lat();
|
||||
const float longitude = gps.location.lng();
|
||||
const int oob = check_bounds(latitude, longitude);
|
||||
if(n_poly>0) {
|
||||
const uint8_t oob = !check_bounds(latitude, longitude);
|
||||
if(oob) {
|
||||
Serial.println(oob);
|
||||
} else {
|
||||
Serial.println(inb);
|
||||
}
|
||||
|
||||
memcpy(buffer+13, (void*)&oob, 1);
|
||||
} else {
|
||||
const uint8_t oob = 0;
|
||||
memcpy(buffer+13, (void*)&oob, 1);
|
||||
}
|
||||
memcpy(buffer+3, (void*)&latitude, 4);
|
||||
memcpy(buffer+8, (void*)&longitude, 4);
|
||||
memcpy(buffer+13, (void*)&oob, 1);
|
||||
|
||||
LMIC_setTxData2(1, buffer, sizeof(buffer), 0);
|
||||
LMIC_setTxData2(1, buffer, sizeof(buffer)-2, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void do_gps(osjob_t* j){
|
||||
while(softserial_available()>0) {
|
||||
#define GPS_MAX_ENCODES 60
|
||||
void read_gps(){
|
||||
timeout = 0;
|
||||
while(softserial_available()>0 && timeout < GPS_MAX_ENCODES) {
|
||||
gps.encode(softserial_read());
|
||||
timeout ++;
|
||||
}
|
||||
if(!is_sending && gps.location.isValid()) {
|
||||
Serial.println("gps");
|
||||
is_sending = 1;
|
||||
do_send(&sendjob);
|
||||
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
|
||||
}
|
||||
os_setTimedCallback(&gpsjob, os_getTime()+sec2osticks(GPS_INTERVAL), do_gps);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(4800);
|
||||
Serial.begin(9600);
|
||||
softserial_init();
|
||||
|
||||
delay(100);
|
||||
@@ -237,10 +230,9 @@ void setup() {
|
||||
|
||||
// Set data rate and transmit power for uplink
|
||||
LMIC_setDrTxpow(DR_SF7,14);
|
||||
|
||||
os_setTimedCallback(&gpsjob, os_getTime()+sec2osticks(GPS_INTERVAL), do_gps);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
os_runloop_once();
|
||||
os_runloop_once();
|
||||
read_gps();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user