Delete LBridgev_PR_170316.ino

Delete LBridgev_PR_170316.ino

--- LBridgev_PR_170316.ino
+++ LBridgev_PR_170316.ino
@@ -1,1908 +0,0 @@
-/*
-   It scans the Freestyle Libre Sensor every 5 minutes
-   and sends the data to the xDrip Android app. You can
-   see the data in the serial monitor of Arduino IDE, too.
-   If you want another scan interval, simply change the
-   sleepTime value. To work with Android 4 you have to disable
-   all lines containing a "for Android 4" comment and set the
-   sleep time to 36.
-
-   This sketch is based on a sample sketch for the BM019 module
-   from Solutions Cubed.
-
-   Wiring for UNO / Pro-Mini:
-
-   Arduino          BM019           BLE-HM11
-   IRQ: Pin 9       DIN: pin 2
-   SS: pin 10       SS: pin 3
-   MOSI: pin 11     MOSI: pin 5
-   MISO: pin 12     MISO: pin4
-   SCK: pin 13      SCK: pin 6
-   I/O: pin 3 (VCC with Android 4)  VCC: pin 9
-   I/O: pin 5                       TX:  pin 2
-   I/O: pin 6                       RX:  pin 4
-*/
-
-/*
- * VWI, V 0.1, 03/2017
- * LBridge, a LimiTTer with xBridge Protocol extension and power saving mods. This sketch is based on LimiTTer code (JoernL)
- * for NFC reading and sleep mode handling and a port of the xBridge2 protocol with queueing from savek-cc.
- * Thanks to JoernL and savek-cc!
- *
- * Hardwaresource in xDrip has to be set to "LimiTTer". Please use an xDrip+ version >= nightly build 2107/02/09
- *
- * The xBridge2 protocol is used to send BG readings to xDrip+. In case of failure it queues up not sended BG readings
- * with the correct timestamp and send them within the next BLE connection. There should be no missed BG readings
- * when LimiTTer is worn.
- */
-
-#include <SPI.h>
-#include <SoftwareSerial.h>
-#include <avr/sleep.h>
-#include <avr/power.h>
-#include <avr/wdt.h>
-//#include "xbridge_libre.h"
-
-#define LNAME F("AT+NAMELBri0315")
-
-/* ************************************************ */
-/* *** config #DEFINES *** */
-/* ************************************************ */
-
-#define N_SHOW_LIMITTER       // show original Limitter output
-#define USE_DEAD_SENSOR       // we can test with a dead sensor
-#define N_SEND_ERROR_COUNTERS // send error counter values via BLE
-#define N_HW_BLE              // detect BLE conn status with system LED pin connected to arduino pin 2
-#define N_DYNAMIC_TXID        // get TXID automatically
-#define N_AUTOCAL_WDT         // code for auto calibrating WDT timing
-#define N_XB_NEW              // send 15 min trend
-
-/* ********************************************* */
-/* ********* LimiTTer stuff ******************** */
-/* ********************************************* */
-
-#define MIN_V 3450 // battery empty level
-#define MAX_V 4050 // battery full level
-
-const int SSPin = 10;   // Slave Select pin
-const int IRQPin = 9;   // Sends wake-up pulse for BM019
-const int NFCPin1 = 7;  // Power pin BM019
-const int NFCPin2 = 8;  // Power pin BM019
-const int NFCPin3 = 4;  // Power pin BM019
-const int BLEPin = 3;   // BLE power pin.
-const int MOSIPin = 11;
-const int SCKPin = 13;
-
-#ifdef HW_BLE
-const int bleSysLedPin = 2; // connect to Sys LED pin of HM-1x
-#endif
-
-byte RXBuffer[24];
-byte NFCReady = 0;  // used to track NFC state
-byte FirstRun = 1;
-byte batteryLow;
-int batteryPcnt;
-long batteryMv;
-
-// sleeptime in multipliers of 8 s
-int sleepTime = 32;
-
-int noDiffCount = 0;
-int sensorMinutesElapse;
-float lastGlucose;
-float trend[16];
-
-SoftwareSerial ble_Serial(5, 6); // RX | TX
-
-/* ************************************************* */
-/* *** VWI control stuff *** */
-/* ************************************************ */
-
-// global error counters
-unsigned long loop_count = 0;          // of main loop
-unsigned long ble_connect_errors = 0;  // no BLE connect after 40 s wait time
-unsigned long nfc_read_errors = 0;     // e. g. no sensor in range
-unsigned long nfc_scan_count = 0;      // how many scans?
-unsigned long nfc_inv_cmd_count = 0;   // how much SetInventroy commands?
-
-unsigned long nfc_prot_set_cmd_errors = 0;
-unsigned long nfc_inv_cmd_errors = 0;
-unsigned long nfc_read_mem_errors = 0;
-unsigned long resend_pkts_events = 0;
-unsigned long queue_events = 0;
-
-#ifdef SEND_ERROR_COUNTERS
-static boolean send_error_counters_via_ble = 0;
-#endif
-
-static boolean show_ble = 1;        // what is shown in serial monitor
-static boolean show_state = 1;      // show print_state() infos, disabled ftm
-
-boolean sensor_oor;                 // sensor out of range
-int ble_answer;                     // char from BLE reeived?
-boolean ble_lost_processing = 1;    // send AT+RESET after OK+LOST?
-
-// absolute progam run time in s counting also sleep() phase
-unsigned long prg_run_time = 0;     // in sec
-
-unsigned long loop_start_time;      // loop starting time in ms
-unsigned long loop_time;            // curretn loop duration in ms
-unsigned long ble_start_time = 0;   // time when BLE starts ms
-unsigned long ble_connect_time = 0; // time up to BLE connection in ms
-
-float avg_sleepTime = 0;            // average sleep time
-float bleTime = 0;                  // average arduino+BLE time
-float bleNFCTime = 0;
-
-/* ************************************************************* */
-/* *** code ported form xBridge2.c *** */
-/* ************************************************************* */
-
-// defines the xBridge protocol functional level.  Sent in each packet as the last byte.
-#define DEXBRIDGE_PROTO_LEVEL (0x01)
-
-static volatile boolean do_sleep = 0;     // indicates we should go to sleep between packets
-static volatile boolean got_ack = 0;      // indicates if we got an ack during the last do_services.
-static volatile boolean dex_tx_id_set;    // indicates if the Dexcom Transmitter id (settings.dex_tx_id) has been set.  Set in doServices.
-static volatile boolean ble_connected;    // bit indicating the BLE module is connected to the phone.  Prevents us from sending data without this.
-#ifdef HW_BLE
-static volatile boolean hwble_connected;  // bit indicating the BLE module is connected to the phone.  Prevents us from sending data without this.
-#endif
-
-static volatile boolean got_ok;           // flag indicating we got OK from the HM-1x
-
-static volatile unsigned long pkt_time = 0;
-static volatile unsigned long abs_pkt_time = 0;
-static volatile unsigned long last_abs_pkt_time = 0;
-
-//define the maximum command string length for USB commands.
-#define COMMAND_MAXLEN 40
-
-//structure of a USB command
-typedef struct _command_buff
-{
-  unsigned char commandBuffer[COMMAND_MAXLEN];
-  unsigned char nCurReadPos;
-} t_command_buff;
-
-static t_command_buff command_buff;
-
-typedef struct _Dexcom_packet
-{
-  unsigned long raw;
-  unsigned long ms;
-} Dexcom_packet;
-
-#define DXQUEUESIZE 36 // 3 h of queue
-
-typedef struct {
-  volatile unsigned char read;
-  volatile unsigned char write;
-  Dexcom_packet buffer[DXQUEUESIZE];
-} Dexcom_fifo;
-
-Dexcom_fifo Pkts;
-
-Dexcom_packet * DexPkt;
-
-// structure of a raw record we will send.
-typedef struct _nRawRecord
-{
-  unsigned char size; //size of the packet.
-  unsigned char cmd_code; // code for this data packet.  Always 00 for a Dexcom data packet.
-  unsigned long raw;  //"raw" BGL value. ??? use unfiltered NFC readings here?
-  unsigned long filtered; //"filtered" BGL value
-  unsigned char dex_battery;  //battery value
-  unsigned char my_battery; //xBridge battery value
-  unsigned long dex_src_id;   //raw TXID of the Dexcom Transmitter
-  unsigned long delay;
-  unsigned char function; // Byte representing the xBridge code funcitonality.  01 = this level.
-} nRawRecord;
-
-// _xBridge_settings - Type definition for storage of xBridge_settings
-// used for compatibility
-typedef struct _xBridge_settings
-{
-  unsigned long dex_tx_id;     //4 bytes
-  unsigned long uart_baudrate; //4 bytes
-} xBridge_settings;     //14 bytes total
-
-xBridge_settings settings;
-
-// array of HM-1x baudrates for rate detection.
-unsigned long uart_baudrate[9] = {9600L,19200L,38400L,57600L,115200L,4800,2400,1200,230400L};
-
-#ifdef XB_NEW
-_QuarterPacket1 qpkt1;
-_QuarterPacket2 qpkt2;
-#endif
-
-/* ********************************************* */
-/*  help functions */
-/* *********************************************** */
-
-// get free mem available
-extern unsigned int __bss_end;
-extern unsigned int __heap_start;
-extern void *__brkval;
-int freeMemory() {
-  int free_memory;
-
-  if((int)__brkval == 0)
-    free_memory = ((int)&free_memory) - ((int)&__bss_end);
-  else
-    free_memory = ((int)&free_memory) - ((int)__brkval);
-
-  return free_memory;
-}
-
-// millis() since program start, Arduino millis() are not counting when in sleep mode
-unsigned long abs_millis(void)
-{
-  return(prg_run_time*1000 + (millis() - loop_start_time));
-}
-
-/* **************************************************************** */
-/* modified LimiTTer code */
-/* **************************************************************** */
-
-// initialize the hardware
-void setup() {
-    // NFC part
-    pinMode(IRQPin, OUTPUT);
-    digitalWrite(IRQPin, HIGH);
-    pinMode(SSPin, OUTPUT);
-    digitalWrite(SSPin, HIGH);
-    pinMode(NFCPin1, OUTPUT);
-    digitalWrite(NFCPin1, HIGH);
-    pinMode(NFCPin2, OUTPUT);
-    digitalWrite(NFCPin2, HIGH);
-    pinMode(NFCPin3, OUTPUT);
-    digitalWrite(NFCPin3, HIGH);
-
-    // BLE part
-    pinMode(BLEPin, OUTPUT); // Disable this for Android 4
-    digitalWrite(BLEPin, HIGH); // Disable this for Android 4
-    ble_start_time = millis();
-    ble_connected = 0;
-#ifdef HW_BLE
-    hwble_connected = 0;
-    pinMode(bleSysLedPin, INPUT);
-#endif
-
-    // NFC part
-/* not needed here, is done in SPI.begin(), acoording to Bert Roode, 170302
-    pinMode(MOSIPin, OUTPUT);
-    pinMode(SCKPin, OUTPUT);
-*/
-    // try this first
-    Serial.begin(9600);
-}
-
-/* *********************************************************** */
-/* LimiTTer code, only small modifications */
-/* *********************************************************** */
-
-void SetProtocol_Command() {
-unsigned long ct;
-
-  digitalWrite(SSPin, LOW);
-  SPI.transfer(0x00);  // SPI control byte to send command to CR95HF
-  SPI.transfer(0x02);  // Set protocol command
-  SPI.transfer(0x02);  // length of data to follow
-  SPI.transfer(0x01);  // code for ISO/IEC 15693
-  SPI.transfer(0x0D);  // Wait for SOF, 10% modulation, append CRC
-  digitalWrite(SSPin, HIGH);
-  delay(1);
-
-  ct = millis();
-  digitalWrite(SSPin, LOW);
-  while( RXBuffer[0] != 8 )
-    {
-    RXBuffer[0] = SPI.transfer(0x03);  // Write 3 until
-    RXBuffer[0] = RXBuffer[0] & 0x08;  // bit 3 is set
-    if ( (millis() - ct) > 1000) {
-      print_state(F(" - poll SetProtocol_Command not successfull"));
-      break;
-    }
-
-    }
-  digitalWrite(SSPin, HIGH);
-  delay(1);
-
-  digitalWrite(SSPin, LOW);
-  SPI.transfer(0x02);   // SPI control byte for read
-  RXBuffer[0] = SPI.transfer(0);  // response code
-  RXBuffer[1] = SPI.transfer(0);  // length of data
-  digitalWrite(SSPin, HIGH);
-
-  if ((RXBuffer[0] == 0) & (RXBuffer[1] == 0))  // is response code good?
-    {
-    print_state(F(" - Protocol Set Command OK"));
-    NFCReady = 1; // NFC is ready
-    }
-  else
-    {
-    print_state(F(" - Protocol Set Command FAIL"));
-    NFCReady = 0; // NFC not ready
-    nfc_prot_set_cmd_errors++;
-    }
-}
-
-void Inventory_Command() {
-unsigned long ct;
-  digitalWrite(SSPin, LOW);
-  SPI.transfer(0x00);  // SPI control byte to send command to CR95HF
-  SPI.transfer(0x04);  // Send Receive CR95HF command
-  SPI.transfer(0x03);  // length of data that follows is 0
-  SPI.transfer(0x26);  // request Flags byte
-  SPI.transfer(0x01);  // Inventory Command for ISO/IEC 15693
-  SPI.transfer(0x00);  // mask length for inventory command
-  digitalWrite(SSPin, HIGH);
-  delay(1);
-
-  ct = millis();
-  digitalWrite(SSPin, LOW);
-  while(RXBuffer[0] != 8)
-    {
-    RXBuffer[0] = SPI.transfer(0x03);  // Write 3 until
-    RXBuffer[0] = RXBuffer[0] & 0x08;  // bit 3 is set
-    if ( (millis() - ct) > 1000) {
-      print_state(F(" - poll Inventory_Command not successfull"));
-      break;
-    }
-    }
-  digitalWrite(SSPin, HIGH);
-  delay(1);
-
-  digitalWrite(SSPin, LOW);
-  SPI.transfer(0x02);   // SPI control byte for read
-  RXBuffer[0] = SPI.transfer(0);  // response code
-  RXBuffer[1] = SPI.transfer(0);  // length of data
-  for (byte i=0;i<RXBuffer[1];i++)
-      RXBuffer[i+2]=SPI.transfer(0);  // data
-  digitalWrite(SSPin, HIGH);
-  delay(1);
-
-  if (RXBuffer[0] == 128)  // is response code good?
-    {
-    print_state(F(" - Sensor in range ... OK"));
-    NFCReady = 2;
-    sensor_oor = 0;
-    }
-  else
-    {
-    print_state(F(" - Sensor out of range"));
-    NFCReady = 1;
-    nfc_inv_cmd_errors++;
-    sensor_oor = 1;
-    }
- }
-
-float Read_Memory() {
- byte oneBlock[8];
- String hexPointer = "";
- String trendValues = "";
- String hexMinutes = "";
- String elapsedMinutes = "";
- float trendOneGlucose;
- float trendTwoGlucose;
- float currentGlucose = 0;  // initialise to avoid compiler warning
- float shownGlucose;
- float averageGlucose = 0;
- int glucosePointer;
- int validTrendCounter = 0;
- float validTrend[16];
-  unsigned long ct;
-
- for ( int b = 3; b < 16; b++) {
-
-  digitalWrite(SSPin, LOW);
-  SPI.transfer(0x00);  // SPI control byte to send command to CR95HF
-  SPI.transfer(0x04);  // Send Receive CR95HF command
-  SPI.transfer(0x03);  // length of data that follows
-  SPI.transfer(0x02);  // request Flags byte
-  SPI.transfer(0x20);  // Read Single Block command for ISO/IEC 15693
-  SPI.transfer(b);  // memory block address
-  digitalWrite(SSPin, HIGH);
-  delay(1);
-
-  ct = millis();
-  digitalWrite(SSPin, LOW);
-  while(RXBuffer[0] != 8)
-    {
-    RXBuffer[0] = SPI.transfer(0x03);  // Write 3 until
-    RXBuffer[0] = RXBuffer[0] & 0x08;  // bit 3 is set
-    if ( (millis() - ct) > 1000) {
-      print_state(F(" - poll Read_Memory"));
-      break;
-    }
-    }
-  digitalWrite(SSPin, HIGH);
-  delay(1);
-
-  digitalWrite(SSPin, LOW);
-  SPI.transfer(0x02);   // SPI control byte for read
-  RXBuffer[0] = SPI.transfer(0);  // response code
-  RXBuffer[1] = SPI.transfer(0);  // length of data
- for (byte i=0;i<RXBuffer[1];i++)
-   RXBuffer[i+2]=SPI.transfer(0);  // data
-
-  digitalWrite(SSPin, HIGH);
-  delay(1);
-
- for (int i = 0; i < 8; i++)
-   oneBlock[i] = RXBuffer[i+3];
-
-  char str[24];
-  unsigned char * pin = oneBlock;
-  const char * hex = "0123456789ABCDEF";
-  char * pout = str;
-  for(; pin < oneBlock+8; pout+=2, pin++) {
-      pout[0] = hex[(*pin>>4) & 0xF];
-      pout[1] = hex[ *pin     & 0xF];
-  }
-  pout[0] = 0;
-#ifdef SHOW_LIMITTER
-  Serial.println(str);
-#endif
-  trendValues += str;
- }
-
- digitalWrite(SSPin, LOW);
-  SPI.transfer(0x00);  // SPI control byte to send command to CR95HF
-  SPI.transfer(0x04);  // Send Receive CR95HF command
-  SPI.transfer(0x03);  // length of data that follows
-  SPI.transfer(0x02);  // request Flags byte
-  SPI.transfer(0x20);  // Read Single Block command for ISO/IEC 15693
-  SPI.transfer(39);  // memory block address
-  digitalWrite(SSPin, HIGH);
-  delay(1);
-
-  digitalWrite(SSPin, LOW);
-  while(RXBuffer[0] != 8)
-    {
-    RXBuffer[0] = SPI.transfer(0x03);  // Write 3 until
-    RXBuffer[0] = RXBuffer[0] & 0x08;  // bit 3 is set
-    }
-  digitalWrite(SSPin, HIGH);
-  delay(1);
-
-  digitalWrite(SSPin, LOW);
-  SPI.transfer(0x02);   // SPI control byte for read
-  RXBuffer[0] = SPI.transfer(0);  // response code
-  RXBuffer[1] = SPI.transfer(0);  // length of data
- for (byte i=0;i<RXBuffer[1];i++)
-   RXBuffer[i+2]=SPI.transfer(0);  // data
-
-  digitalWrite(SSPin, HIGH);
-  delay(1);
-
- for (int i = 0; i < 8; i++)
-   oneBlock[i] = RXBuffer[i+3];
-
-  char str[24];
-  unsigned char * pin = oneBlock;
-  const char * hex = "0123456789ABCDEF";
-  char * pout = str;
-  for(; pin < oneBlock+8; pout+=2, pin++) {
-      pout[0] = hex[(*pin>>4) & 0xF];
-      pout[1] = hex[ *pin     & 0xF];
-  }
-  pout[0] = 0;
-
-#ifdef SHOW_LIMITTER
-  Serial.println(str);
-#endif
-
-  elapsedMinutes += str;
-
-  if (RXBuffer[0] == 128) // is response code good?
-    {
-      hexMinutes = elapsedMinutes.substring(10,12) + elapsedMinutes.substring(8,10);
-      hexPointer = trendValues.substring(4,6);
-      sensorMinutesElapse = strtoul(hexMinutes.c_str(), NULL, 16);
-      glucosePointer = strtoul(hexPointer.c_str(), NULL, 16);
-
-#ifdef SHOW_LIMITTER
-      Serial.println(F(""));
-      Serial.print(F("Glucose pointer: "));
-      Serial.print(glucosePointer);
-      Serial.println(F(""));
-#endif
-      int ii = 0;
-      for (int i=8; i<=200; i+=12) {
-        if (glucosePointer == ii)
-        {
-          if (glucosePointer == 0)
-          {
-            String trendNow = trendValues.substring(190,192) + trendValues.substring(188,190);
-            String trendOne = trendValues.substring(178,180) + trendValues.substring(176,178);
-            String trendTwo = trendValues.substring(166,168) + trendValues.substring(164,166);
-            currentGlucose = Glucose_Reading(strtoul(trendNow.c_str(), NULL ,16));
-            trendOneGlucose = Glucose_Reading(strtoul(trendOne.c_str(), NULL ,16));
-            trendTwoGlucose = Glucose_Reading(strtoul(trendTwo.c_str(), NULL ,16));
-
-            if (FirstRun == 1)
-               lastGlucose = currentGlucose;
-
-            if (((lastGlucose - currentGlucose) > 50) || ((currentGlucose - lastGlucose) > 50))
-            {
-               if (((lastGlucose - trendOneGlucose) > 50) || ((trendOneGlucose - lastGlucose) > 50))
-                  currentGlucose = trendTwoGlucose;
-               else
-                  currentGlucose = trendOneGlucose;
-            }
-          }
-          else if (glucosePointer == 1)
-          {
-            String trendNow = trendValues.substring(i-10,i-8) + trendValues.substring(i-12,i-10);
-            String trendOne = trendValues.substring(190,192) + trendValues.substring(188,190);
-            String trendTwo = trendValues.substring(178,180) + trendValues.substring(176,178);
-            currentGlucose = Glucose_Reading(strtoul(trendNow.c_str(), NULL ,16));
-            trendOneGlucose = Glucose_Reading(strtoul(trendOne.c_str(), NULL ,16));
-            trendTwoGlucose = Glucose_Reading(strtoul(trendTwo.c_str(), NULL ,16));
-
-            if (FirstRun == 1)
-               lastGlucose = currentGlucose;
-
-            if (((lastGlucose - currentGlucose) > 50) || ((currentGlucose - lastGlucose) > 50))
-            {
-               if (((lastGlucose - trendOneGlucose) > 50) || ((trendOneGlucose - lastGlucose) > 50))
-                  currentGlucose = trendTwoGlucose;
-               else
-                  currentGlucose = trendOneGlucose;
-            }
-          }
-          else
-          {
-            String trendNow = trendValues.substring(i-10,i-8) + trendValues.substring(i-12,i-10);
-            String trendOne = trendValues.substring(i-22,i-20) + trendValues.substring(i-24,i-22);
-            String trendTwo = trendValues.substring(i-34,i-32) + trendValues.substring(i-36,i-34);
-            currentGlucose = Glucose_Reading(strtoul(trendNow.c_str(), NULL ,16));
-            trendOneGlucose = Glucose_Reading(strtoul(trendOne.c_str(), NULL ,16));
-            trendTwoGlucose = Glucose_Reading(strtoul(trendTwo.c_str(), NULL ,16));
-
-
-            if (FirstRun == 1)
-               lastGlucose = currentGlucose;
-
-            if (((lastGlucose - currentGlucose) > 50) || ((currentGlucose - lastGlucose) > 50))
-            {
-               if (((lastGlucose - trendOneGlucose) > 50) || ((trendOneGlucose - lastGlucose) > 50))
-                  currentGlucose = trendTwoGlucose;
-               else
-                  currentGlucose = trendOneGlucose;
-            }
-          }
-        }
-
-        ii++;
-      }
-
-     for (int i=8, j=0; i<200; i+=12,j++) {
-          String t = trendValues.substring(i+2,i+4) + trendValues.substring(i,i+2);
-          trend[j] = Glucose_Reading(strtoul(t.c_str(), NULL ,16));
-       }
-
-    for (int i=0; i<16; i++)
-    {
-      if (((lastGlucose - trend[i]) > 50) || ((trend[i] - lastGlucose) > 50)) // invalid trend check
-         continue;
-      else
-      {
-         validTrend[validTrendCounter] = trend[i];
-         validTrendCounter++;
-      }
-    }
-
-    if (validTrendCounter > 0)
-    {
-      for (int i=0; i < validTrendCounter; i++)
-         averageGlucose += validTrend[i];
-
-      averageGlucose = averageGlucose / validTrendCounter;
-
-      if (((lastGlucose - currentGlucose) > 50) || ((currentGlucose - lastGlucose) > 50))
-         shownGlucose = averageGlucose; // If currentGlucose is still invalid take the average value
-      else
-         shownGlucose = currentGlucose; // All went well. Take and show the current value
-    }
-    else
-      shownGlucose = currentGlucose; // If all is going wrong, nevertheless take and show a value
-
-    if ((lastGlucose == currentGlucose) && (sensorMinutesElapse > 21000)) // Expired sensor check
-      noDiffCount++;
-
-    if (lastGlucose != currentGlucose) // Reset the counter
-      noDiffCount = 0;
-
-    if (currentGlucose != 0)
-      lastGlucose = currentGlucose;
-
-    NFCReady = 2;
-    FirstRun = 0;
-
-    print_state(F(" - bg reading "));
-    Serial.print(shownGlucose);
-    Serial.print(F(", BatLev: "));
-    Serial.print(batteryPcnt);
-    Serial.print(F("%, "));
-    Serial.print(F("BatMv: "));
-    Serial.print(batteryMv);
-    Serial.print(F("mV, "));
-    Serial.print(F("SensLife: "));
-    Serial.print(sensorMinutesElapse);
-    Serial.print(F(" min elapsed"));
-
-    print_state(F(" - 15 minutes-trend: "));
-    for (int i=0; i<16; i++) {
-      Serial.print(trend[i]);
-      Serial.print(F(" "));
-      if ( i == 8 )
-        Serial.println(F(""));
-    }
-
-#ifdef XB_NEW
-    int i;
-    qpkt1.size = 19;
-    qpkt1.cmd_code = 0x02;
-    qpkt1.sub_code = 0x04;
-    for ( i = 0 ; i < 8 ; i++ )
-      qpkt1.trend[i] = trend[i];
-    qpkt2.size = 19;
-    qpkt2.cmd_code = 0x02;
-    qpkt2.sub_code = 0x05;
-    for ( i = 8 ; i < 16 ; i++ )qpkt
-
-      qpkt2.trend[i-8] = trend[i];
-#endif /* XB_NEW */
-
-#ifdef USE_DEAD_SENSOR
-    // we do support tests with expired sensors!
-    noDiffCount = 0;
-#endif
-
-    if (noDiffCount > 5)
-      return 0;
-    else
-      return shownGlucose;
-    }
-  else
-    {
-    Serial.print(F("Read Memory Block Command FAIL"));
-    NFCReady = 0;
-    nfc_read_mem_errors++;
-    }
-    return(0);  // return added to avoid compiler warning
- }
-
-float Glucose_Reading(unsigned int val) {
-        int bitmask = 0x0FFF;
-        return ((val & bitmask) / 8.5);
-}
-
-/* ******************************************* */
-/* *** Vcc and sleep *** */
-/* ******************************************* */
-
-int readVcc() {
-  // Read 1.1V reference against AVcc
-  // set the reference to Vcc and the measurement to the internal 1.1V reference
-  #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
-    ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
-  #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
-    ADMUX = _BV(MUX5) | _BV(MUX0);
-  #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
-    ADMUX = _BV(MUX3) | _BV(MUX2);
-  #else
-    ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
-  #endif
-
-  delay(2); // Wait for Vref to settle
-  ADCSRA |= _BV(ADSC); // Start conversion
-  while (bit_is_set(ADCSRA,ADSC)); // measuring
-
-  uint8_t low  = ADCL; // must read ADCL first - it then locks ADCH
-  uint8_t high = ADCH; // unlocks both
-
-  batteryMv = (high<<8) | low;
-
-  batteryMv = 1125300L / batteryMv; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
-  int batteryLevel = min(map(batteryMv, MIN_V, MAX_V, 0, 100), 100); // Convert voltage to percentage
-  return batteryLevel;
-}
-
-void goToSleep(const byte interval, int time) {
-  // say how long we want to sleep in absolute
-  print_state(F(" - go to sleep for "));
-  Serial.print((time)*8);
-  Serial.print(F(" s - "));
-  waitDoingServices(100, 1);
-  for (int i=0; i<time; i++) {
-    MCUSR = 0;
-    WDTCSR |= 0b00011000;
-    WDTCSR =  0b01000000 | interval;
-    set_sleep_mode (SLEEP_MODE_PWR_DOWN);
-    sleep_enable();
-    sleep_cpu();
-  }
-}
-ISR(WDT_vect)
- {
- wdt_disable();
- }
-
-void lowBatterySleep() {
-
-//  shutNFCDown();
-  shutBLEDown(0);
-
-  print_state(F("Battery low! LEVEL: "));
-  Serial.print(batteryPcnt);
-  Serial.print(F("%"));
-  waitDoingServices(100, 1);
-
- // Switch LED on and then off shortly
-  for (int i=0; i<10; i++) {
-    digitalWrite(SCKPin, HIGH);
-    delay(50);
-    digitalWrite(SCKPin, LOW);
-    delay(100);
-  }
-
-  MCUSR = 0;
-  WDTCSR |= 0b00011000;
-  WDTCSR =  0b01000000 | 0b100001;
-  set_sleep_mode (SLEEP_MODE_PWR_DOWN);
-  sleep_enable();
-  sleep_cpu();
-  sleep_disable();
-  power_all_enable();
-  wdt_reset();
-}
-
-void wakeUp(void)
-{
-  sleep_disable();
-  power_all_enable();
-  wdt_reset();
-}
-
-// LimiTTer code to save battery
-void check_battery(void)
-{
-  // LimiTTer battery OK ?
-  batteryPcnt = readVcc();
-  if (batteryPcnt < 1)
-    batteryLow = 1;
-  while (batteryLow == 1)
-  {
-    lowBatterySleep();
-    batteryPcnt = readVcc();
-    if (batteryPcnt > 10)
-    {
-      batteryLow = 0;
-      wakeUp();
-//      wakeBLEUp();
-      waitDoingServices(250, 1);
-    }
-  }
-}
-
-/* ************************************************ */
-/* *** calibration of WDT oscillator *** */
-/* *** see http://forum.arduino.cc/index.php/topic,38046.0.html *** */
-/* *** and http://forum.arduino.cc/index.php?topic=49549.0 *** /
-/* ************************************************ */
-
-#ifdef AUTOCAL_WDT
-
-long timeSleep = 0;  // total time due to sleep
-float calibv = 0.93; // ratio of real clock with WDT clock
-volatile byte isrcalled = 0;  // WDT vector flag
-
-// Internal function: Start watchdog timer
-// byte psVal - Prescale mask
-void WDT_On (byte psVal)
-{
- // prepare timed sequence first
- byte ps = (psVal | (1<<WDIE)) & ~(1<<WDE);
- cli();
- wdt_reset();
- /* Clear WDRF in MCUSR */
- MCUSR &= ~(1<<WDRF);
- // start timed sequence
- WDTCSR |= (1<<WDCE) | (1<<WDE);
- // set new watchdog timeout value
- WDTCSR = ps;
- sei();
-}
-
-// Internal function.  Stop watchdog timer
-void WDT_Off() {
- cli();
- wdt_reset();
- /* Clear WDRF in MCUSR */
- MCUSR &= ~(1<<WDRF);
- /* Write logical one to WDCE and WDE */
- /* Keep old prescaler setting to prevent unintentional time-out */
- WDTCSR |= (1<<WDCE) | (1<<WDE);
- /* Turn off WDT */
- WDTCSR = 0x00;
- sei();
-}
-
-// Calibrate watchdog timer with millis() timer(timer0)
-void calibrate() {
- // timer0 continues to run in idle sleep mode
- set_sleep_mode(SLEEP_MODE_IDLE);
- long tt1=millis();
- doSleep(256);
- long tt2=millis();
- calibv = 256.0/(tt2-tt1);
-}
-
-// Estimated millis is real clock + calibrated sleep time
-long estMillis() {
- return millis()+timeSleep;
-}
-
-// Delay function
-void sleepCPU_delay(long sleepTime) {
- ADCSRA &= ~(1<<ADEN);  // adc off
- PRR = 0xEF; // modules off
-
- set_sleep_mode(SLEEP_MODE_PWR_DOWN);
- int trem = doSleep(sleepTime*calibv);
- timeSleep += (sleepTime-trem);
-
- PRR = 0x00; //modules on
- ADCSRA |= (1<<ADEN);  // adc on
-}
-
-// internal function.
-int doSleep(long timeRem) {
- byte WDTps = 9;  // WDT Prescaler value, 9 = 8192ms
-
- isrcalled = 0;
- sleep_enable();
- while(timeRem > 0) {
-   //work out next prescale unit to use
-   while ((0x10<<WDTps) > timeRem && WDTps > 0) {
-     WDTps--;
-   }
-   // send prescaler mask to WDT_On
-   WDT_On((WDTps & 0x08 ? (1<<WDP3) : 0x00) | (WDTps & 0x07));
-   isrcalled=0;
-   while (isrcalled==0) {
-     // turn bod off
-     MCUCR |= (1<<BODS) | (1<<BODSE);
-     MCUCR &= ~(1<<BODSE);  // must be done right before sleep
-     sleep_cpu();  // sleep here
-   }
-   // calculate remaining time
-   timeRem -= (0x10<<WDTps);
- }
- sleep_disable();
- return timeRem;
-}
-
-// wdt int service routine
-ISR(WDT_vect) {
- WDT_Off();
- isrcalled=1;
-}
-
-#endif /* AUTOCAL_WDT */
-
-/* ************************************************ */
-/* *** BLE handling *** */
-/* ************************************************ */
-
-// init HM-1x module
-int setupBLE()
-{
-  int i;
-
-  //detect HM-1x baudrate, if not currently set
-  print_state(F(" - determining HM-1x baudrate"));
-
-  for( i = 0 ; i <= 8 ; i++ ) {
-    init_command_buff(&command_buff);
-    print_state(F(" - trying ")); Serial.print(uart_baudrate[i]);
-    settings.uart_baudrate = uart_baudrate[i];
-    ble_Serial.begin(uart_baudrate[i]);
-    send_string(F("AT"), 100);
-    if ( waitDoingServicesInterruptible(500,&got_ok,1) )
-      break;
-  }
-
-  if(!got_ok){
-    print_state(F("Could not detect baudrate of HM-1x, setting 9600"));
-    settings.uart_baudrate=9600;
-    resetBLE();
-    return(0);
-  }
-
-  print_state(F(" - baudrate set to "));
-  Serial.print(settings.uart_baudrate);
-  init_command_buff(&command_buff);
-  return(1);
-}
-
-// Configure the BlueTooth module with a name.
-// from LimiTTer setp() moved to here
-void configBLE() {
-  send_string(F("AT"), 100);        // cut BLE connection to do commands
-  send_ble_string(F("AT+RENEW"));   // restore factory defaults
-  send_ble_string(F("AT+IMME1"));   // be sure not to be connected before the AT commands are done
-  send_ble_string(LNAME);           // set unique LimiTTer name, max 11 (not 12) chars
-  send_ble_string(F("AT+VERR?"));   // look for correct BLE module - answer schould be "HMSoft V54x"
-                                    // to detect fake modules which starts with 115200
-  send_ble_string(F("AT+ADDR?"));   // get MAC address
-  send_ble_string(F("AT+NOTI1"));   // notify CONNECT and LOST
-  send_ble_string(F("AT+RESET"));   // reset the module
-  waitDoingServices(500, 1);
-  if ( !ble_connected )
-    send_string(F("AT+START"), 200);  // all AT commands done, now we can connect
-}
-
-void resetBLE(void)
-{
-  print_state(F(" - reset BLE, power down - 500 ms - power up - 500 ms"));
-
-  digitalWrite(5, LOW); // Disable this for Android 4
-  digitalWrite(6, LOW); // Disable this for Android 4
-  digitalWrite(BLEPin, LOW); // Disable this for Android 4
-
-  waitDoingServices(500, 1);
-
-  digitalWrite(BLEPin, HIGH);
-  digitalWrite(5, HIGH);
-  digitalWrite(6, HIGH);
-
-  waitDoingServices(500, 1);
-
-}
-
-void wakeBLEUp(void)
-{
-  digitalWrite(BLEPin, HIGH);
-  digitalWrite(5, HIGH);
-  digitalWrite(6, HIGH);
-
-  ble_start_time = millis();
-  ble_connected = 0;
-#ifdef HW_BLE
-  hwble_connected = 0;
-#endif
-
-  // wait 1 s for initial connect, time for a fast BLE connect
-  waitDoingServicesInterruptible(1000, &ble_connected, 1);
-
-//  send_string(F("AT"), 100);        // cut BLE connection to do commands
-  send_ble_string(F("AT+RENEW"));   // restore factory defaults
-  send_ble_string(F("AT+IMME1"));   // first do all AT commands
-  send_ble_string(LNAME);           // set unique LimiTTer name, max 11 (not 12) chars
-  send_ble_string(F("AT+NOTI1"));   // notify CONNECT and LOST
-  send_ble_string(F("AT+RESET"));   // reset the BLE module
-  waitDoingServices(500, 1);
-  if ( !ble_connected )
-    send_string(F("AT+START"), 200);  // all AT commands done, now we can connect
-
-  print_state(F(" - wake up - wait 40 s for BLE\r\n"));
-  int i;
-  for ( i = 0 ; i < 40 ; i++) {
-    waitDoingServices(1000, 1);
-    // typical CONN/LOST time difference is 1 - 2 s, mostly happens at the beginning of loop
-    // wait for this 2 s before break
-    if ( ble_connected ) {
-      print_state(F(" - connected, wait to manage CONN+LOST cyle then send packet(s)"));
-      // LOST on CONN mostly after 800 - 900 ms
-      waitDoingServices(2000, 1);
-      break;
-    }
-  }
-
-  // must be connected here to start communication
-  if ( !ble_connected )
-    ble_connect_errors++;
-
-  // send error counters to xDrip+ for future use
-#ifdef SEND_ERROR_COUNTERS
-  if ( ble_connected && send_error_counters_via_ble ) {
-    ble_Serial.print(F("E"));
-    ble_Serial.print(loop_count);
-    ble_Serial.print(F(" "));
-    ble_Serial.print(ble_connect_errors);
-    ble_Serial.print(F(" "));
-    ble_Serial.print(nfc_prot_set_cmd_errors);
-    ble_Serial.print(F(" "));
-    ble_Serial.print(nfc_inv_cmd_errors);
-    ble_Serial.print(F(" "));
-    ble_Serial.print(nfc_read_mem_errors);
-    ble_Serial.print(F(" "));
-    ble_Serial.print(resend_pkts_events);
-    ble_Serial.print(F(" "));
-    ble_Serial.print(queue_events);
-    send_error_counters_via_ble = 0;
-  }
-#endif
-}
-
-void shutBLEDown(boolean quit_ble)
-{
-  // cut BLE conection via AT command
-  if ( quit_ble && ble_connected ) {
-    print_state(F(" - disconnect BLE, wait 2 s then sleep"));
-    ble_lost_processing = 0;
-    send_string(F("AT"), 100);
-    waitDoingServices(1500, 1);
-    ble_lost_processing = 0;
-  }
-
-  digitalWrite(5, LOW); // Disable this for Android 4
-  digitalWrite(6, LOW); // Disable this for Android 4
-  digitalWrite(BLEPin, LOW); // Disable this for Android 4
-
-  ble_connected = 0;
-#ifdef HW_BLE
-  hwble_connected = 0;
-#endif
-}
-
-/* ************************************************** */
-/* *** NFC handling *** */
-/* ************************************************** */
-
-// new function to fit Limitter in xBridge concept, called from loop(), formerly in setup()
-void configNFC(void)
-{
-  SPI.begin();
-
-  SPI.setDataMode(SPI_MODE0);
-  SPI.setBitOrder(MSBFIRST);
-  SPI.setClockDivider(SPI_CLOCK_DIV32);
-
-  delay(10);                      // send a wake up
-  digitalWrite(IRQPin, LOW);      // pulse to put the
-  delayMicroseconds(100);         // BM019 into SPI
-  digitalWrite(IRQPin, HIGH);     // mode
-  delay(10);
-  digitalWrite(IRQPin, LOW);
-
-  SPI.end();    // according to Bert Roode 170227. Nested SPI.begin() and SPI.end()
-}
-
-void wakeNFCUp(void)
-{
-// print_state(F(" - mem avail "));
-// Serial.print(freeMemory());
-
-  digitalWrite(NFCPin1, HIGH);
-  digitalWrite(NFCPin2, HIGH);
-  digitalWrite(NFCPin3, HIGH);
-  digitalWrite(IRQPin, HIGH);
-
-  SPI.begin();
-
-  SPI.setDataMode(SPI_MODE0);
-  SPI.setBitOrder(MSBFIRST);
-  SPI.setClockDivider(SPI_CLOCK_DIV32);
-  delay(10);
-  digitalWrite(IRQPin, LOW);
-  delayMicroseconds(100);
-  digitalWrite(IRQPin, HIGH);
-  delay(10);
-  digitalWrite(IRQPin, LOW);
-
-  NFCReady = 0;
-}
-
-void shutNFCDown(void)
-{
-// print_state(F(" - mem avail "));
-// Serial.print(freeMemory());
-
-  SPI.end();
-
-  digitalWrite(MOSIPin, LOW);
-  digitalWrite(SCKPin, LOW);
-  digitalWrite(NFCPin1, LOW); // Turn off all power sources completely
-  digitalWrite(NFCPin2, LOW); // for maximum power save on BM019.
-  digitalWrite(NFCPin3, LOW);
-  digitalWrite(IRQPin, LOW);
-}
-
-/* ****************************************************************** */
-/* port of xBridge2 code */
-/* ***************************************************************** */
-
-// send data to BLE
-void send_data(unsigned char *msg, unsigned char len)
-{
-  unsigned char i = 0;
-
-  for( i = 0; i < len; i++ )
-    ble_Serial.write(msg[i]);
-
-  // wait up to 40 chars (@9600)
-  delay(40);
-  print_state(F(" - sending: <"));
-  for ( i = 0 ; i < len ; i++ ) {
-    Serial.print(msg[i], HEX);
-    Serial.print(F(" "));
-  }
-  Serial.print(F(">"));
-  print_state(F(" - response: "));
-}
-
-// due to Arduino cast problems between String and char * use extra function
-int send_ble_string(String cmd)
-{
-  int i;
-  unsigned long now;
-  boolean timeout;
-
-  if ( !ble_connected ) {
-    for ( i = 0 ; i < 3 ; i++ ) {
-      if ( show_ble ) {
-        print_state(F(" ->("));
-        Serial.print(cmd);
-        Serial.print(F(") - "));
-      }
-
-      ble_Serial.print(cmd);
-
-      now = millis();
-      ble_answer = 0;
-      timeout = 1;
-
-      while ( (millis() - now) < 2000 ) {
-        waitDoingServices(30, 1);
-        if ( ble_answer == 2 ) {
-          ble_answer = 0;
-          timeout = 0;
-          break;
-        }
-      }
-      waitDoingServices(100, 1);
-
-      if ( timeout ) {
-        resetBLE();
-      }
-      else
-        return(1);
-    }
-    return(0);
-  }
-  return(0);
-}
-
-void send_string(String cmd, int dly)
-{
-  ble_Serial.print(cmd);
-  if ( show_ble ) {
-    print_state(F(" ->("));
-    Serial.print(cmd);
-    Serial.print(F(") - "));
-  }
-  waitDoingServices(dly, 1);
-}
-
-// send a beacon with the TXID
-void sendBeacon(void)
-{
-  //char array to store the response in.
-  unsigned char cmd_response[7];
-  //return if we don't have a connection or if we have already sent a beacon
-  //prepare the response
-  //responding with number of bytes,
-  cmd_response[0] = sizeof(cmd_response);
-  //responding to command 01,
-  cmd_response[1] = 0xF1;
-  //return the encoded TXID
-  memcpy(&cmd_response[2], &settings.dex_tx_id, sizeof(settings.dex_tx_id));
-  cmd_response[6] = DEXBRIDGE_PROTO_LEVEL;
-  send_data(cmd_response, sizeof(cmd_response));
-}
-
-int init_command_buff(t_command_buff* pCmd)
-{
-  if(!pCmd)
-    return 0;
-  memset(pCmd->commandBuffer, 0, COMMAND_MAXLEN);
-  pCmd->nCurReadPos = 0;
-  return 0;
-}
-
-//decode a command received ??
-int commandBuffIs(char* command)
-{
-  unsigned char len = strlen(command);
-  if(len != command_buff.nCurReadPos)
-    return(0);
-  return( memcmp(command, command_buff.commandBuffer, len)==0 );
-}
-
-// decode incoming serial/USB or BLE data commands
-int doCommand(void)
-{
-  // TXID packet?
-  if(command_buff.commandBuffer[1] == 0x01 && command_buff.commandBuffer[0] == 0x06)
-  {
-    memcpy(&settings.dex_tx_id, &command_buff.commandBuffer[2],sizeof(settings.dex_tx_id));
-    // send back the TXID we think we got in response
-    return(0);
-  }
-  // ACK packet?
-  if(command_buff.commandBuffer[0] == 0x02 && command_buff.commandBuffer[1] == 0xF0 && !got_ack) {
-    got_ack = 1;
-    init_command_buff(&command_buff);
-    return(0);
-  }
-  if( toupper(command_buff.commandBuffer[0]) == 'B' ) {
-    if ( show_ble ) {
-      print_state(F(" - B command - dont show BLE communication"));
-      show_ble = 0;
-    }
-    else {
-      print_state(F(" - B command - show BLE communication"));
-      show_ble = 1;
-    }
-  }
-  if( toupper(command_buff.commandBuffer[0]) == 'S' ) {
-    if ( show_state ) {
-      print_state(F(" - S command - dont show states"));
-      show_state = 0;
-    }
-    else {
-      show_state = 1;
-      print_state(F(" - S command - show states"));
-    }
-  }  // "OK+..." answer from BLE?
-  if( commandBuffIs("OK") )
-  {
-    got_ok = 1;;
-    return(0);
-  }
-  // we don't respond to unrecognised commands.
-  return(1);
-}
-
-typedef struct {
-  unsigned char pattern[8];   // pattern string including '\0'
-} t_pattern;
-
-t_pattern okstr[3] = {
-  { "OK+CONN" },  // HM-1x answer when BLE connection is active
-  { "OK+LOST" },  // HM-1x answer when BLE connection is disconnected
-  { "OK+ERRS" }   // message sent via BLE to force LBridge to send error counters via BLE
-};
-
-#ifdef HW_BLE
-// detect BLE connection status via SysLED pin on HM-1x module
-void bleConnectMonitor() {
-  //to store the time we went high
-  static unsigned long timer;
-  //to store P1_2 the last time we looked.
-  static boolean last_ble_check;
-  boolean last_ble;
-
-  last_ble = hwble_connected;
-  // if P1_2 is high, ble_connected is low, and the last_ble_check was low, sav the time and set last_ble_check.
-  if ( digitalRead(bleSysLedPin) && !hwble_connected && !last_ble_check) {
-    timer = millis();
-    last_ble_check = 1;
-  // otherwise if P1_2 goes low, and ble_connected is high, we cancel everything.
-  } else if ( !digitalRead(bleSysLedPin) ) {
-    hwble_connected =0;
-    last_ble_check = 0;
-  //otherwise, if P1_2 has been high for more than 550ms, we can safely assume we have ble_connected, so say so.
-  } else if ( digitalRead(bleSysLedPin) && last_ble_check && ((millis() - timer)>550)) {
-    hwble_connected = 1;
-  }
-
-  // show if BLE state has changed here
-  if ( last_ble != hwble_connected ) {
-    print_state(F(" - ***** HW BLE "));
-    if ( hwble_connected )
-      Serial.print(F("connected"));
-    else
-      Serial.print(F("lost"));
-  }
-}
-#endif /* HW_BLE */
-
-// detect BLE connection status via software / AT commands
-boolean monitor_ble(unsigned char b)
-{
-  static int bi = 0;
-  int i, c_found = 0;
-
-  if ( show_ble ) {
-    if ( b >= ' ' && b < 128 ) {
-      Serial.write(b);
-    }
-    else {
-      Serial.print(b, HEX);
-      Serial.print(F(" "));
-    }
-  }
-
-  for ( i = 0 ; i < 3 ; i++) {
-    if ( b == okstr[i].pattern[bi] )
-      c_found |= 1;
-  }
-
-  if ( c_found )
-    bi++;
-  else
-    bi = 0;
-
-  if ( bi == 7 ) { // one of pattern complete
-    bi = 0;
-    if ( b == okstr[0].pattern[6] ) {
-        ble_connected = 1;
-        print_state(F(" - +++++ BLE connected after "));
-        ble_connect_time = millis();
-        Serial.print( millis() - ble_start_time );
-        Serial.print(F(" ms"));
-        return(1);
-    } else if ( b == okstr[1].pattern[6] ) {
-        ble_connected = 0;
-        ble_connect_time = 0;
-        print_state(F(" - +++++ BLE lost after "));
-        Serial.print( millis() - ble_start_time );
-        Serial.print(F(" ms"));
-
-        // ??? test
-        if ( ble_lost_processing )
-          send_string(F("AT+RESET"), 500);
-
-        return(1);
-    } else if ( b == okstr[2].pattern[6] ) {
-#ifdef SEND_ERROR_COUNTERS
-        send_error_counters_via_ble = 1;
-        print_state(F(" - send error counters after next wakeup"));
-        return(1);
-#endif
-    }
-  }
-  return(0);
-}
-
-// Process any commands from BLE
-int controlProtocolService()
-{
-  static unsigned long cmd_to;
-  // ok this is where we check if there's anything happening incoming on the USB COM port or UART 0 port.
-  int nRet = 1;
-  int i;
-  unsigned char b;
-
-  //if we have timed out waiting for a command, clear the command buffer and return.
-  if(command_buff.nCurReadPos > 0 && (millis() - cmd_to) > 2000)
-  {
-    print_state(F(" - got <-["));
-    for ( i = 0 ; i  < command_buff.nCurReadPos ; i++ )
-    if ( command_buff.commandBuffer[0] >= ' ') {
-      Serial.write(command_buff.commandBuffer[i]);
-    }
-    else {
-      Serial.print(F("0x"));
-      Serial.print(command_buff.commandBuffer[i], HEX);
-      Serial.print(F(" "));
-    }
-    Serial.print(F("]"));
-    // clear command buffer if there was anything
-    init_command_buff(&command_buff);
-    return(nRet);
-  }
-  //while we have something in either buffer,
-  while( (Serial.available() || ble_Serial.available()) && command_buff.nCurReadPos < COMMAND_MAXLEN) {
-
-    if ( ble_answer == 0 ) {
-//      Serial.print("-1-");
-      ble_answer = 1;
-    }
-
-    if ( ble_Serial.available() )
-      monitor_ble( b = ble_Serial.read() );
-    else
-      b = Serial.read();
-
-    command_buff.commandBuffer[command_buff.nCurReadPos] = b;
-    command_buff.nCurReadPos++;
-    cmd_to = millis();
-    // if it is the end for the byte string, we need to process the command
-    // valid data packet or "OK" received?
-    if(      command_buff.nCurReadPos == command_buff.commandBuffer[0] \
-         || (command_buff.commandBuffer[0] == 'O' && command_buff.commandBuffer[1] == 'K' ) \
-         || (command_buff.nCurReadPos == 1 && toupper(command_buff.commandBuffer[0]) == 'B') \
-         || (command_buff.nCurReadPos == 1 && toupper(command_buff.commandBuffer[0]) == 'S') )
-     {
-      // ok we got the end of a command;
-      if(command_buff.nCurReadPos) {
-        // do the command
-        nRet = doCommand();
-        //re-initialise the command buffer for the next one.
-        init_command_buff(&command_buff);
-        // break out if we got a breaking command
-        if(!nRet)
-          return(nRet);
-      }
-    }
-    // otherwise, if the command is not up to the maximum length, add the character to the buffer.
-  }
-
-  if ( ble_answer == 1 ) {
-//    Serial.print("-2-");
-    ble_answer = 2;
-  }
-
-  if ( command_buff.nCurReadPos ){
-    //re-initialise the command buffer for the next one.
-    init_command_buff(&command_buff);
-  }
-  return(nRet);
-}
-
-// process each of the services we need to be on top of.
-// if bWithProtocol is true, also check for commands on both USB and UART
-int doServices(unsigned char bWithProtocol)
-{
-  dex_tx_id_set = (settings.dex_tx_id != 0);
-#ifdef HW_BLE
-  bleConnectMonitor();
-#endif
-  if(bWithProtocol)
-    return(controlProtocolService());
-  return(1);
-}
-
-#ifdef DYNAMIC_TXID
-//format an array to decode the dexcom transmitter name from a Dexcom packet source address.
-char SrcNameTable[32] = { '0', '1', '2', '3', '4', '5', '6', '7',
-              '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
-              'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P',
-              'Q', 'R', 'S', 'T', 'U', 'W', 'X', 'Y' };
-
-char dex_addr[6];
-
-// convert the passed uint32 Dexcom source address into an ascii string in the passed char addr[6] array.
-char *dexcom_src_to_ascii(unsigned long src)
-{
-  //each src value is 5 bits long, and is converted in this way.
-  dex_addr[0] = SrcNameTable[(src >> 20) & 0x1F];   //the last character is the src, shifted right 20 places, ANDED with 0x1F
-  dex_addr[1] = SrcNameTable[(src >> 15) & 0x1F];   //etc
-  dex_addr[2] = SrcNameTable[(src >> 10) & 0x1F];   //etc
-  dex_addr[3] = SrcNameTable[(src >> 5) & 0x1F];    //etc
-  dex_addr[4] = SrcNameTable[(src >> 0) & 0x1F];    //etc
-  dex_addr[5] = 0;  //end the string with a null character.
-  return (char *)dex_addr;
-}
-
-unsigned long asciiToDexcomSrc(char addr[6])
-{
-  // prepare a uint32 variable for our return value
-  unsigned long src = 0;
-  // look up the first character, and shift it 20 bits left.
-  src |= (getSrcValue(addr[0]) << 20);
-  // look up the second character, and shift it 15 bits left.
-  src |= (getSrcValue(addr[1]) << 15);
-  // look up the third character, and shift it 10 bits left.
-  src |= (getSrcValue(addr[2]) << 10);
-  // look up the fourth character, and shift it 50 bits left.
-  src |= (getSrcValue(addr[3]) << 5);
-  // look up the fifth character
-  src |= getSrcValue(addr[4]);
-  //printf("asciiToDexcomSrc: val=%u, src=%u\r\n", val, src);
-  return src;
-}
-
-/* getSrcValue - function to determine the encoding value of a character in a Dexcom Transmitter ID.
-Parameters:
-srcVal - The character to determine the value of
-Returns:
-uint32 - The encoding value of the character.
-*/
-unsigned long getSrcValue(char srcVal)
-{
-  unsigned char i = 0;
-
-  for(i = 0; i < 32; i++)
-  {
-    if (SrcNameTable[i]==srcVal) break;
-  }
-  //printf("getSrcVal: %c %u\r\n",srcVal, i);
-  return i & 0xFF;
-}
-#endif /* DYNAMIC_TXID */
-
-// use function instead of macro, use pointer to flag, volatile boolen <var> dont work on Arduino!!!
-int waitDoingServicesInterruptible(unsigned long wait_time, volatile boolean *break_flag, unsigned char bProtocolServices)
-{
-  unsigned long start_wait = millis();
-  while ( (millis() - start_wait ) < wait_time ) {
-    doServices(bProtocolServices);
-    if ( *break_flag ) {
-      return(1);
-    }
-    delay(20);
-  }
-  return(0);
-}
-
-void waitDoingServices(unsigned long wait_time, unsigned char bProtocolServices)
-{
-  unsigned long start_wait = millis();
-  while ( (millis() - start_wait ) < wait_time )
-  {
-    doServices(bProtocolServices);
-    delay(20);
-  }
-}
-
-// help function to read the Freestyle Libre sensor
-// in LimiTter part of loop(), moved to reuse xBridge code
-int get_nfc_reading(float *ptr)
-{
-  // store the current wixel milliseconds so we know how long we are waiting.
-  unsigned long start = millis();
-  unsigned long ms = 3000;  // try for 3 seconds, then leave
-  // set the return code to timeout indication, as it is the most likely outcome.
-  float act_glucose;
-
-  // while we haven't reached the delay......
-  while ( (millis() - start) < ms ) {
-    if (NFCReady == 0) {
-      SetProtocol_Command(); // ISO 15693 settings
-      waitDoingServices(100, 1);
-      continue;
-    }
-    else if (NFCReady == 1) {
-      for (int i=0; i<3; i++) {
-        Inventory_Command(); // sensor in range?
-        nfc_inv_cmd_count++;
-        if (NFCReady == 2)
-          break;
-        waitDoingServices(1000, 1);
-      }
-      if (NFCReady == 1) {
-        // count missed readings
-        nfc_read_errors++;
-        print_state(F(" - NFC timed out"));
-        return(0);
-      }
-    }
-    else if ( NFCReady == 2 ) {
-      act_glucose = Read_Memory();
-      nfc_scan_count++;
-#ifdef SHOW_LIMITTER
-      Serial.print(F("\r\nGlucose level: "));
-      Serial.println(act_glucose);
-      Serial.println(F("15 minutes-trend: "));
-      for (int i=0; i<16; i++)
-        Serial.println(trend[i]);
-      Serial.print(F("Battery level: "));
-      Serial.print(batteryPcnt);
-      Serial.println(F("%"));
-      Serial.print(F("Battery mVolts: "));
-      Serial.print(batteryMv);
-      Serial.println(F("mV"));
-      Serial.print(F("Sensor lifetime: "));
-      Serial.print(sensorMinutesElapse);
-      Serial.print(F(" minutes elapsed"));
-#endif /* SHOW_LIMITTER */
-      // only for showing package content
-      *ptr = act_glucose;
-      return(1);
-    }
-  }
-  // timeout
-  return(0);
-}
-
-// wait for a NFC reading and put it into Dexcom_packet
-int get_packet(Dexcom_packet* pPkt)
-{
-  // store the current wixel milliseconds so we know how long we are waiting.
-  unsigned long start = millis();
-  // set the return code to timeout indication, as it is the most likely outcome.
-  float glucose;
-
-  if ( get_nfc_reading(&glucose) ) {
-    pPkt->raw = glucose*1000;    // use C casting for conversion of float to unsigned long
-    pkt_time = millis();
-    pPkt->ms = abs_millis();
-/*
-    print_state(F(" - packet read at "));
-    Serial.print(pPkt->ms/1000);
-    Serial.print(F(" s"));
-*/
-    if ( abs_pkt_time != 0 )
-      last_abs_pkt_time = abs_pkt_time;
-    abs_pkt_time = pPkt->ms;
-/*
-    print_state(F(" - last packet read "));
-    Serial.print((abs_millis() - last_abs_pkt_time)/1000);
-    Serial.print(F(" s before"));
-*/
-    return(1);
-  }
-  else
-    pkt_time = millis();    // to avoid endless waiting if no packet can be read
-  return(0);
-}
-
-//function to format and send the passed Dexom_packet.
-int print_packet(Dexcom_packet* pPkt)
-{
-  nRawRecord msg;
-
-  //prepare the message
-  msg.size = sizeof(msg);
-  msg.cmd_code = 0x00;
-  msg.raw = pPkt->raw;
-  msg.filtered = pPkt->raw;
-  msg.dex_battery = 214;  // simulate good dexcom transmitter battery
-  msg.my_battery = batteryPcnt;
-  msg.dex_src_id = settings.dex_tx_id;
-  msg.delay = abs_millis() - pPkt->ms;
-  msg.function = DEXBRIDGE_PROTO_LEVEL; // basic functionality, data packet (with ack), TXID packet, beacon packet (also TXID ack).
-
-  if ( !ble_connected )
-    return(0);
-/*
-  print_state(F(" - sending packet with a delay of "));
-  Serial.print(msg.delay/1000);
-  Serial.print(F(" s"));
-*/
-  send_data( (unsigned char *)&msg, msg.size);
-
-  return(1);
-}
-
-// print timestamp and current status/action
-void print_state(String str)
-{
-  Serial.println(F(""));
-  Serial.print(millis());
-  Serial.print(str);
-}
-
-// main processing loop
-void loop(void)
-{
-  int i;
-  unsigned long var_sleepTime;
-  boolean evtflg1;
-  boolean evtflg2;
-  int get_packet_result;
-
-  waitDoingServices(2000, 1);
-
-  print_state(F(" - ************************"));
-  print_state(F(" - *** LBridge starting ***"));
-//  print_state(F(" - *** Version )); Serial.print(LNAME); Serial.print(F(" ***"));
-  print_state(F(" - *** mem: "));
-  Serial.print(freeMemory()); Serial.print(F(" ***"));
-  print_state(F(" - ************************"));
-
-  loop_start_time = millis();             // log the current time
-  init_command_buff(&command_buff);       //initialise the command buffer
-
-  for ( i = 0 ; i < 3 ; i++ ) {
-    if ( setupBLE() )                 // Open the UART and initialize HM-1x module
-      break;
-  }
-
-  waitDoingServices(2000, 1); // wait for possible OK+LOST
-  configBLE();                // configure the BLE module
-  configNFC();                // initialize and configure the NFC module
-
-  // wait for a BLE connection to xDrip to get a TXID if needed
-  print_state(F(" - initial wake up, waiting 40s fixed for BLE / time for a xDrip+ BT scan if needed\r\n"));
-  waitDoingServices(40000, 1);
-
-  // if BLE connected wait some time to get an answer for TXID
-  if ( ble_connected )
-    waitDoingServices(3000, 1);
-
-#ifdef DYNAMIC_TXID
-  // if dex_tx_id is zero, we do not have an ID to filter on.  So, we keep sending a beacon every 5 seconds until it is set.
-  print_state(F(" - testing TXID"));
-  settings.dex_tx_id = 0xFFFFFFFF;
-  // should we ask for a TXID?
-  if(settings.dex_tx_id >= 0xFFFFFFFF)
-    settings.dex_tx_id = 0;
-  // instead of save current TXID to EEPROM get it every time from xDrip using the Beacon mechanism
-  while(settings.dex_tx_id == 0) {
-    print_state(F(" - no TXID. Sending beacon"));
-    // wait until we have a BLE connection
-//    while(!ble_connected) doServices(1);
-    if ( !waitDoingServicesInterruptible(40000, &ble_connected, 1) && (settings.dex_tx_id != 0) )
-      break;
-    waitDoingServices(1000, 1);
-    //send a beacon packet
-    sendBeacon();
-    // set flag dex_tx_id_set
-    doServices(0);
-    //wait 5 seconds, beatify output
-    waitDoingServicesInterruptible(5000, &dex_tx_id_set, 1);
-  }
-
-  print_state(F(" - new TXID is ")); Serial.print(settings.dex_tx_id, HEX); Serial.print(F(" ("));
-  // and show first 5 char from the TXID setting in xDrips menue
-  Serial.print(dexcom_src_to_ascii(settings.dex_tx_id)); Serial.print(F(")"));
-
-#else
-  settings.dex_tx_id = 0xA5B1AE;    // -> "ABCDE" xBridge Wixel default
-#endif /* DYNAMIC_TXID */
-
-  // initialize to empty queue
-  Pkts.read = 0;
-  Pkts.write = 0;
-
-  print_state(F(" - entering main loop"));
-
-  while (1)
-  {
-    check_battery();    // check for low battery and go to sleep here if wrong
-//    print_state(F(" - wake up NFC"));
-    wakeNFCUp();
-    get_packet_result = get_packet(&Pkts.buffer[Pkts.write]);
-//    print_state(F(" - shut NFC down"));
-    shutNFCDown();
-
-    if ( sensor_oor )
-      print_state(F(" - sensor out of range"));
-
-    if( get_packet_result ) {
-      print_state(F(" - got packet, stored at position "));
-      Serial.print(Pkts.write);
-      Serial.print(F(", incrementing write to "));
-      // so increment write position for next round...
-      if ( ++Pkts.write >= DXQUEUESIZE )
-        Pkts.write = 0;
-      Serial.print(Pkts.write);
-      if (Pkts.read == Pkts.write) {
-        print_state(F(" - queue overflow, incrementing read overwriting oldest entry"));
-        if ( ++Pkts.read >= DXQUEUESIZE ) //overflow in ringbuffer, overwriting oldest entry, thus move read one up
-          Pkts.read = 0;
-      }
-
-      do_sleep = 1; // we got a packet, so we are aligned with the 5 minute interval - so go to sleep after sending out packets
-    }
-    else {
-      print_state(F(" - did not receive a pkt with "));
-      Serial.print(Pkts.write-Pkts.read);
-      Serial.print(F(" pkts in queue"));
-//      if ( ble_connected )
-//        sendBeacon();
-      do_sleep = 1; // no NFC reading, go to sleep to save battery
-    }
-
-    if ( (loop_count > 0) && !sensor_oor ) {
-//      print_state(F(" - wake up BLE"));
-      wakeBLEUp();
-    }
-
-    if ( (Pkts.read != Pkts.write) && !sensor_oor ) { // if we have a packet
-      // we wait up to 40 s for BLE connect
-      while (!ble_connected && ((millis() - pkt_time) < 40000 )) {
-        print_state(F(" - packet waiting for ble connect"));
-        if ( waitDoingServicesInterruptible(10000, &ble_connected, 1) ) {
-          print_state(F(" - connected, wait before sending the packet"));
-          waitDoingServices(2000, 1);
-        }
-      }
-
-      evtflg1 = 0;
-      evtflg2 = 0;
-      // we got a connection, so send pending packets now - at most for two minutes after the last packet was received
-      // wait enough time to empty the complete queue!
-      while ((Pkts.read != Pkts.write) && ble_connected && ((millis() - pkt_time) < ((DXQUEUESIZE+1)*1000) )) {
-        got_ack = 0;
-
-        print_packet(&Pkts.buffer[Pkts.read]);
-
-        // wait 10 s for ack
-        int j;
-        for ( j = 0 ; j < 10 ; j++ ) {
-          waitDoingServicesInterruptible(1000, &got_ack, 1);
-          if ( !ble_connected ) {
-            print_state(F(" - connection lost during wait for ack, go to sleep"));
-            break;
-          }
-        }
-
-        if (got_ack) {
-          print_state(F(" - got ack for read position "));
-          Serial.print(Pkts.read); Serial.print(F(" while write is "));
-          Serial.print(Pkts.write); Serial.print(F(", incrementing read to "));
-          if ( ++Pkts.read >= DXQUEUESIZE )
-            Pkts.read = 0;     //increment read position since we got an ack for the last package
-          Serial.print(Pkts.read);
-          if ( !evtflg1 && (Pkts.read != Pkts.write) ) {  // when still available we have a queue event
-            evtflg1 = 1;
-            queue_events++;
-          }
-#ifdef XB_NEW
-          send_data((unsigned char *)&qpkt1, (unsigned char)qpkt1.size);
-          send_data((unsigned char *)&qpkt2, (unsigned char)qpkt2.size);
-#endif
-        }
-        else {
-          print_state(F(" - no ack received, try again next wakeup"));
-          if ( !evtflg2 ) {
-            evtflg2 = 1;
-            resend_pkts_events++;   // we got no ACK, pkts to be resended
-          }
-          break;
-        }
-      }
-    }
-
-    // can't safely sleep if we didn't get an ACK, or if we are already sleeping!
-    if ( do_sleep )
-    {
-      // empty serial out buffer and open tasks
-      waitDoingServices(500, 1);
-
-      // ensure to use 300 s for a full cycle of loop() and sleeping
-      loop_time = millis() - loop_start_time;
-      if ( loop_time < 290000 )
-        var_sleepTime = (((300000 - loop_time)/1000)+4) / 8;
-      else
-        var_sleepTime = 32;
-
-      print_state(F(" - loop time ")); Serial.print(loop_time); Serial.print(F(" ms, sleep for "));
-      Serial.print(var_sleepTime*8); Serial.print(F("s"));
-      Serial.print(F(", complete loop is ")); Serial.print((loop_time+var_sleepTime*8000)/1000);
-      Serial.print(F(" s"));
-
-      shutBLEDown(1);          // cut BLE connection and switch HM-1x OFF
-
-      goToSleep(0b100001, var_sleepTime);    // mask for 8s
-
-      // waking up, power on BLE, initialze NFC
-      // count programm run time up
-      prg_run_time += (loop_time + var_sleepTime*8000)/1000;
-      loop_start_time = millis();       // set start point for loop
-      do_sleep = 0;                     // clear do_sleep, cause we have just woken up.
-      init_command_buff(&command_buff); // no BLE connection
-
-      print_state(F(" - ******************************************************************"));
-      print_state(F(" - loop ")); Serial.print(++loop_count);
-      Serial.print(F(" BLE conn errors ")); Serial.print(ble_connect_errors);
-      Serial.print(F(" run time ")); Serial.print(prg_run_time); Serial.print(F(" s"));
-      print_state(F(" - InvCmdErr ")); Serial.print(nfc_inv_cmd_errors);
-      Serial.print(F(" ProtCmdErr ")); Serial.print(nfc_prot_set_cmd_errors);
-      Serial.print(F(" ReadMem Err ")); Serial.print(nfc_read_mem_errors);
-      Serial.print(F(" NFC scans ")); Serial.print(nfc_scan_count);
-      print_state(F(" - ResndPktEvt ")); Serial.print(resend_pkts_events);
-      Serial.print(F(" QueueEvt ")); Serial.print(queue_events);
-
-      if ( loop_count > 1 ) {
-        avg_sleepTime += var_sleepTime*8;
-        bleTime += loop_time;
-      }
-      else {
-        avg_sleepTime = 0;
-        bleTime = 0;
-      }
-      print_state(F(" - avg. sleep time: "));  Serial.print(avg_sleepTime/loop_count);
-      Serial.print(F(" s, "));
-      Serial.print(F("ProMini+BLE On: "));  Serial.print((bleTime/loop_count)/1000);
-      Serial.print(F(" s"));
-
-      wakeUp();
-      waitDoingServices(250,1);
-    }
-  }
-}
-
-


