Classes

  Name
class Margay
Arduino library for the Margay data logger.

Types

  Name
enum board { MODEL_0v0 = 0, MODEL_1v0 = 1, MODEL_2v0 = 2, MODEL_2v1 = 2, MODEL_2v2 = 2, MODEL_3v0 = 3}
Hardware model version of the Margay data logger.
enum build { BUILD_A = 0, BUILD_B = 1, BUILD_C = 2, BUILD_D = 3}
Build variant of the Margay data logger.
enum temp_source { RTC_temp_sensor = 1}
Temperature sensor selection for getTemp().

Defines

  Name
  MARGAY_LIBRARY_VERSION
  MARGAY_LIBRARY_COMMIT
  SKETCH_COMMIT

Types Documentation

enum board

Enumerator Value Description
MODEL_0v0 0 Margay v0.0 (prototype).
MODEL_1v0 1 Margay v1.0.
MODEL_2v0 2 Margay v2.0.
MODEL_2v1 2 Margay v2.1 (same pin map as v2.0).
MODEL_2v2 2 Margay v2.2 (same pin map as v2.0).
MODEL_3v0 3 Margay v3.0.

Hardware model version of the Margay data logger.

Pass to the Margay constructor to select the correct pin map and hardware configuration. MODEL_2v0, MODEL_2v1, and MODEL_2v2 all map to the same pin configuration (value 2).

enum build

Enumerator Value Description
BUILD_A 0 No on-board ADC.
BUILD_B 1 On-board ADC at 0x69 (MCP3421A1).
BUILD_C 2 On-board ADC at 0x6B (MCP3421A3).
BUILD_D 3 On-board ADC at 0x6A (MCP3421A2); MODEL_1v0 only.

Build variant of the Margay data logger.

Selects the I2C address of the on-board MCP3421 ADC. BUILD_A has no on-board ADC. BUILD_B uses address 0x69 (MCP3421A1), BUILD_C uses 0x6B (MCP3421A3), BUILD_D uses 0x6A (MCP3421A2).

enum temp_source

Enumerator Value Description
RTC_temp_sensor 1 DS3231 RTC internal temperature sensor.

Temperature sensor selection for getTemp().

Macros Documentation

define MARGAY_LIBRARY_VERSION

#define MARGAY_LIBRARY_VERSION "1.2.0"

define MARGAY_LIBRARY_COMMIT

#define MARGAY_LIBRARY_COMMIT ""

define SKETCH_COMMIT

#define SKETCH_COMMIT ""

Source code

/*
Margay Library
Licensed: GNU GPL v3

Written by:
Bobby Schulz
Andy Wickert
*/

#ifndef MARGAY_h
#define MARGAY_h

#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include <avr/pgmspace.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/power.h>
#include <EEPROM.h>
#include <NW_Logger.h>  // the logger core that Margay and Okapi share (NW_Sensor and NW_Pages through it)

// Build identity: this library's version (held equal to library.properties by
// NW-Tests/version_check.py) and its build commit, set by the NW-Build wrapper from
// git and blank in an Arduino IDE build; the sketch's commit the same way.
#define MARGAY_LIBRARY_VERSION "1.2.0"
#ifndef MARGAY_LIBRARY_COMMIT
#define MARGAY_LIBRARY_COMMIT ""
#endif
#ifndef SKETCH_COMMIT
#define SKETCH_COMMIT ""
#endif
#include "MCP3421.h"

enum board {
  MODEL_0v0 = 0,  
  MODEL_1v0 = 1,  
  MODEL_2v0 = 2,  
  MODEL_2v1 = 2,  
  MODEL_2v2 = 2,  
  MODEL_3v0 = 3   
};

enum build {
  BUILD_A = 0,  
  BUILD_B = 1,  
  BUILD_C = 2,  
  BUILD_D = 3   
};

enum temp_source {
  RTC_temp_sensor = 1  
};


class Margay : public NW_Logger {

public:
  Margay(board model_ = MODEL_3v0, build specs_ = BUILD_B);

  bool beginBoard(uint8_t* vals, uint8_t numVals) override;
  using NW_Logger::begin;  

  const char* name() const override {
    return "Margay";
  }
  size_t printStatus(Print& out, bool boot = false) override;

  float getVoltage();


  float getTemp(temp_source sensor = RTC_temp_sensor);

  float getBatVoltage();

  float getBatPercentage();
  float batPercentFromVoltage(float batVoltage);

  void initADC(uint8_t desiredResolution);

  void powerOB(bool state);

  void powerAux(bool state);

  // -----------------------------------------------------------------------
  // Public pin definitions
  // These are initialised by the constructor to the correct values for the
  // selected board model and build. Advanced users may read these to
  // determine hardware assignments; do not modify after begin() is called.
  // -----------------------------------------------------------------------

  uint8_t VRef_Pin = 2;        
  uint8_t BatSense_Pin = 0;    

  uint8_t VSwitch_Pin = 3;  

  uint8_t Ext3v3Ctrl = 19;  
  uint8_t PG = 18;          
  uint8_t ExtInt = 11;      
  uint8_t BatSwitch = 22;   
  uint8_t TX = 11;          
  uint8_t RX = 10;          
  uint8_t D0 = 3;           

  // -----------------------------------------------------------------------
  // Public configuration variables
  // May be modified between begin() and the first call to run().
  // -----------------------------------------------------------------------

  uint8_t nCells = 3;                                                
  float batVoltageError = 3.3;                                       
  float batPercentageWarning = 50;                                   
  float _rtcTemp = NW_ERROR;                                         
  float _batVoltage = NW_ERROR;                                      
  float _batPercent = NW_ERROR;                                      
  static constexpr const char* libVersion = MARGAY_LIBRARY_VERSION;  

protected:
  void sleepNow() override;
  void turnOffSDcard();
  void turnOnSDcard();
  void readOnBoard() override;
  // TEMPORARY (Andy, 2026-10-28): the pack's level colours the chirp and the
  // per-row blip, blue, purple or red, from the alkaline fit's percentage.
  // Placeholder thresholds until Page 1's battery block decides them; and until
  // Margay_Library#34 gives a USB discriminator, a reading below 3.4 V is not a
  // pack the board can be running from (a benched board on USB reads 3.29 V) and
  // gets no judgement: blue.
  uint32_t batteryColour();
  uint32_t chirpColour() override;
  uint32_t rowBlipColour() override;

  size_t printDataHeader(Print& out) override;

  size_t printDataRow(Print& out) override;

  uint8_t defaultAddress() const override {
    return 0;
  }

  bool wake(uint8_t address) override {
    (void)address;
    return true;
  }

  bool acquire() override {
    readOnBoard();
    return true;
  }

  size_t printNote(Print& out, bool beginFailed = false) override {
    (void)out;
    (void)beginFailed;
    return 0;
  }

  void addDataPoint() override;

  void batTest();
  void powerTest();
  void bme280Readings();

  MCP3421 adc;


  float _batteryDivider = 2.0;  //Default for v1.0

  board _model;
  build _specs;

  uint8_t chipFaults();  // Margay's chip-fault bits for Block 0: SDCard, Clock, BME280, SensorBus, Battery
  void fillPages();      // Page 2 and 3 from the logger's own readings, then endReading()
};

#endif

Updated on 2026-11-05 at 05:12:15 +0000