seems to work

This commit is contained in:
sessionm21 2020-05-16 07:51:15 +01:00
parent 869b2cab3c
commit 0b55fce9b8

View File

@ -27,7 +27,7 @@ static const u4_t DEVADDR = 0x260212B6;
// }
void debug_function(char *str) {
Serial.println(str);
// Serial.println(str);
}
@ -68,7 +68,7 @@ float * const polyy = polyx + N_POLY_MAX;
* of vertices
* - https://wrf.ecse.rpi.edu//Research/Short_Notes/pnpoly.html
***************************************************/
const int pnpoly
inline const int pnpoly
(const uint8_t nvert, const float *vertx, const float *verty, const float testx, const float testy)
{
uint8_t i, j, c = 0;
@ -85,7 +85,7 @@ const int pnpoly
* - takes current GPS coordinates
* - return 1 if in bounds
***************************************************/
const int check_bounds(const float x, const float y) {
inline const int check_bounds(const float x, const float y) {
return pnpoly(n_poly, polyx, polyy, x, y);
}
@ -95,8 +95,6 @@ const int check_bounds(const float x, const float y) {
* - loading arrays in nanopb does not appear
* to work.
***************************************************/
static volatile uint8_t data_available = 0;
static volatile uint8_t done_sending = 0;
inline void import_protobuf(const uint8_t *protobuffer, const uint8_t size) {
if(size != 122) {
Serial.println("nmd");
@ -108,25 +106,19 @@ inline void import_protobuf(const uint8_t *protobuffer, const uint8_t size) {
if(isr>N_POLY_MAX) isr = 0;
const uint8_t *ptr = protobuffer + 5;
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);
memcpy(&polyy[i], ptr + i*12, 4);
memcpy(&polyx[i], ptr + i*12+5, 4);
}
n_poly = isr;
data_available = 1;
}
void do_send(osjob_t* j);
void onEvent (ev_t ev) {
if(ev == EV_TXCOMPLETE) {
// Serial.print(F("- EV_TXCOMPLETE"));
Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
if (LMIC.txrxFlags & TXRX_ACK)
Serial.println(F("Received ack"));
if (LMIC.dataLen) {
Serial.println(F("Received "));
Serial.print(F(" -Received: "));
Serial.println(LMIC.dataLen);
Serial.println(F(" bytes of payload"));
}
// Schedule next transmission
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
@ -134,13 +126,6 @@ void onEvent (ev_t ev) {
if (LMIC.dataLen) {
import_protobuf(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
}
done_sending = 1;
}
else if(ev == EV_TXSTART) {
// Serial.print(F("- EV_TXSTART"));
}
else {
Serial.print(F("- Unknown event: "));
}
}
@ -157,12 +142,7 @@ volatile uint8_t state;
volatile uint32_t since;
void setup() {
Serial.begin(9600);
Serial.println(F("Starting"));
softserial_init();
// softserial_init();
// pinMode(LED_BUILTIN, OUTPUT);
softserial_init();
os_init();
LMIC_reset();
@ -186,31 +166,25 @@ void setup() {
delay(1000);
data_available = 0;
state = START_GPS;
do_send(&sendjob);
}
inline void read_gps() {
uint8_t available = softserial_available();
while(available> 0) {
while(available) {
gps.encode(softserial_read());
available--;
}
}
inline void on_start_gps() {
Serial.println("Starting gps");
state = WAIT_GPS;
}
inline void on_wait_gps() {
Serial.println("Waiting for gps");
if(gps.location.isValid()) {
state = START_LORA;
Serial.println("end of gps");
}
}
@ -222,8 +196,8 @@ inline void on_wait_gps() {
#define FIELD_TWO_VARIANT 0x10
const char oob[] = "OUT OF BOUNDS";
const char inb[] = "IN BOUNDS";
const char oob[] = " -OUT OF BOUNDS";
const char inb[] = " -IN BOUNDS";
uint8_t buffer[] = {
TYPE_STRING,
PROTO_LEN,
@ -237,12 +211,11 @@ void on_start_lora() {
void send_lora() {
if (LMIC.opmode & OP_TXRXPEND) {
Serial.print(" -cns");
done_sending = 1;
} else {
const float latitude = gps.location.lat();
const float longitude = gps.location.lng();
if(n_poly>0) {
const uint8_t out_of_bounds = 0;//!check_bounds(latitude, longitude);
const uint8_t out_of_bounds = check_bounds(latitude, longitude);
if(out_of_bounds) {
Serial.println(oob);
} else {
@ -268,13 +241,13 @@ void do_send(osjob_t* j){
#define TIMEOUT TX_INTERVAL * 2 * 1000UL
void on_wait_lora() {
if(millis()- since > TX_INTERVAL*1000UL || done_sending) {
if(millis()- since > TX_INTERVAL*1000UL) {
Serial.println(" -Lora Done Sending");
done_sending = 0;
state = START_GPS;
}
if(millis()- since > TIMEOUT) {
if(millis() - since > TIMEOUT) {
Serial.println(" -Scheduling another job");
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
}
}
@ -290,14 +263,12 @@ void loop() {
Serial.print('.');
if(state == START_GPS) {
// softserial_init();
on_start_gps();
}
else if(state == WAIT_GPS) {
on_wait_gps();
}
else if(state == START_LORA) {
// softserial_end();
on_start_lora();
}
else if(state == WAIT_LORA) {
@ -306,23 +277,18 @@ void loop() {
} else {
// Serial.print('.');
}
if(data_available) {
Serial.println("Data available");
for(uint8_t i=0;i<n_poly;i++) {
Serial.print('(');
Serial.print((int)(polyy[i]*100));
Serial.print(',');
Serial.print((int)(polyx[i]*100));
Serial.print(')');
Serial.print(',');
}
data_available = 0;
}
if(state == WAIT_GPS)
read_gps();
// if(data_available) {
// Serial.println("Data available");
// for(uint8_t i=0;i<n_poly;i++) {
// Serial.print('(');
// Serial.print((int)(polyy[i]*100));
// Serial.print(',');
// Serial.print((int)(polyx[i]*100));
// Serial.print(')');
// Serial.print(',');
// }
// data_available = 0;
// }
read_gps();
os_runloop_once();
// Serial.print(time);
// Serial.print(':');
// Serial.println(state);
// delay(100);
}