Minor changes

Minor changes


Delete Matrix_Ctrlr_v1.05.ino

Delete Matrix_Ctrlr_v1.05.ino

--- Matrix_Ctrlr_v1.05.ino
+++ Matrix_Ctrlr_v1.05.ino
@@ -1,509 +0,0 @@
-
-
-/*
-  Arduino 64 AIN 128 DIN 128 DOUT ported on arduino using e-lecktronic libraries + SFC60 code
-  it uses :
-   Arduino MEGA 2560
-   32 pots
-   AINx4 module
-   32 switches
-   DINx4 module
-   32 leds
-   DOUTx4 module
-   custom 3 ports in/out MIDI shield
-   2x20 LCD compatble 44780 hitachi
-*/
-
-#include <elapsedMillis.h>
-#include <TimerOne.h>
-#include <Wire.h>
-#include <EEPROM.h>
-#include <MIDI.h>
-#include <midi_Defs.h>
-#include <midi_Namespace.h>
-#include <midi_Settings.h>
-#include <avr/pgmspace.h>
-#include <avr/wdt.h>
-#include <LiquidCrystal.h>
-#include <EEPROM24LC256_512.h>
-#include <SoftwareSerial.h>
-#include <FlexiTimer2.h>
-
-#include "arp.h"
-#include <SRIO_mxctr.h>
-#include "din.h"
-#include "midi.h"
-#include "Display.h"
-#include "livepanel.h"
-#include "device.h"
-#include "chaosmatrix.h"
-#include "ui_patch.h"
-#include "memo.h"
-#include "define.h"
-//#include "math.h"
-#include "softpanel.h"
-#include "oner.h"
-
-
-// Define various ADC prescaler
-const unsigned char PS_16 = (1 << ADPS2);
-const unsigned char PS_32 = (1 << ADPS2) | (1 << ADPS0);
-const unsigned char PS_64 = (1 << ADPS2) | (1 << ADPS1);
-const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
-
-// LCD display defines ---------
-#define firmware "1.05 "
-#define  LCD_ROWS  2
-#define  LCD_COLS  20
-LiquidCrystal lcd(44, 45, 40, 43, 41, 42); // julien proto arduino (ne marche pas en 47/48) ... pour les 2 premieres entrées A4 A5 ok
-bool appflag_updateDisplay;
-
-// used to debug various stuff
-#define DEBUG 0
-#define DEBUG_looptime 0
-#define DEBUG_analog   0
-#define DEBUG_SRIO    0
-#define DEBUG_din      0
-#define DEBUG_dout     0
-#define DEBUG_serialout 0
-#define DEBUG_LCDparamvaluedescription 0
-#define DEBUG_softpanel 0
-#define DEBUG_eeprom 0
-#define DEBUG_inteeprom 0
-#define DEBUG_encoder 0
-#define DEBUG_device 0
-#define DEBUG_midi 0
-#define DEBUG_router 0
-#define DEBUG_sysexhandler 0 // put to zero to allow bank damp without computer Serial is to long and bug the trick
-#define DEBUG_sysexbankhandler 0
-#define DEBUG_bargraph 0
-#define DEBUG_matrix 0
-#define DEBUG_master 0
-#define DEBUG_CLOCK 0
-#define DEBUG_ARP 0
-#define DEBUG_SEQ 0
-#define DEBUG_SEQTIK 0
-#define DEBUG_ARP2 0
-#define DEBUG_ARPN 0
-#define DEBUG_ARP2STEP 0
-#define DEBUG_ARP2CHORD 0
-#define DEBUG_ARPSTACK 0
-#define DEBUG_TRIG 0
-#define DEBUG_TRIG2 0
-#define DEBUG_LCD 0
-#define DEBUG_factory 0
-#define DEBUG_wizz 0
-
-#define RX1_pin 19
-#define ANALOG_INPUTS_ACTIVE 1 // set to 0 for defective unit (accident coffre 206)
-#define DIGITAL_INPUTS_ACTIVE 1
-#define SOFTSERIAL_ENABLED 1 // NOTA : SoftSerial uses nearly 1KB of RAM ! but provides 2 additional MIDI ports (see below)
-#define MIDITHRU_OFF_BOOT 1
-#define INVERT_ENCODER 1
-#define CHINESE_RED_LCD 1 // specific to red lcd from aliexpress
-#define GliGliON 1
-
-
-// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1208715493/11
-#define FASTADC 0 // à essayer à 1 plus tard quand les ain non utilisés seront reliés à la masse
-//defines for setting and clearing register bits
-#ifndef cbi
-#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
-#endif
-#ifndef sbi
-#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
-#endif
-
-byte ain_pin;
-byte ain_pin_value;
-//byte din_pin;
-//byte din_pin_value;
-byte dout_pin;
-byte dout_pin_value;
-byte Shift;
-byte Alt;
-
-
-
-bool inTest;
-
-bool RefreshSoftPanel = 0;
-bool encoder_inverted = true;
-bool mThru_XCc = false; // used for sysex/CC translator, translate if true, don't translate if false, stored in eeprom and recalled in boot()
-
-// last ain/din/dout
-unsigned char last_din_pin;
-unsigned char last_dout_pin;
-unsigned char last_encoder;
-//-------------------------------------------------------//
-//Define PINS numbers//
-//-------------------------------------------------------//
-
-// arduino ANALOG PIN :
-byte Analog1 = A0;
-byte Analog2 = A1;
-byte Analog3 = A2;
-byte Analog4 = A3;
-//byte Analog5 = A4;
-//byte Analog6 = A5;
-
-
-////Analog MUXES
-////Configure MUX adress pins S0 S1 S2 (datasheet HEF4051)
-//byte SPINS[3] = {
-//  40, 42, 44
-//};
-
-//Configure MUX OUT PIN
-byte VOUT_ANA1 = Analog1; //read pin analog0
-byte VOUT_ANA2 = Analog2; //read pin analog1
-byte VOUT_ANA3 = Analog3; //read pin analog2
-byte VOUT_ANA4 = Analog4; //read pin analog3
-//byte VOUT_ANA5 = Analog5; //read pin analog4
-//byte VOUT_ANA6 = Analog6; //read pin analog5
-
-////////////////////////////////////////////////////////////////////////////////////////////
-// VARIABLES
-////////////////////////////////////////////////////////////////////////////////////////////
-byte AnalogThreshold = 3; //threshold (6 = OK)
-//elapsedMicros timer_analog = 0;
-elapsedMicros timer_digital = 0;
-
-byte AnalogSamplingPeriod = 1; // in microsec
-unsigned int DigitalScanPeriod = 0; // 5000 in microsec
-
-int timer_display = 0;
-
-
-////MUX control array SFC_60_RevA_V1_8_no_presets
-//int muxChannel[8][3] = { // HEF4051 truth table
-//  {0, 0, 0},
-//  {1, 0, 0},
-//  {0, 1, 0},
-//  {1, 1, 0},
-//  {0, 0, 1},
-//  {1, 0, 1},
-//  {0, 1, 1},
-//  {1, 1, 1},
-//};
-
-byte controlPins[] = { // set bit to control HFE4051 S0 S1 S2
-  B00000000,
-  B10000000,
-  B01000000,
-  B11000000,
-  B00100000,
-  B10100000,
-  B01100000,
-  B11100000
-};
-
-bool InitFlag = 0;
-bool booting = 1;
-
-bool running;
-bool send_start;
-bool send_stop;
-bool send_continue;
-bool send_tick;
-///uint32_t tempo_delay;
-unsigned char bpm;
-double Pulses;
-unsigned char ui_external_clk; // external clock event tag
-bool trigger;
-bool klock;
-bool localControl;
-
-void setPin(byte outputPin)  // function to select pin on 74HC4067
-{
-  PORTA = controlPins[outputPin];
-}
-
-// Note: https://www.arduino.cc/en/Reference/SoftwareSerial
-// Not all pins on the Mega and Mega 2560 support change interrupts,
-// so only the following can be used for RX:
-// 10, 11, 12, 13,  -> used by DIN DOUT
-// 50, 51, 52, 53, -> used by SD card
-// 62(A8), 63, 64(A10), 65(A11), -> used by CV jacks (at least 64 et 65)
-// 66(A12), 67, 68, 69(A15) -> use for SoftSerial
-// alternative :
-// http://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
-
-#if SOFTSERIAL_ENABLED
-SoftwareSerial mSerial4 (A12, A13); // Rx Tx (MIDI4 in out ) A8/A9 marche tres bien
-SoftwareSerial mSerial5 (A14, A15); // Rx Tx (MIDI5 in out ) A10/A11 aussi
-#endif
-
-MIDI_CREATE_INSTANCE(HardwareSerial, Serial3, MIDI1); // RX3 TX3 port IIC 1
-MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI2); // RX2 TX2 port IIC 2
-MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI3); // RX1 TX1 port CORE OUT
-
-#if SOFTSERIAL_ENABLED // additional midi ports
-MIDI_CREATE_INSTANCE(SoftwareSerial, mSerial4, MIDI4); // RX4 TX4 port non native serial port IIC 3
-MIDI_CREATE_INSTANCE(SoftwareSerial, mSerial5, MIDI5); // RX5 TX5 port non native serial port IIC 4
-#endif
-
-using namespace midi;
-
-unsigned char INTERFACE_SERIAL;
-
-//define eeprom chips (1 per chip)
-EEPROM256_512 mem_1;
-EEPROM256_512 mem_2;
-EEPROM256_512 mem_3;
-EEPROM256_512 mem_4;
-
-elapsedMillis elapsedTime; // used for temporary message of pot movements
-unsigned int interval = 1800; // 1800 milliseconds
-
-void ResetTmpMsg() // used for temporary message of pot movements
-{
-  if (elapsedTime == interval)
-  {
-    // return to previous screen
-    SoftPanel.Mode = SoftPanel.LastMode;
-    SoftPanel.Page = SoftPanel.LastPage;
-    SoftPanel.IsNewPage = 1;
-    SoftPanel_DisplayHandler();
-  }
-
-}
-////////////////////////////////////////////////////////////////////////////////////////////
-// SETUP /////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////////////////
-void setup() {
-  delay(1000); // let arduino wake up ;)
-
-  // set up the ADC
-  ADCSRA &= ~PS_128;  // remove bits set by Arduino library
-
-  // you can choose a prescaler from above.
-  // PS_16, PS_32, PS_64 or PS_128
-  //  ADCSRA |= PS_64;    // set our own prescaler to 64 if pots 100KB
-  ADCSRA |= PS_32; // 32 prescaler if pots 10 KB
-  // ADCSRA |= PS_16; // 16 prescaler
-
-
-#if FASTADC
-  // set prescale to 16
-  sbi(ADCSRA, ADPS2) ;
-  cbi(ADCSRA, ADPS1) ;
-  cbi(ADCSRA, ADPS0) ;
-#endif
-
-  // CONFIGURE SERIAL DEBUG
-  Serial.begin(SERIAL_BUS_SPEED); // fast !
-
-  //VOUT MUX PIN CONFIG
-  pinMode(VOUT_ANA1, INPUT);
-  pinMode(VOUT_ANA2, INPUT);
-  pinMode(VOUT_ANA3, INPUT);
-  pinMode(VOUT_ANA4, INPUT);
-  //  pinMode(VOUT_ANA5, INPUT);
-  //  pinMode(VOUT_ANA6, INPUT);
-
-  pinMode(encoderClicPin, INPUT_PULLUP);
-
-  // triggers (CV gate inputs)
-  pinMode(TRIGGER_INPUT_Pin, INPUT_PULLUP); // pin trig for analog drummachine
-  pinMode(ANALOGCLOCK_Pin, INPUT_PULLUP); // pin trig for analog clock
-
-  //MUX adress PINS CONFIG
-  //  for (int i = 0; i < 3; i++) {
-  //    pinMode(SPINS[i], OUTPUT);
-  //  }
-  DDRA = B11111111; // set PORT A to outputs
-
-  //RESET PINS CONFIG
-  //  for (int i = 0; i < 3; i++) digitalWrite(SPINS[i], LOW);
-  setPin(1);
-
-  //Potentiometers init
-#if FASTADC
-  // set prescale to 16
-  sbi(ADCSRA, ADPS2) ;
-  cbi(ADCSRA, ADPS1) ;
-  cbi(ADCSRA, ADPS0) ;
-#endif
-
-  // Initialize SR in and out
-  SR.Initialize();
-  //-------------------------------------------------------//
-  //READING DIGITAL INPUTS//
-  //-------------------------------------------------------//
-  if (DIGITAL_INPUTS_ACTIVE)
-    ReadDigital();
-
-  //-------------------------------------------------------//
-  //TOGGLE SWITCHES AND DIGITAL MIDI
-  //-------------------------------------------------------//
-//  if (DIGITAL_INPUTS_ACTIVE)
-//    DIN_NotifyChange();
-
-
-  MIDI1.begin(MIDI_CHANNEL_OMNI);
-  MIDI2.begin(MIDI_CHANNEL_OMNI);
-  MIDI3.begin(MIDI_CHANNEL_OMNI);
-
-#if SOFTSERIAL_ENABLED
-  MIDI4.begin(MIDI_CHANNEL_OMNI);
-  MIDI5.begin(MIDI_CHANNEL_OMNI);
-#endif
-  // Connect the midi callbacks to the library,
-  enableMidiCallbacks();
-
-  // set up the LCD's number of rows and columns:
-  lcd.begin(20, 2);
-  lcd.clear();
-  LCD_Init(); // custom char
-
-  Timer1.initialize(20000); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second) // 20000 OK
-  Timer1.attachInterrupt( timer1Isr );   // attach the service routine
-
-  //  Timer3.initialize(100); // used for tempoClock() // was 500 // 200 ok
-  //  Timer3.attachInterrupt(timer3Isr);
-
-  // mclock
-  running = false;
-  send_start = false;
-  send_stop = false;
-  //  send_tick = false;
-  send_tick = true;
-
-  //  check_timing();
-  FlexiTimer2::set(60, 1.000 / 1440, timer_callback); // bpm = 120
-  FlexiTimer2::start();
-
-  //  attachInterrupt(digitalPinToInterrupt(RX1_pin), midiReadISR, RISING); // interrupt to midi In of the controller -> plante Wire.h
-
-  Wire.begin(); // activate i2c bus !
-  //begin EEPROM with I2C Address :
-  mem_1.begin(0, 1); //addr 0 (DEC) type 1 (defined as 24LC512)
-  mem_2.begin(1, 1); //addr 1 (DEC) type 1 (defined as 24LC512)
-  mem_3.begin(2, 1); //addr 2 (DEC) type 1 (defined as 24LC512)
-  mem_4.begin(3, 1); //addr 3 (DEC) type 1 (defined as 24LC512)
-
-  //SD.begin();
-
-  Boot();
-
-}
-
-
-////////////////////////////////////////////////////////////////////////////////////////////
-// LOOPS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
-////////////////////////////////////////////////////////////////////////////////////////////
-void loop() {
-
-#if DEBUG_looptime
-  long looptime, looptime1;
-  looptime = looptime1 = micros();
-#endif
-
-  // first, read midi !
-  MIDI3.read();  // NRT : stable 2/4/2016 // Core IN
-  MIDI1.read(); // Matrix A IN
-
-  //-------------------------------------------------------//
-  //READING AND SMOOTHING ANALOG//
-  //-------------------------------------------------------//
-  if (ANALOG_INPUTS_ACTIVE)
-    ReadAnalog();
-
-  //-------------------------------------------------------//
-  //READING DIGITAL INPUTS//
-  //-------------------------------------------------------//
-  if (DIGITAL_INPUTS_ACTIVE)
-    ReadDigital();
-
-  //-------------------------------------------------------//
-  //SAMPLING, TESTING THRESHOLD AND SENDS MIDI//
-  //-------------------------------------------------------//
-  if (ANALOG_INPUTS_ACTIVE)
-    AIN_NotifyChange();
-
-  //-------------------------------------------------------//
-  //TOGGLE SWITCHES AND DIGITAL MIDI
-  //-------------------------------------------------------//
-//  if (DIGITAL_INPUTS_ACTIVE)
-//    DIN_NotifyChange();
-
-
-  //------------------------------------------------------//
-  //READING ENCODER
-  //------------------------------------------------------//
-  encoder();
-  Encoder_Clic();
-
-  //------------------------------------------------------//
-  //READING TRIGGER (ext CV Gate)
-  //------------------------------------------------------//
-  Triggers();
-
-  //-------------------------------------------------------//
-  //RECALL INIT//
-  //-------------------------------------------------------//
-  Recall();
-
-  Display_Tick();
-  Tick();
-  ResetTmpMsg();
-  //    tempoClock();
-
-  InitFlag = 0;
-  booting = 0;
-
-#if DEBUG_looptime
-  looptime1 = micros();
-  Serial.print (F("looptime (usec) : "));
-  Serial.println(looptime1 - looptime);
-#endif
-
-}
-
-/// --------------------------
-/// Custom ISR Timer Routine
-/// --------------------------
-void timer1Isr()
-{
-  // Toggle LED
-  LivePanel_BlinkLEDs();
-  LivePanel_BlinkLFOs();
-}
-
-/////////////////////////////////////////////////////////////////////////////
-// This function is called in the mainloop when nothing else is to do
-/////////////////////////////////////////////////////////////////////////////
-void Tick(void)
-{
-  // the timing of delayed midi transmits is a low priority
-  LivePanel_HandleTransmitDelay();                 // live panel
-
-  MIDI_HandleMatrixModTransmitDelay(INTERFACE_SERIAL);        // from soft panel
-  MIDI_HandleDelayedVoiceParam(INTERFACE_SERIAL, true);             // from soft panel
-  MIDI_HandleFilterSustainTransmitDelay(INTERFACE_SERIAL);    // from live panel
-}
-
-/////////////////////////////////////////////////////////////////////////////
-// This function reboot arduino board (RISKY)
-// https://arduino103.blogspot.fr/2013/06/comment-un-reset-darduino-par-logiciel.html
-/////////////////////////////////////////////////////////////////////////////
-void software_Reboot()
-{
-  // reboot system :
-  Serial.println(F("rebooting system / software_Reboot() "));Serial.println();
-  delay(200);
-
-  // print temp message on display
-  lcd.clear();
-  lcd.setCursor(0, 0);
-  lcd.print(F(" --SYSTEM REBOOT-- "));
-
-  wdt_enable(WDTO_1S);
-
-  while (1)
-  {
-  }
-}
-
-


