Skip to content
Snippets Groups Projects
MAX31856.hpp 5.22 KiB
Newer Older
fxk8y's avatar
fxk8y committed
#pragma once

// C++ system level
// #include <functional>

// ESP32 specific
// #include "esp_log.h"

// project specific
#include <SpiderLib.hpp>

// qthing stuff
// #include <qthing>

// misc
// #include ""


namespace MAX31856 {


  enum PowerlineFrequencyRejection : u8 {
    F60Hz = 0,
    F50Hz = 1
  };

  enum ThermocoupleType : u8 {
    B_Type = 0x00,
    E_TYPE = 0x01,
    J_TYPE = 0x02,
    K_TYPE = 0x03,
    N_TYPE = 0x04,
    R_TYPE = 0x05,
    S_TYPE = 0x06,
    T_TYPE = 0x07
  };


fxk8y's avatar
fxk8y committed
  // #################################
  // ###                           ###
  // ###   Register Declarations   ###
  // ###                           ###
  // #################################

fxk8y's avatar
fxk8y committed
  union CR0 {

    struct __attribute__((packed)) {
      u8 PWRLNRJ  : 1;  // Power Line Rejection Mode  (0 = 60Hz; 1 = 50Hz)
      u8 FAULTCLR : 1;
      u8 FAULT    : 1;
      u8 CJ       : 1;
      u8 OCFAULT  : 2;
      u8 ONESHOT  : 1;
      u8 CMODE    : 1;
    };

    u8 value = 0x00;

    static constexpr u8 addr = 0x00;

  };


  union CR1 {

    struct __attribute__((packed)) {
      u8 TCTYPE     : 4;  // see enum: ThermocoupleType
      u8 AVGSEL     : 3;
      u8 _reserved_ : 1;
    };

    u8 value = 0x00;

    static constexpr u8 addr = 0x01;

  };


  union MASK {

    struct __attribute__((packed)) {
fxk8y's avatar
fxk8y committed
      u8 OpenFAULTMask   : 1;
      u8 OVUVFAULTMask   : 1;
      u8 TCLowFAULTMask  : 1;
      u8 TCHighFAULTMask : 1;
      u8 CJLowFAULTMask  : 1;
      u8 CJHighFAULTMask : 1;
      u8 _reserved_      : 2;      
fxk8y's avatar
fxk8y committed
    };

    u8 value = 0x00;

    static constexpr u8 addr = 0x02;

  };


  union CJHF {

    struct __attribute__((packed)) {
    };

    u8 value = 0x00;

    static constexpr u8 addr = 0x03;

  };


  union CJLF {

    struct __attribute__((packed)) {
    };

    u8 value = 0x00;

    static constexpr u8 addr = 0x04;

  };


  union LTHFTH {

    struct __attribute__((packed)) {
    };

    u8 value = 0x00;

    static constexpr u8 addr = 0x05;

  };

  union LTHFTL {

    struct __attribute__((packed)) {
    };

    u8 value = 0x00;

    static constexpr u8 addr = 0x06;

  };

  union LTLFTH {

    struct __attribute__((packed)) {
    };

    u8 value = 0x00;

    static constexpr u8 addr = 0x07;

  };

  union LTLFTL {

    struct __attribute__((packed)) {
    };

    u8 value = 0x00;

    static constexpr u8 addr = 0x08;

  };

fxk8y's avatar
fxk8y committed
  union CJTO {

    struct __attribute__((packed)) {
    };

    u8 value = 0x00;

    static constexpr u8 addr = 0x09;

  };

  union CJTH {
fxk8y's avatar
fxk8y committed

fxk8y's avatar
fxk8y committed
    struct __attribute__((packed)) {
    };

    u8 value = 0x00;

    static constexpr u8 addr = 0x0A;
fxk8y's avatar
fxk8y committed

fxk8y's avatar
fxk8y committed
  };

  union CJTL {

