2019-06-24 14:43:04 +00:00
# include "settings.h"
2019-06-12 19:18:37 +00:00
# include <Arduino.h>
2019-06-28 13:02:44 +00:00
# ifdef ENABLE_MEMORY_DIAGNOSTICS
// #include <MemoryFree.h>
# define DISPLAY_FREE_MEMORY() Serial.println(freeMemory(), DEC);
# else
# define DISPLAY_FREE_MEMORY() // noop
# endif
2019-06-12 19:18:37 +00:00
2019-06-13 13:33:08 +00:00
# include "random.h"
2019-06-13 13:47:40 +00:00
// BAD PRACTICE: For some extremely strange reason, the Arduino IDE doesn't pick up random.cpp like it does our other source files - so we've got to explicitly include it here. If we had control over the build process (which we don't), we've use a Makefile here that handled this better.
# include "random.cpp"
2019-06-12 19:18:37 +00:00
# include "gps.h"
2019-06-24 12:52:33 +00:00
# include "peripheral.h"
2019-06-24 13:31:33 +00:00
# include "storage.h"
# include "power.h"
2019-06-24 14:43:04 +00:00
# include "transmission.h"
2019-06-12 19:18:37 +00:00
2019-06-13 13:33:08 +00:00
2019-06-12 19:18:37 +00:00
void setup ( ) {
Serial . begin ( BAUD_PC ) ;
2019-06-26 14:46:07 +00:00
Serial . println ( F ( " [main] Starting " ) ) ;
2019-06-13 13:33:08 +00:00
random_begin ( ) ;
2019-07-08 13:48:09 +00:00
power_init ( ) ;
2019-06-28 13:02:44 +00:00
DISPLAY_FREE_MEMORY ( ) ;
2019-06-26 14:46:07 +00:00
GPSLocation gps_data = gps_fetch ( ) ;
2019-06-28 13:02:44 +00:00
DISPLAY_FREE_MEMORY ( ) ;
2019-07-09 12:11:31 +00:00
// Serial.print(F("[main] Location ")); Serial.print(gps_data.lat); Serial.print(F(", ")); Serial.println(gps_data.lng);
2019-06-12 19:18:37 +00:00
2019-06-13 13:33:08 +00:00
uint32_t id = random_get ( ) ;
2019-06-12 19:18:37 +00:00
2019-07-09 12:11:31 +00:00
// Serial.print(F("[main] Id "));
// Serial.println(id);
2019-06-12 19:18:37 +00:00
2019-06-24 12:52:33 +00:00
// Activate microSD card breakout board on the SPI bus
2019-06-26 14:59:49 +00:00
peripheral_register ( PIN_SPI_CS_RFM95 ) ;
peripheral_register ( PIN_SPI_CS_SD ) ;
2019-06-24 12:52:33 +00:00
peripheral_unsilence ( PIN_SPI_CS_SD ) ;
2019-06-28 13:02:44 +00:00
DISPLAY_FREE_MEMORY ( ) ;
2019-06-26 14:46:07 +00:00
store_reading ( id , gps_data ) ;
2019-06-28 13:02:44 +00:00
DISPLAY_FREE_MEMORY ( ) ;
2019-06-26 14:46:07 +00:00
# ifdef SD_DEBUG_ENABLED
2019-06-27 16:26:22 +00:00
store_debug ( gps_data . time , 19 - 1 ) ; // Don't print the null byte
2019-06-28 13:02:44 +00:00
DISPLAY_FREE_MEMORY ( ) ;
2019-06-27 16:26:22 +00:00
# endif
2019-06-20 12:56:08 +00:00
2019-06-24 12:52:33 +00:00
// ------------------------------------------------------------------------
// Activate the RFM95
2019-06-24 14:43:04 +00:00
peripheral_switch ( PIN_SPI_CS_SD , PIN_SPI_CS_RFM95 ) ;
2019-07-15 13:31:39 +00:00
// Initialise an array to store the message in,but make sure it's at least 16 bytes long (because of AES restrictions at the other end)
uint8_t message_size = sizeof ( uint32_t ) + sizeof ( float ) * 2 ;
if ( message_size < 16 ) message_size = 16 ;
2019-06-28 12:34:30 +00:00
uint8_t message [ message_size ] ;
2019-07-15 13:31:39 +00:00
// Initialise the message to a known state (i.e. null bytes)
for ( uint8_t i = 0 ; i < message_size ; i + + )
message [ i ] = 0 ;
2019-06-24 12:52:33 +00:00
2019-06-28 12:34:30 +00:00
const uint8_t * bytes_id = reinterpret_cast < uint8_t const * > ( & id ) ;
const uint8_t * bytes_lat = reinterpret_cast < uint8_t const * > ( & ( gps_data . lat ) ) ;
2019-07-09 12:11:31 +00:00
const uint8_t * bytes_lng = reinterpret_cast < uint8_t const * > ( & ( gps_data . lng ) ) ;
2019-06-28 12:34:30 +00:00
2019-07-02 13:07:11 +00:00
// for(int i = 0; i < sizeof(uint32_t); i++) {
// }
// The size of a float == the size of a uint32_t
2019-06-28 12:34:30 +00:00
for ( int i = 0 ; i < sizeof ( float ) ; i + + ) {
2019-07-02 13:07:11 +00:00
message [ i ] = bytes_id [ i ] ;
2019-06-28 12:34:30 +00:00
message [ sizeof ( uint32_t ) + i ] = bytes_lat [ i ] ;
message [ sizeof ( uint32_t ) + sizeof ( float ) + i ] = bytes_lng [ i ] ;
}
2019-06-28 13:04:21 +00:00
# ifdef ENABLE_RADIO
2019-07-02 13:07:11 +00:00
// Serial.println(F("[main] Transmitting"));
2019-06-28 12:34:30 +00:00
transmit_init ( ) ;
transmit_send ( message , message_size ) ;
2019-06-28 13:04:21 +00:00
# endif
2019-06-24 12:52:33 +00:00
2019-06-20 12:56:08 +00:00
power_off ( ) ; // Doesn't return
2019-06-12 19:18:37 +00:00
}
void loop ( ) {
}