Indicate that slot freq is the current freq. Close #2

--- DigitalVFO.ino
+++ DigitalVFO.ino
@@ -877,6 +877,12 @@
   // write indexed item on lower row, right-justified
   lcd.setCursor(0, 1);
   lcd.write(BlankRow);
+  // show '*' if slot frequency equal to current
+  if (freq == VfoFrequency)
+  {
+    lcd.setCursor(0, 1);
+    lcd.write("*");
+  }
   lcd.setCursor(NUM_COLS - max_len, 1);
   lcd.write(slot_num + '0');
   lcd.write(": ");


Code cleanup

Code cleanup

--- Firmware/R2-Quadcopter.ino
+++ Firmware/R2-Quadcopter.ino
@@ -7,13 +7,10 @@
 #include <Adafruit_BNO055.h>
 #include <utility/imumaths.h>

-
-
 Servo motor_right;
 Servo motor_left;
 Servo motor_front;
 Servo motor_back;
-

 // PID variables
 double pid_roll_in,   pid_roll_out,   pid_roll_setpoint = 0;
@@ -23,25 +20,26 @@
 // MOTORS
 int mR, mL, mF, mB;

-//rx
+//RX
 int throttle = THROTTLE_RMIN;
 volatile int input[4];

 unsigned long timer[4];
 byte last_channel[4];

