switch to event loop style
This commit is contained in:
parent
62f1a532cc
commit
b84a0fea12
177
collar.cpp
177
collar.cpp
|
@ -41,7 +41,7 @@ unsigned char loraData[50] = {"hello LoRa"};
|
|||
|
||||
// How many times data transfer should occur, in seconds
|
||||
// const unsigned int sendInterval = 30;
|
||||
const unsigned int sendInterval = 60;
|
||||
const unsigned int sendInterval = 3000;
|
||||
|
||||
// Pinout for Adafruit Feather 32u4 LoRa
|
||||
TinyLoRa lora = TinyLoRa(2, 10, 9);
|
||||
|
@ -52,7 +52,7 @@ SoftwareSerial ss(6, 7);
|
|||
* Track each pair of X and Y coordinates
|
||||
* - arrays are used by the pnpoly function
|
||||
***************************************************/
|
||||
const uint8_t N_POLY_MAX=100;
|
||||
const uint8_t N_POLY_MAX=10;
|
||||
float polyx[N_POLY_MAX];
|
||||
float polyy[N_POLY_MAX];
|
||||
int n_poly=0;
|
||||
|
@ -109,6 +109,7 @@ bool Fenceless_Coordinates_callback(pb_istream_t *stream, const pb_field_iter_t
|
|||
push_vert(m.x,m.y);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/****************************************************
|
||||
* Load coordinates from protobuff stream
|
||||
|
@ -145,18 +146,6 @@ void import_protobuf(uint8_t *protobuffer, uint32_t size) {
|
|||
push_vert(m.coord0.x, m.coord0.y);
|
||||
}
|
||||
}
|
||||
/****************************************************
|
||||
* nanopb callback
|
||||
***************************************************/
|
||||
bool Fenceless_Coordinates_encode(pb_ostream_t *stream, const pb_field_iter_t *field, void * const * arg) {
|
||||
Serial.println("Encode called");
|
||||
return false;
|
||||
Fenceless_Coordinate c = *(Fenceless_Coordinate*)field->pData;
|
||||
if(!pb_encode_tag_for_field(stream, field)) {
|
||||
return false;
|
||||
}
|
||||
return pb_encode(stream, Fenceless_Coordinate_fields, field);
|
||||
}
|
||||
/****************************************************
|
||||
* live test of protobuf
|
||||
***************************************************/
|
||||
|
@ -177,7 +166,8 @@ void test() {
|
|||
void setup()
|
||||
{
|
||||
// give GPS time to start up
|
||||
delay(1000);
|
||||
delay(3000);
|
||||
|
||||
/****************************************************
|
||||
* Configure USART
|
||||
* - onboard serial to usb
|
||||
|
@ -207,6 +197,10 @@ void setup()
|
|||
// }
|
||||
// }
|
||||
|
||||
push_vert(44.55818, -123.28341);
|
||||
push_vert(44.55818, -123.28332);
|
||||
push_vert(44.558308, -123.28332);
|
||||
push_vert(44.558308, -123.28341);
|
||||
/****************************************************
|
||||
* Configure LoRa
|
||||
* - set to multi-channel
|
||||
|
@ -225,68 +219,46 @@ void setup()
|
|||
}
|
||||
lora.setPower(15); // 1
|
||||
|
||||
/****************************************************
|
||||
* Configure Timer Interrupt
|
||||
* - set for one second intervals
|
||||
***************************************************/
|
||||
TCCR1A = 0;
|
||||
TCCR1B = 0;
|
||||
|
||||
TCNT1 = 34286; // preload timer
|
||||
TCCR1B |= (1 << CS12); // 256 prescaler
|
||||
TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt
|
||||
|
||||
Serial.println("OK");
|
||||
}
|
||||
|
||||
const int16_t PROGRESS_BAR_COUNT = 50;
|
||||
const int16_t START_OF_LINE = 13;
|
||||
/****************************************************
|
||||
* Timer interrupt service routine
|
||||
* - this logic could be moved to loop()
|
||||
* and be controlled by a state variable
|
||||
* - generates a protobuf stream with current
|
||||
* GPS coordinate values
|
||||
* - sends buffer over LoRaWAN on a given
|
||||
* send interval
|
||||
* Read a byte from GPS over software serial
|
||||
***************************************************/
|
||||
int sendCounter = 0;
|
||||
ISR(TIMER1_OVF_vect) {
|
||||
digitalWrite(LED_BUILTIN, sendCounter%2);
|
||||
if(sendCounter==0 && gps.location.isValid()) {
|
||||
Serial.println("Valid gps");
|
||||
|
||||
// reset send counter
|
||||
sendCounter = sendInterval;
|
||||
|
||||
Fenceless_CollarResponse coord;
|
||||
coord.loc.x = gps.location.lat();
|
||||
coord.loc.y = gps.location.lng();
|
||||
|
||||
pb_ostream_t stream;
|
||||
stream = pb_ostream_from_buffer(loraData, sizeof(loraData));
|
||||
int err = pb_encode(&stream, Fenceless_CollarResponse_fields, &coord);
|
||||
|
||||
// Generate copy pasteable base64
|
||||
// char base64[50];
|
||||
// base64_encode(base64, (char*)loraData, stream.bytes_written);
|
||||
// Serial.println(stream.bytes_written);
|
||||
// Serial.println(base64);
|
||||
|
||||
Serial.println("Sending LoRa Data...");
|
||||
lora.sendData(loraData, stream.bytes_written, lora.frameCounter);
|
||||
// Optionally set the Frame Port (1 to 255)
|
||||
// uint8_t framePort = 1;
|
||||
// lora.sendData(loraData, sizeof(loraData), lora.frameCounter, framePort);
|
||||
Serial.print("Frame Counter: ");Serial.println(lora.frameCounter);
|
||||
lora.frameCounter++;
|
||||
}
|
||||
else if (!gps.location.isValid()) {
|
||||
Serial.println("waiting for gps");
|
||||
}
|
||||
else {
|
||||
Serial.println("delaying til next frame counter");
|
||||
}
|
||||
sendCounter--;
|
||||
int read_gps() {
|
||||
int ret = 0;
|
||||
while(ss.available()>0) {
|
||||
gps.encode(ss.read());
|
||||
ret = 1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
/****************************************************
|
||||
* Set cursor to beginning of line and clear it
|
||||
***************************************************/
|
||||
void clear_line() {
|
||||
Serial.write(START_OF_LINE);
|
||||
for(int i=0;i<PROGRESS_BAR_COUNT;i++)
|
||||
Serial.write(' ');
|
||||
Serial.write(START_OF_LINE);
|
||||
}
|
||||
/****************************************************
|
||||
* State variables
|
||||
* - track events of main loop
|
||||
***************************************************/
|
||||
enum STATE_ {
|
||||
START_GPS,
|
||||
WAITING_GPS,
|
||||
VERIFYING_GPS,
|
||||
SENDING_LORA,
|
||||
WAITING_LORA,
|
||||
LORA_DONE
|
||||
};
|
||||
int state = START_GPS;
|
||||
int loopCounter = 0;
|
||||
int startTime = 0;
|
||||
/****************************************************
|
||||
* Main loop
|
||||
* - feeds data to GPS module as it is made
|
||||
|
@ -294,13 +266,58 @@ ISR(TIMER1_OVF_vect) {
|
|||
***************************************************/
|
||||
void loop()
|
||||
{
|
||||
// For later when recieving from gateway is implemented
|
||||
// if(recieved) {
|
||||
// Serial.println("Recieved something");
|
||||
// } else {
|
||||
// Serial.println("Nothing yet");
|
||||
// }
|
||||
if(ss.available()>0)
|
||||
gps.encode(ss.read());
|
||||
delay(1000);
|
||||
if(state == START_GPS) {
|
||||
Serial.println("Waiting for GPS");
|
||||
state = WAITING_GPS;
|
||||
}
|
||||
else if(state == WAITING_GPS) {
|
||||
int got_data = read_gps();
|
||||
|
||||
if(got_data) {
|
||||
if(loopCounter%100==0)
|
||||
Serial.write('.');
|
||||
if(loopCounter>PROGRESS_BAR_COUNT*100) {
|
||||
clear_line();
|
||||
loopCounter=0;
|
||||
|
||||
state = VERIFYING_GPS;
|
||||
}
|
||||
loopCounter++;
|
||||
}
|
||||
}
|
||||
else if(state == VERIFYING_GPS) {
|
||||
if(gps.location.isValid())
|
||||
state = SENDING_LORA;
|
||||
else
|
||||
state = WAITING_GPS;
|
||||
}
|
||||
else if(state == SENDING_LORA) {
|
||||
Fenceless_CollarResponse coord;
|
||||
coord.loc.x = gps.location.lat();
|
||||
coord.loc.y = gps.location.lng();
|
||||
coord.oob = check_bounds(coord.loc.x, coord.loc.y);
|
||||
|
||||
pb_ostream_t stream;
|
||||
stream = pb_ostream_from_buffer(loraData, sizeof(loraData));
|
||||
pb_encode(&stream, Fenceless_CollarResponse_fields, &coord);
|
||||
|
||||
Serial.println("Sending LoRa Data...");
|
||||
lora.sendData(loraData, stream.bytes_written, lora.frameCounter);
|
||||
|
||||
Serial.print("Frame Counter: ");
|
||||
Serial.println(lora.frameCounter);
|
||||
startTime = millis();
|
||||
|
||||
state = WAITING_LORA;
|
||||
}
|
||||
else if(state == WAITING_LORA) {
|
||||
read_gps();
|
||||
|
||||
if(millis()/1000 - startTime >= sendInterval) {
|
||||
state = LORA_DONE;
|
||||
}
|
||||
}
|
||||
else if(state == LORA_DONE) {
|
||||
state = VERIFYING_GPS;
|
||||
}
|
||||
}
|
||||
|
|
2
gateway
2
gateway
|
@ -1 +1 @@
|
|||
Subproject commit 5dc3587ccb50c38eee2d7786f4bccee9d5ea2809
|
||||
Subproject commit d39b4e8494780050fe9875ee2f97a40122e6e9ac
|
Loading…
Reference in New Issue
Block a user