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 { thermistor_temp_sensor = 0, 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
thermistor_temp_sensor 0 On-board NTC thermistor (ADC-based)
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
{
    thermistor_temp_sensor = 0, 
    RTC_temp_sensor        = 1  
};


class Margay : public NW_Logger
{

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

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

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

    float getVoltage();

    void addDataPoint(String (*update)(void)) override;

    float getTemp(temp_source sensor = thermistor_temp_sensor);

    float getBatVoltage();

    float getBatPercentage();

    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 ThermSense_Pin = 1; 
    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;  
    const String LibVersion = MARGAY_LIBRARY_VERSION; 

  protected:
    float tempConvert(float V, float vcc, float R,
        float A, float B, float C, float D, float R25);
    void sleepNow() override;
    void turnOffSDcard();
    void turnOnSDcard();
    String getOnBoardVals();
    String dataHeader() override; // the data file's header row: old loggers lack the BME280
    void batTest();
    void powerTest();
    void bme280Readings();
    void _addDataPoint(String data);

    MCP3421 adc;

    float A = 0.003354016;
    float B = 0.0003074038;
    float C = 1.019153E-05;
    float D = 9.093712E-07;

    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-09-24 at 01:53:23 +0000