-
+//IMU variables
 float roll_angle;
 float pitch_angle;
 float yaw_angle;

 Adafruit_BNO055 bno = Adafruit_BNO055();

-PID roll_controller(&pid_roll_in, &pid_roll_out, &pid_roll_setpoint, 5.0, 1.0, 1.0, REVERSE);
+//Init PID
+PID roll_controller(&pid_roll_in, &pid_roll_out, &pid_roll_setpoint, ROLL_PID_KP, ROLL_PID_KI, ROLL_PID_KD, REVERSE);
 PID pitch_controller(&pid_pitch_in, &pid_pitch_out, &pid_pitch_setpoint, 5.0, 1.0, 1.0, REVERSE);
 PID yaw_controller(&pid_yaw_in, &pid_yaw_out, &pid_yaw_setpoint, 5.0, 1.0, 1.0, REVERSE);

-
+//Init IMU
 void bno_initialisation() {
   bno.begin();
   bno.setExtCrystalUse(true);
@@ -131,15 +129,16 @@

 void rx_read()
 {
-//  input[0] = pulseIn(A0, HIGH);//
-//  input[1] = pulseIn(A1, HIGH);//THIS is now interrupts for ARM
-//  input[2] = pulseIn(A2, HIGH);//
-//  input[3] = pulseIn(A3, HIGH);//
+  //TODO: Try with interrupts
+  input[0] = pulseIn(A0, HIGH);
+  input[1] = pulseIn(A1, HIGH);
+  input[2] = pulseIn(A2, HIGH);
+  input[3] = pulseIn(A3, HIGH);

 }

 void print_rx_values() {
-  Serial.print(input[0]);//Ici on print les valeurs en microsecondes
+  Serial.print(input[0]);//Print values in millis
   Serial.print(" - ");
   Serial.print(input[1]);
   Serial.print(" - ");
@@ -193,7 +192,7 @@
   motor_left.writeMicroseconds(mL - 100);
   motor_front.writeMicroseconds(mF - 100);
   motor_back.writeMicroseconds(mB - 100);
-  print_pitch_and_roll();
+  //print_pitch_and_roll();

 }


Fixed hellodotstar.ino with proper hardware SPI pins

--- arduino/hellodotstar/hellodotstar.ino
+++ arduino/hellodotstar/hellodotstar.ino
@@ -1,8 +1,6 @@
 #include <Adafruit_DotStar.h>
 #include <SPI.h>

-#define DATAPIN    MISO
-#define CLOCKPIN   MOSI
 #define NUMPIXELS    30  // Number of LEDs in strip

 #define RED    0xFF0000
@@ -11,13 +9,12 @@
 #define WHITE  0xFFFFFF

 uint32_t frame[NUMPIXELS];
-uint32_t colors[3] = {RED, GREEN, BLUE };
+uint32_t colors[3] = {RED, GREEN, BLUE};

-Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, DATAPIN, CLOCKPIN, DOTSTAR_BGR);
+Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, DOTSTAR_BGR);

 void setup() {
   strip.begin(); // Initialize pins for output
-  strip.show();  // Turn all LEDs off ASAP

   //initialize strip array
   for (int i = 0; i < NUMPIXELS; i++) {
@@ -25,7 +22,7 @@
     strip.setPixelColor(i, frame[i]);
   }
   strip.show();
-  delay(5000);
+  delay(1000);
   //initialize strip array
   for (int i = 0; i < NUMPIXELS; i++) {
     frame[i] = GREEN; //initialize all to GREEN


knob loop working but needs restructuring

--- arduino/modularsynth/modularsynth.ino
+++ arduino/modularsynth/modularsynth.ino
@@ -84,27 +84,30 @@
             }
           }
         }
