 #include "waypoint_store.h"
 #include "gps.h"
 
+#define REQ 9
+#define RDY 7
+#define RST 10
+
 #define WAYPOINT_DISTANCE_THRESHOLD 50 /* meters */
 #define BUSY_LED 17
+
+#define GREEN_BUTTON_INTERRUPT_NUM 1 /* corresponds to pin 2 (D2) */
+#define BLUE_BUTTON_INTERRUPT_NUM 0 /* corresponds to pin 3 (D3) */
+
+/* Global flag indicating a button press has not yet been handled.
+   Will be set to 1 on button press, and should be read and cleared
+   atomically (disable/renable interrupts). */
+uint8_t g_green_button_pressed = 0;
+uint8_t g_blue_button_pressed = 0;
 
 void print_tracking_display(struct tracking_data_t *data)
 {

#include <SPI.h>

#include "nokia_5110.h"
#include "time.h"

void loop(void)
{ 
}

/* called on button press interrupt */
void green_button_handler(void)
{
    /* only allow button press every 200 ms */
    static uint32_t last = 0;
    uint32_t current = millis();
    if (current - last > 200) {
        g_green_button_pressed = 1;
    }
    last = current;
}

void blue_button_handler(void)
{
    /* only allow button press every 200 ms */
    static uint32_t last = 0;
    uint32_t current = millis();
    if (current - last > 200) {
        g_blue_button_pressed = 1;
    }
    last = current;
}

void run_bluetooth(void)
{
    lcd_clear_display();
    lcd_write_str("Bluetooth");
    Adafruit_BLE_UART bluetooth = Adafruit_BLE_UART(REQ, RDY, RST);
    bluetooth.setDeviceName("PETRICE"); /* 7 characters max! */
    bluetooth.begin();
    get_and_store_waypoints(bluetooth);
}

void get_and_store_waypoints(Adafruit_BLE_UART bluetooth)
{
    char buffer[21];
    uint32_t count, received;
    float latitude, longitude;
    uint32_t *eeprom_address = 0;

    /* get count */
    bluetooth_get_token(bluetooth, buffer);
    count = atoi(buffer);

    eeprom_write_dword(eeprom_address, count);
    eeprom_address += 1;

    /* get latitudes and longitudes */
    for (received = 0; received < count; received++) {

        bluetooth_get_token(bluetooth, buffer);
        latitude = (float)atof(buffer);
        eeprom_write_float((float*)eeprom_address, latitude);

        eeprom_address += 1;

        bluetooth_get_token(bluetooth, buffer);
        longitude = (float)atof(buffer);
        eeprom_write_float((float*)eeprom_address, longitude);

        eeprom_address += 1;
    }
}

/* blocks until data is available from bluetooth */
aci_evt_opcode_t bluetooth_get_token(Adafruit_BLE_UART bluetooth, char buffer[20])
{
    aci_evt_opcode_t status;
    int pos = 0;

    /* wait for token */
    while (!bluetooth.available()) {
        bluetooth.pollACI();
        status = bluetooth.getState();
    }

    /* read token into buffer */
    while (bluetooth.available()) {
        buffer[pos++] = bluetooth.read();
    }
    buffer[pos] = '\0';

    return status;
}
