<commit_before>// HeaterMeter Copyright 2011 <NAME> <<EMAIL>>
#ifndef __GRILLPID_H__
#define __GRILLPID_H__

#include "Arduino.h"
#include "grillpid_conf.h"

#define PROBE_NAME_SIZE 13

// Probe types used in probeType config
#define PROBETYPE_DISABLED 0  // do not read
#define PROBETYPE_INTERNAL 1  // read via analogRead()
#define PROBETYPE_RF12     2  // RFM12B wireless
#define PROBETYPE_TC_ANALOG  3  // Analog thermocouple, Stein[3] is mV/C

#define STEINHART_COUNT 4

#define NOISEDUMP_PIN 5

struct __eeprom_probe
{
  char name[PROBE_NAME_SIZE];
  unsigned char probeType;
  char tempOffset;
  int alarmLow;
  int alarmHigh;
  char unused1;
  char unused2;
  float steinhart[STEINHART_COUNT];  // The last one is actually Rknown
};

#define ALARM_IDX_LOW  0
#define ALARM_IDX_HIGH 1
#define ALARM_ID_TO_PROBE(id) (id / 2)
#define ALARM_ID_TO_IDX(id) (id % 2)
#define MAKE_ALARM_ID(probe, idx) (probe * 2 + idx)

class ProbeAlarm
{
public:
  ProbeAlarm(void) {};

  // Check value against Low/High
  void updateStatus(int value);
  void setLow(int value);
  void setHigh(int value);
  int getLow(void) const { return Thresholds[ALARM_IDX_LOW]; }
  int getHigh(void) const { return Thresholds[ALARM_IDX_HIGH]; }
  boolean getLowEnabled(void) const { return Thresholds[ALARM_IDX_LOW] > 0; }
  boolean getHighEnabled(void) const { return Thresholds[ALARM_IDX_HIGH] > 0; }
  boolean getLowRinging(void) const { return Ringing[ALARM_IDX_LOW]; }
  boolean getHighRinging(void) const { return Ringing[ALARM_IDX_HIGH]; }
  void setThreshold(unsigned char idx, int value);
  int getThreshold(unsigned char idx) const { return Thresholds[idx]; }
  void silenceAll(void) { setThreshold(ALARM_IDX_LOW, 0); setThreshold(ALARM_IDX_HIGH, 0); }
  int Thresholds[2];
  boolean Ringing[2];
  boolean Armed[2];
};

class TempProbe
{
private:
  const unsigned char _pin; 
  unsigned char _accumulatedCount;
  unsigned int _accumulator;
  unsigned char _probeType;  
  
public:
  TempProbe(const unsigned char pin);

  const unsigned char getPin(void) const { return _pin; }

  /* Configuration */  
  // Probe Type
  unsigned char getProbeType(void) const { return _probeType; }
  void setProbeType(unsigned char probeType);
  // Offset (in degrees) applied when calculating temperature
  char Offset;
  // Steinhart coefficients
  float Steinhart[STEINHART_COUNT];
  // Copy struct to members
  void loadConfig(struct __eeprom_probe *config);
  // Takes a Temperarure ADC value and adds it to the accumulator
  void addAdcValue(unsigned int analog_temp);

  /* Runtime Data/Methods */
  // Last averaged temperature reading
  float Temperature;
  boolean hasTemperature(void) const { return !isnan(Temperature); }
  void setTemperatureC(float T);
  // Temperature moving average 
  float TemperatureAvg;
  boolean hasTemperatureAvg(void) const { return !isnan(TemperatureAvg); }
  // Do the duty of reading ADC
  void readTemp(void);
  // Convert ADC to Temperature
  void calcTemp(void);
  
  ProbeAlarm Alarms;
};

// Indexes into the pid array
#define PIDB 0
#define PIDP 1
#define PIDI 2
#define PIDD 3

// Indexes into outputFlags bitfield
// Invert the fan PWM - pidOutput=100 would generate no PWM pulses
#define PIDFLAG_INVERT_FAN    0
// Invert servo direction - scale from max to min rather than min to max
#define PIDFLAG_INVERT_SERVO  1
// Fan only runs (at max) when pidOutput=100 (max)
#define PIDFLAG_FAN_ONLY_MAX  2
// Servo opens (to max) when pidOutput>0 (any output)
#define PIDFLAG_SERVO_ANY_MAX 3
// Try to output a constant voltage instead of PWM
#define PIDFLAG_FAN_FEEDVOLT 4

// oversampled analogRead from current freerunning ADC
unsigned int analogReadOver(unsigned char pin, unsigned char bits);

class GrillPid
{
private:
  unsigned char const _fanPin;
  unsigned char const _servoPin;
  unsigned char const _feedbackAPin;