+
+        // knob multiplexers are sharing selector pins with connection multiplexers
+        // this means this is a good place to read the analogue values
+        if(i==0 && j==0) {
+          for(m=0;m<2;m++) {
+            Serial.print("A");
+            Serial.print(m*8+k);
+            Serial.print("-");
+            Serial.print(analogRead(m));
+            Serial.print("\n");
+          }
+        }
       }
     }
   }

   //checkMidi();
-  for(byte i = 0; i < 0; i ++) {
-    checkMidi();
-    Serial.print("A");
-    Serial.print(i);
-    Serial.print("-");
-    Serial.print(analogRead(i));
-    Serial.print("\n");
-  }
-  checkMidi();
   Serial.println(gate?"G1":"G0");
   Serial.print("N");
   Serial.print(currentNote);
   Serial.print("\n");
   Serial.println("DONE");
   digitalWrite(13, gate);
-  delay(1000);
+  delay(4000);
 }

 void checkMidi() {


Example application updated

Example application updated

Example application updated

--- firmware/examples/mdns.ino
+++ firmware/examples/mdns.ino
@@ -37,6 +37,8 @@
     // If we started successfully, then wait for the client to connect
     // else do nothing
     if(isStarted){
+        mdns.update();
+
         TCPClient client = server.available();
         if (client){
             client.write(szHTML);


Mightywatt double voltage read fix

Mightywatt double voltage read fix

--- mightywatt/mightywatt.ino
+++ mightywatt/mightywatt.ino
@@ -595,7 +595,7 @@
     analogResult += analogRead(channel);
   }
   #ifdef AVR
-    return (analogResult >> 2);
+    return (analogResult >> 2) >> 1; // TODO: See why another shift is required
   #endif
   #ifdef ARM
     return (analogResult >> 4);


logging to multiple server

logging to multiple server

--- _40_esp12-random-test-udp-report/_40_esp12-random-test-udp-report.ino
+++ _40_esp12-random-test-udp-report/_40_esp12-random-test-udp-report.ino
@@ -27,6 +27,8 @@

 String clientName;
 unsigned long startMills;
+unsigned long prevMills;
+unsigned long portCount;

 WiFiClient wifiClient;
 WiFiUDP udp;
@@ -166,6 +168,8 @@
 void setup()
 {
   startMills = millis();
+  prevMills =  millis();
+  portCount = 0;
   //Serial.begin(74880);

   Serial.println("");
@@ -200,6 +204,8 @@
     payload += ESP.getFreeHeap();
     payload += "i,RSSI=";
     payload += WiFi.RSSI();
+    payload += ",delay=";
+    payload += (millis() - prevMills);

     Serial.print("payload : ");
     Serial.println(payload);
@@ -210,6 +216,8 @@
   }
   //delay(1);
   delayMicroseconds(200);
+  prevMills = millis() ;
+  portCount++;
 }

 void sendUdpSyslog(String msgtosend)
@@ -218,7 +226,11 @@
   byte* p = (byte*)malloc(msg_length);
   memcpy(p, (char*) msgtosend.c_str(), msg_length);

-  udp.beginPacket(influxdbudp, 8089);
+  if ( portCount % 2 == 0 ) {
+    udp.beginPacket(influxdbudp, 8089);
+  } else {
+    udp.beginPacket(influxdbudp, 8090);
+  }
   udp.write(p, msg_length);
   udp.endPacket();
   free(p);


Renamed Variables for Consistency

Renamed Variables for Consistency

There were a few offending global variables, fixed

--- KSPboard.ino
+++ KSPboard.ino
@@ -71,7 +71,7 @@

 // helm control variables
 int16_t pitch, yaw, roll, throttle;
-int8_t pitch_adj = 0, yaw_adj = 0, roll_adj = 0; // for ramping the direction input
+int8_t pitchAdjust = 0, yawAdjust = 0, rollAdjust = 0; // for ramping the direction input

 // op control variables
 // state == 0 to DELAY_OP-1 -> off / debounce, state == DELAY_OP -> on
@@ -208,16 +208,16 @@

 void updateHelm()
 {
-  pitch_adj = getAdjustment(PITCH_PINS, pitch_adj);
-  pitch = getDirection(pitch, pitch_adj, PITCH_STICK);
+  pitchAdjust = getAdjustment(PITCH_PINS, pitchAdjust);
+  pitch = getDirection(pitch, pitchAdjust, PITCH_STICK);
   Serial.println(pitch);

-  roll_adj = getAdjustment(ROLL_PINS, roll_adj);
-  roll = getDirection(roll, roll_adj, ROLL_STICK);
+  rollAdjust = getAdjustment(ROLL_PINS, rollAdjust);
+  roll = getDirection(roll, rollAdjust, ROLL_STICK);
   Serial.println(roll);

-  yaw_adj = getAdjustment(YAW_PINS, yaw_adj);
-  yaw = getDirection(yaw, yaw_adj, YAW_STICK);
+  yawAdjust = getAdjustment(YAW_PINS, yawAdjust);
+  yaw = getDirection(yaw, yawAdjust, YAW_STICK);
   Serial.println(yaw);

   throttle = getThrottle(PIN_THROTTLE);


horloge_cycles_ultradiens

--- files/horloge_cycles_ultradiens/horloge_cycles_ultradiens.ino
+++ files/horloge_cycles_ultradiens/horloge_cycles_ultradiens.ino
@@ -193,7 +193,7 @@
   // Conversion de “frac16eJour” en pixels
   // Les valeurs de la 1ère moitié du cycle sont affichées à droite de l’écran.
   // Les valeurs de la 2e moitié du cycle sont affichées à gauche de l’écran.
-  // Il faut donc permuter les deux moitiés pour qu’elles s’affichent au bon endroit.
+  // Le code ci-dessous permute les deux moitiés pour qu’elles s’affichent au bon endroit.
   int16_t frac16eJourPx;
   if( frac16eJour < 5400/2 )
     { frac16eJourPx = map( frac16eJour,
@@ -201,7 +201,7 @@
                               displayWidth/2, displayWidth ); }
   else
     { frac16eJourPx = map( frac16eJour,
-                              5400/2,         5399,
+                              5400/2,         5400-1,
                               0,              displayWidth/2-1 ); }

   // Calcul du pourcentage du cycle d’attention


Remove unused cruft

Remove unused cruft

--- heatpump.ino
+++ heatpump.ino
@@ -1,17 +1,11 @@
 // based on
-// https://github.com/tiegz/Spark-Core-IRremote
-// https://github.com/r45635/HVAC-IR-Control
+//    https://github.com/tiegz/Spark-Core-IRremote
+//    https://github.com/r45635/HVAC-IR-Control

 #include "IRremote.h"

-#define NUM_0 48
-#define NUM_1 49
-#define NUM_2 50
-#define ENTER 10
-
 IRsend irsend(D3);
 int led = D7;
-int msDelay = 5000;

 int run(String command) {
   int temperature = command.toInt();


fixing breaking arduino code

fixing breaking arduino code

--- arduino/OpenROV.ino
+++ arduino/OpenROV.ino
@@ -24,7 +24,6 @@

   motors.reset();
   led.reset();
-  count = 0;
 }

 void loop(){


Included Neopixles API and "Hello World"

--- arduino/arduino.ino
+++ arduino/arduino.ino
@@ -1,3 +1,4 @@
+#include <Adafruit_NeoPixel.h>
 #include <EEPROMex.h>
 #include <EEPROMVar.h>
 #include <stdint>
@@ -24,6 +25,8 @@
 #else
     #define SERIAL_BUFFER_SIZE 64
 #endif
+
+Adafruit_NeoPixel* strip;

 void setup() {
   //Beginn Serial Communication
@@ -50,7 +53,20 @@
      EEPROM.write(EEPROM_INITIALISED_FLAG, 42);

   }
-
+
+  //Create Neopixels strip and init
+  strip = &Adafruit_NeoPixel((uint16_t)60, (uint8_t) 6,(uint8_t)( NEO_GRB + NEO_KHZ800));
+  strip.begin();
+
+  //Apply red to green gradient
+  for(int i=0; i<60; i++) {
+    strip.setPixelColor(i, 255*(60-i)/60, 255*i/60, 0);
+
+  }
+
+  //show the gradient
+  strip.show();
+
   //Tell host boot is complete
   sendDone();


Adding servo management for pan/tilt

Adding servo management for pan/tilt

Servo range is 0-160 degrees. There is a hardware limitation for the moment, but when I find a shorter camera mounting screw, we can make the max 180 degrees. Note that passing the angle value of 999 resets the pan and tilt servos to their home positions.

--- Arduino/Thingamabot/Thingamabot.ino
+++ Arduino/Thingamabot/Thingamabot.ino
@@ -14,8 +14,8 @@
 // mr <value>:     Motor Right. Value from -100 to 100. 0 is off.
 // bl <value>:     Brake Left. 0 is off, other value is on.
 // br <value>:     Break Right. 0 is off, other value is on.
-// sp <value>:     Servo Pan. Value from -180 to 180.
-// st <value>:     Servo Tilt. Value from -180 to 180.
+// sp <value>:     Servo Pan. Value from 0 - 160 (eventually 180).
+// st <value>:     Servo Tilt. Value from 0 - 160 (eventually 180).
 // ka:             Keep alive. Prevent from entering fail-safe mode.
 // fs <value>:     Set fail-safe interval in milliseconds. Use 0 to disable fail-safe mode.
 // ef:             Exit fail safe. Once fail-safe mode is entered, you must
@@ -152,24 +152,39 @@
 // Values designating the servos.
 const int PAN_SERVO = 0;
 const int TILT_SERVO = 1;
-
-// Configure servo hardware.
+const int PAN_SERVO_PIN = 9;
+const int TILT_SERVO_PIN = 10;
+
+// Declare servos
+Servo pan_servo;
+Servo tilt_servo;
+
+// Configure pan/tilt servo hardware.
 void setup_servos()
 {
-    // TODO: Configure servo hardware.
-}
+  pan_servo.attach(PAN_SERVO_PIN);
+  tilt_servo.attach(TILT_SERVO_PIN);
+  pan_servo.write(0);
+  tilt_servo.write(90);
+}
+

 // Set a servo to a particular setting.
 // "servo" is which servo to set.
-// "angle" is a value from -180 to 180, in degrees
+// "angle" is a value from 0 to 160 (eventually 180), in degrees, except that 999 indicates return to home position
 void set_servo(int servoID, int angle)
 {
-    // TODO: Set servo hardware.

-    if (servoID == PAN_SERVO) {
+    if (angle == 999) {
+        pan_servo.write(0);
+        tilt_servo.write(90);
+    }
+    else if (servoID == PAN_SERVO) {
+        pan_servo.write(angle);
         log(F("Set pan servo to:"), angle);
     }
     else if (servoID == TILT_SERVO) {
+        tilt_servo.write(angle);
         log(F("Set tilt servo to:"), angle);
     }
 }
@@ -313,11 +328,11 @@
     else if (c1 == 's') {
         // "sp" - pan servo, "st" - tilt servo

-        // Clamp value to -180 to 180.
-        if (value > 180)
-            value = 180;
-        else if (value < -180)
-            value = -180;
+        // Clamp value to 0 to 160 - eventually 180.
+        if (value > 160 && value != 999)  // using 999 as value to return camera to home position
+            value = 160;
+        else if (value < 0)
+            value = 0;

         if (c2 == 'p')
             set_servo(PAN_SERVO, value);


Duplex working between vibration and microphone

--- Arduino/_nano_firmware/nanoFirmware2/nanoFirmware2.ino
+++ Arduino/_nano_firmware/nanoFirmware2/nanoFirmware2.ino
@@ -56,7 +56,7 @@

 //peak finding variables
 const int number_of_peaks = 5;
-const int noise_floor = 100;
+int noise_floor = 100;
 int biggest_peaks[number_of_peaks];
 int peak_power[FFT_N/2];

@@ -119,29 +119,26 @@
     print_peaks();
     print_max_peaks();

-    //SEND DATA TO ARDUINO-ARDUINO INTERFACE
-    //NEED INPUT FROM JIM HERE
-
-//      is_microphone = true; //set to microphone mode
-//      down_sampling_rate =1;// microphone mode
-
-    //TIME DUPLEX BETWEEN MICROPHONE AND VIBRATION SENSOR
+    //TODO: SEND DATA TO ARDUINO-ARDUINO INTERFACE
+
+
+    //ALTERNATE BETWEEN MICROPHONE AND VIBRATION SENSOR
     if (is_microphone == true){
+      //SWITCH BACK TO VIBRATION SENSOR
       is_microphone = false; //set to vibration mode
       down_sampling_rate =20;//vibration mode uses downsampling factor 20
+      ADMUX = 0x41; // change it to adc1
+      noise_floor = 50;
     }else{
+      //SWITCH BACK TO MICROPHONE
       is_microphone = true; //set to microphone mode
       down_sampling_rate =1;// microphone mode
-    }
-
-
-
-//delay(2000);
+      ADMUX = 0x40; // use adc0
+      noise_floor = 100;
+    }

 //When TIMSK0 is on:
 delay(120000);
-
-//    delay(500000);
   }
 }


2016 update

2016 update

-setup for 2016
-made title text scroll
-made on board led flash ever second

--- CountDownToGrad/CountDownToGrad.ino
+++ CountDownToGrad/CountDownToGrad.ino
@@ -2,16 +2,18 @@
 #include <SimpleTimer.h>

 //--- SETTINGS ---
-const String strTitle = "2015 GRADUATION";    // title - max 16 characters
+String strTitle = " 2016 GRADUATION";         // title - must be 16 characters, add padding to front
 const String strGrad = "CONGRATS GRADS!";     // timer done message - max 16 characters
 const int lcdPin = 2;                         // pin for LCD

 // time to count down from
-const int dMax = 29;                          // days
-const int hMax = 7;                           // hours
-const int mMax = 40;                          // minutes
+const int dMax = 74;                          // days
+const int hMax = 0;                           // hours
+const int mMax = 0;                           // minutes
 const int sMax = 0;                           // seconds
 //--- END OF SETTINGS ---
+
+const int onBoardLEDPin = 13;                 // built in led on pin 13

 // time definitions
 const double secDay = 86400;                  // seconds in a day
@@ -21,15 +23,24 @@
 // objects
 SoftwareSerial mySerial = SoftwareSerial(255, lcdPin);
 SimpleTimer myTimer;
-int timerId;                                  // reference to clock timer
+int timerId, timerId2, timerId3;                          // reference to clock timer, light timer, and title timer
+String strTime, strLine2;

 // Configure display and timer.
 void setup() {
-  pinMode(lcdPin, OUTPUT);                           // set lcdPin as output to LCD
-  mySerial.begin(9600);                              // start serial
-  timerId = myTimer.setInterval(1000, modifyClock);  // update clock every 1 sec
-  setupTime();                                       // setup time left
+  pinMode(lcdPin, OUTPUT);                                // set lcdPin as output to LCD
+  pinMode(onBoardLEDPin, OUTPUT);                         // set onBoardLEDPin as output to LCD
+  mySerial.begin(9600);                                   // start serial
+  timerId = myTimer.setInterval(1000, modifyClock);       // update clock every 1 sec
+  timerId2 = myTimer.setInterval(500, flashLED);          // flash light every 0.5 sec
+  timerId3 = myTimer.setInterval(500, updateTitle);       // update title every 0.5 sec
+  setupTime();                                            // setup time left
   delay(100);
+
+  // pad title string with 17 whitespaces in the front
+  for (int i = 0; i < 17; i++) {
+     strTitle = " " + strTitle;
+  }
 }

 // Run the timer.
@@ -39,27 +50,6 @@

 // Modify clock display.
 void modifyClock() {
-  String time = getClockTime();
-
-  if (time == "done")
-  {
-    myTimer.disable(timerId);
-    onTimerEnd();
-  }
-  else
-  {
-    mySerial.write(22);                   // no cursor
-    mySerial.write(12);                   // clear
-    mySerial.println(strTitle);           // first line, display title
-    mySerial.write(148);                  // new line
-    mySerial.print(time + " PK");         // second line, display time and initials
-    Serial.println("Time: " + time);      // console debug
-  }
-}
-
-// Get time left in DD:HH:MM:SS format.
-String getClockTime() {
-  int m = 0, h = 0, d = 0;
   double s;

   seconds--;  //update seconds
@@ -72,27 +62,80 @@
   }

   // calc days left
-  d = dMax - days;
+  int d = dMax - days;
+  strTime = getClockTime(d);

-  // calc seconds left
-  s = seconds;
+  if (d < 0) {
+    // timer done
+    myTimer.disable(timerId);
+    myTimer.disable(timerId2);
+    myTimer.disable(timerId3);
+    onTimerEnd();
+  }
+  else {
+    mySerial.write(22);                   // no cursor
+    mySerial.write(12);                   // clear
+    mySerial.println(strTitle);           // first line, display title
+    mySerial.write(148);                  // new line
+
+    int spaceCount = 16 - strTime.length() - 2;
+    String strSpace = "";
+    for (int i = 0; i < spaceCount; i++) {
+      strSpace += " ";
+    }
+
+    strLine2 = strTime + strSpace + "PK";
+
+    mySerial.print(strLine2);      // second line, display time and initials
+    Serial.println("Time: " + strTime);   // console debug
+  }
+}
+
+void flashLED() {
+  int ledState = digitalRead(onBoardLEDPin);
+  if (ledState == HIGH) {
+    digitalWrite(onBoardLEDPin, LOW);
+  }
+  else if (ledState == LOW) {
+    digitalWrite(onBoardLEDPin, HIGH);
+  }
+}
+
+void updateTitle() {
+  strTitle = strTitle.charAt(strTitle.length() - 1) + strTitle.substring(0, strTitle.length() - 1);

-  if (d < 0)
-  {
-    // timer done
-    return "done";
+  mySerial.write(22);                   // no cursor
+  mySerial.write(12);                   // clear
+  mySerial.println(strTitle);           // first line, display title
+  mySerial.write(148);                  // new line
+
+  int spaceCount = 16 - strTime.length() - 2;
+  String strSpace = "";
+  for (int i = 0; i < spaceCount; i++) {
+    strSpace += " ";
   }

+  strLine2 = strTime + strSpace + "PK";
+
+  mySerial.print(strLine2);      // second line, display time and initials
+  Serial.println("Time: " + strTime);   // console debug
+}
+
+// Get time left in DD:HH:MM:SS format.
+String getClockTime(int daysLeft) {
+  // calc seconds left
+  double s = seconds;
+
   // calc minutes left
-  m = (int)(s / 60);
+  int m = (int)(s / 60);
   s -= m * 60;

   // calc hours left
-  h = (int)(m / 60);
+  int h = (int)(m / 60);
   m -= h * 60;

   // format DD:HH:MM:SS
-  String strD = printDigits(d, true);
+  String strD = printDigits(daysLeft, true);
   String strH = printDigits(h, true);
   String strM = printDigits(m, true);
   String strS = printDigits((int)s, false);


added sensors

added sensors

--- Arduino/code/code.ino
+++ Arduino/code/code.ino
@@ -1,9 +1,12 @@
 #include <OneWire.h>
 #include <DallasTemperature.h>
+#include <dht.h>

 #define DS18B20_PIN 10
 #define LUX_PIN A1
 #define READ A0
+#define DHT_PIN A2
+#define SOUND_PIN A3

 #define RED  2 //this sets the red led pin
 #define GREEN  4 //this sets the green led pin
@@ -12,6 +15,7 @@

 OneWire oneWire(DS18B20_PIN);
 DallasTemperature sensors(&oneWire);
+dht DHT;

 void setup() {
   pinMode(RED, OUTPUT);
@@ -22,15 +26,16 @@
 }

 void loop() {
-  setBlue();
-  int b = Serial.read();
-
-  if ((char)b == 'r') {
+ setBlue();
+ int b = Serial.read();
+ if ((char)b == 'r') {
     setGreen();
+    DHT.read11(DHT_PIN);
     String json = "{'points':[[";
-    json += temperature() + "," + brightness() + "]],";
-    json += String("'name': 'sensors','columns': ['temp1', 'light_sensor', 'light']}");
+    json += temperature() + "," + DHT.temperature +  "," + DHT.humidity + "," + brightness() + "," + (1023-analogRead(SOUND_PIN)) + "]],";
+    json += String("'name': 'sensors','columns': ['temp1','temp2','humidity', 'light_sensor', 'light','sound']}");
     Serial.println(json);
+
   }
 }


Fixed some notes.

Fixed some notes.

--- ProcessingAccel101/ProcessingAccel101.ino
+++ ProcessingAccel101/ProcessingAccel101.ino
@@ -77,7 +77,7 @@
   {
     digitalWrite(ledPin, HIGH);
   }
-  // wait 5 seconds
+  // wait 50 mseconds
   delay(50);
   digitalWrite(ledPin, LOW);
 }


fixing output

fixing output

--- RocketMegaChibiAVR/RocketMegaChibiAVR.ino
+++ RocketMegaChibiAVR/RocketMegaChibiAVR.ino
@@ -1109,8 +1109,8 @@
 	dtostrf(altitude.filteredVal, 5, 2, alt);
 	char vel[8];
 	dtostrf(alt_vel.filteredVal, 5, 2, vel);
-	char prs[8];
-	dtostrf(pressure.filteredVal, 8, 2, prs);
+	char prs[10];
+	dtostrf(pressure.filteredVal, 7, 2, prs);
 	char tem[8];
 	dtostrf(temperature.filteredVal, 4, 2, tem);

@@ -1174,15 +1174,15 @@
 	if (GPS.fix)
 	{
 		char lat[16];
-		dtostrf(latitude, 10, 6, lat);
+		dtostrf(latitude, 9, 6, lat);
 		char lon[16];
-		dtostrf(longitude, 10, 6, lon);
+		dtostrf(longitude, 9, 6, lon);
 		char spd[16];
-		dtostrf(GPS.speed, 6, 2, spd);
+		dtostrf(GPS.speed, 3, 2, spd);
 		char ang[16];
-		dtostrf(GPS.angle, 6, 2, ang);
+		dtostrf(GPS.angle, 3, 2, ang);
 		char galt[16];
-		dtostrf(GPS.altitude, 6, 2, galt);
+		dtostrf(GPS.altitude, 3, 2, galt);

 		//printBuffer += "\r\nL:";
 		printBuffer += ',';
@@ -1213,8 +1213,8 @@
     dtostrf(altitude.rawVal, 5, 2, ralt);
     char rvel[8];
     dtostrf(alt_vel.rawVal, 5, 2, rvel);
-    char rprs[8];
-    dtostrf(pressure.rawVal, 8, 2, rprs);
+    char rprs[10];
+    dtostrf(pressure.rawVal, 7, 2, rprs);
     char rtem[8];
     dtostrf(temperature.rawVal, 4, 2, rtem);