    struct __attribute__((packed)) {
    };
fxk8y's avatar
fxk8y committed

fxk8y's avatar
fxk8y committed
    u8 value = 0x00;
fxk8y's avatar
fxk8y committed

fxk8y's avatar
fxk8y committed
    static constexpr u8 addr = 0x0B;
fxk8y's avatar
fxk8y committed

fxk8y's avatar
fxk8y committed

fxk8y's avatar
fxk8y committed
  union LTCBH {
    // Info: Read only!
fxk8y's avatar
fxk8y committed

fxk8y's avatar
fxk8y committed
    struct __attribute__((packed)) {
    };
fxk8y's avatar
fxk8y committed

fxk8y's avatar
fxk8y committed
    u8 value = 0x00;
fxk8y's avatar
fxk8y committed

fxk8y's avatar
fxk8y committed
    static constexpr u8 addr = 0x0C;
fxk8y's avatar
fxk8y committed

fxk8y's avatar
fxk8y committed

fxk8y's avatar
fxk8y committed
  union LTCBM {
    // Info: Read only!
fxk8y's avatar
fxk8y committed

fxk8y's avatar
fxk8y committed
    struct __attribute__((packed)) {
    };
fxk8y's avatar
fxk8y committed

fxk8y's avatar
fxk8y committed
    u8 value = 0x00;
fxk8y's avatar
fxk8y committed

fxk8y's avatar
fxk8y committed
    static constexpr u8 addr = 0x0D;
fxk8y's avatar
fxk8y committed

fxk8y's avatar
fxk8y committed

fxk8y's avatar
fxk8y committed
  union LTCBL {
    // Info: Read only!
fxk8y's avatar
fxk8y committed

    struct __attribute__((packed)) {
    };

    u8 value = 0x00;

fxk8y's avatar
fxk8y committed
    static constexpr u8 addr = 0x0E;
fxk8y's avatar
fxk8y committed

  };

fxk8y's avatar
fxk8y committed
  union SR {
    // Info: Read only!

    struct __attribute__((packed)) {
fxk8y's avatar
fxk8y committed
      bool OPEN    : 1;
      bool OVUV    : 1;
      bool TCLOW   : 1;
      bool TCHIGH  : 1;
      bool CJLOW   : 1;
      bool CJHIGH  : 1;
      bool TCRANGE : 1;
      bool CJRANGE : 1;
fxk8y's avatar
fxk8y committed
    };

    u8 value = 0x00;

    static constexpr u8 addr = 0x0F;
fxk8y's avatar
fxk8y committed

fxk8y's avatar
fxk8y committed
  };

  // #################################
  // #################################
fxk8y's avatar
fxk8y committed



  class SoftSPI {
    public:

      SoftSPI(u8 miso, u8 mosi, u8 sclk, u8 cs);

      void writeU8(u8 addr, u8 value);
      u8 readU8(u8 addr);

      const u8 cs;
      const u8 mosi;
      const u8 miso;
      const u8 sclk;


    protected:

      void setMOSI(bool value);
      void setSCLK(bool value);
      void setCS(bool value);

      bool getMISO();

      SpiderLib::ManagedMutex mutex;

  };



  class MAX31856 {

    public:

      MAX31856(u8 miso, u8 mosi, u8 sclk, u8 cs);

      // Methods below are semi-public: From a hidden-box model perspective they should be protected
      // but from my POV they can be public as the user CANNOT misuse them in any way
      // TODO: elaborate on this…

      u8 readAddr ();
      u8 writeAddr();


      void                        setPwrLnRjFrq(PowerlineFrequencyRejection pfr);  // TODO: Better Name…?!
      PowerlineFrequencyRejection getPwrLnRjFrq();                                 // TODO: Better Name…?!

fxk8y's avatar
fxk8y committed
      void             setThermocoupleType(ThermocoupleType tc);
      ThermocoupleType getThermocoupleType();

fxk8y's avatar
fxk8y committed
      bool isTCOpenCircuit();  // fault bit
      bool isTCOVUV();  // fault bit
      bool isTCLOW();  // fault bit
      bool isTCHIGH();  // fault bit
      bool isCJLOW();  // fault bit
      bool isCJHIGH();  // fault bit
      bool isTCRANGE();  // fault bit
      bool isCJRANGE();  // fault bit
fxk8y's avatar
fxk8y committed


      void mainLoop();

      const u8 cs;
      const u8 mosi;
      const u8 miso;
      const u8 sclk;

    protected:


      SoftSPI* spi;



      // bool getIRQState();

      //void mainLoop();

      SpiderLib::ManagedMutex mutex;

      SpiderLib::Util::LambdaTask* loopTask = NULL;

  };

}