  unsigned char _pidOutput;
  unsigned long _lastWorkMillis;
  boolean _pitTemperatureReached;
  int _setPoint;
  boolean _manualOutputMode;
  unsigned char _periodCounter;
  // Counter used for "long PWM" mode
  unsigned char _longPwmTmr;
  unsigned int _lidOpenDuration;
  // Last values used in PID calculation = B + P + I + D;
  float _pidCurrent[4];<commit_msg>[hm] Add analogReadRange() to header<commit_after>// HeaterMeter Copyright 2011 <NAME> <<EMAIL>>
#ifndef __GRILLPID_H__
#define __GRILLPID_H__

#include "Arduino.h"
#include "grillpid_conf.h"

#define PROBE_NAME_SIZE 13

// Probe types used in probeType config
#define PROBETYPE_DISABLED 0  // do not read
#define PROBETYPE_INTERNAL 1  // read via analogRead()
#define PROBETYPE_RF12     2  // RFM12B wireless
#define PROBETYPE_TC_ANALOG  3  // Analog thermocouple, Stein[3] is mV/C

#define STEINHART_COUNT 4

#define NOISEDUMP_PIN 5

struct __eeprom_probe
{
  char name[PROBE_NAME_SIZE];
  unsigned char probeType;
  char tempOffset;
  int alarmLow;
  int alarmHigh;
  char unused1;
  char unused2;
  float steinhart[STEINHART_COUNT];  // The last one is actually Rknown
};

#define ALARM_IDX_LOW  0
#define ALARM_IDX_HIGH 1
#define ALARM_ID_TO_PROBE(id) (id / 2)
#define ALARM_ID_TO_IDX(id) (id % 2)
#define MAKE_ALARM_ID(probe, idx) (probe * 2 + idx)

class ProbeAlarm
{
public:
  ProbeAlarm(void) {};

  // Check value against Low/High
  void updateStatus(int value);
  void setLow(int value);
  void setHigh(int value);
  int getLow(void) const { return Thresholds[ALARM_IDX_LOW]; }
  int getHigh(void) const { return Thresholds[ALARM_IDX_HIGH]; }
  boolean getLowEnabled(void) const { return Thresholds[ALARM_IDX_LOW] > 0; }
  boolean getHighEnabled(void) const { return Thresholds[ALARM_IDX_HIGH] > 0; }
  boolean getLowRinging(void) const { return Ringing[ALARM_IDX_LOW]; }
  boolean getHighRinging(void) const { return Ringing[ALARM_IDX_HIGH]; }
  void setThreshold(unsigned char idx, int value);
  int getThreshold(unsigned char idx) const { return Thresholds[idx]; }
  void silenceAll(void) { setThreshold(ALARM_IDX_LOW, 0); setThreshold(ALARM_IDX_HIGH, 0); }
  int Thresholds[2];
  boolean Ringing[2];
  boolean Armed[2];
};

class TempProbe
{
private:
  const unsigned char _pin; 
  unsigned char _accumulatedCount;
  unsigned int _accumulator;
  unsigned char _probeType;  
  
public:
  TempProbe(const unsigned char pin);

  const unsigned char getPin(void) const { return _pin; }

  /* Configuration */  
  // Probe Type
  unsigned char getProbeType(void) const { return _probeType; }
  void setProbeType(unsigned char probeType);
  // Offset (in degrees) applied when calculating temperature
  char Offset;
  // Steinhart coefficients
  float Steinhart[STEINHART_COUNT];
  // Copy struct to members
  void loadConfig(struct __eeprom_probe *config);
  // Takes a Temperarure ADC value and adds it to the accumulator
  void addAdcValue(unsigned int analog_temp);

  /* Runtime Data/Methods */
  // Last averaged temperature reading
  float Temperature;
  boolean hasTemperature(void) const { return !isnan(Temperature); }
  void setTemperatureC(float T);
  // Temperature moving average 
  float TemperatureAvg;
  boolean hasTemperatureAvg(void) const { return !isnan(TemperatureAvg); }
  // Do the duty of reading ADC
  void readTemp(void);
  // Convert ADC to Temperature
  void calcTemp(void);
  
  ProbeAlarm Alarms;
};

// Indexes into the pid array
#define PIDB 0
#define PIDP 1
#define PIDI 2
#define PIDD 3

// Indexes into outputFlags bitfield
// Invert the fan PWM - pidOutput=100 would generate no PWM pulses
#define PIDFLAG_INVERT_FAN    0
// Invert servo direction - scale from max to min rather than min to max
#define PIDFLAG_INVERT_SERVO  1
// Fan only runs (at max) when pidOutput=100 (max)
#define PIDFLAG_FAN_ONLY_MAX  2
// Servo opens (to max) when pidOutput>0 (any output)
#define PIDFLAG_SERVO_ANY_MAX 3
// Try to output a constant voltage instead of PWM
#define PIDFLAG_FAN_FEEDVOLT 4

// oversampled analogRead from current freerunning ADC
unsigned int analogReadOver(unsigned char pin, unsigned char bits);
// Range of the last ADC period for this pin, always 10bit
unsigned int analogReadRange(unsigned char pin);

class GrillPid
{
private:
  unsigned char const _fanPin;
  unsigned char const _servoPin;
  unsigned char const _feedbackAPin;

  unsigned char _pidOutput;
  unsigned long _lastWorkMillis;
  boolean _pitTemperatureReached;
  int _setPoint;
  boolean _manualOutputMode;
  unsigned char _periodCounter;
  // Counter used for "long PWM" mode
  unsigned char _longPwmTmr;
  unsigned int _lidOpenDuration;
  // Last values used in PID calculation = B + P + I + D;
  float _pidCurrent[4];
{'fraction_non_alphanumeric': 0.0811980811980812, 'fraction_numerical': 0.011466011466011465, 'mean_word_length': 4.47247119078105, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4986330', 'n_tokens_mistral': 2985, 'n_tokens_neox': 2836, 'n_words': 907}