Lydia - Printhead
3PosAnalog.h
Go to the documentation of this file.
1 #ifndef POS3_ANALOG_H
2 #define POS3_ANALOG_H
3 
4 #include <ArduinoLog.h>
5 #include "../config.h"
6 #include <osr-base.h>
7 #include <App.h>
8 #include <Component.h>
9 #include "types.h"
10 #include <xmath.h>
11 #include <ModbusValue.h>
12 
13 class Pos3Analog : public Component, public ModbusValue<int>
14 {
15 public:
17  {
18  UP = 1,
19  MIDDLE = 0,
20  DOWN = 2,
21  INVALID = 10
22  };
23 
25  Component *owner,
26  short _upPin, short _downPin,
27  short _id,
28  short _addr) : ModbusValue<int>(_addr, MB_FC::MB_FC_READ_REGISTERS),
29  Component("Pos3Analog", _id, Component::COMPONENT_DEFAULT, owner),
30  upPin(_upPin),
31  downPin(_downPin)
32  //registerMode(MB_REGISTER_MODE::E_MB_REGISTER_MODE_READ)
33  {
34  // setFlag(OBJECT_RUN_FLAGS::E_OF_DEBUG);
35  SBI(nFlags, OBJECT_NET_CAPS::E_NCAPS_MODBUS);
36  setRegisterMode(MB_REGISTER_MODE::E_MB_REGISTER_MODE_READ);
37  }
38 
39  short setup()
40  {
41  last_switch = read();
42  return E_OK;
43  }
44 
45  short info(short val0, short val1)
46  {
47  Log.verboseln("3PosAnalog::info - Pin-0=%d | Pin-1=%d | Key=%d | Addr=%d | Val=%d | NetVal=%d ", upPin, downPin, id, addr, value, netVal());
48  return E_OK;
49  }
50  short debug()
51  {
52  return info(0, 0);
53  }
54 
55  short loop()
56  {
57  if (now - last < ANALOG_SWITCH_READ_INTERVAL)
58  {
59  return E_OK;
60  }
61  last = now;
62  int newDirection = read();
63  if (newDirection != value)
64  {
66  }
67  value = newDirection;
68  onSet(value);
69  return value;
70  }
71 
72  int last_switch = -1; // last switch position
73  int value = -1; // current switch position
74  short upPin;
75  short downPin;
76 
77  short onRegisterMethods(Bridge *bridge)
78  {
79  bridge->registerMemberFunction(id, this, C_STR("setFlag"), (ComponentFnPtr)&Pos3Analog::setFlag);
80  //bridge->registerMemberFunction(id, this, C_STR("clearFlag"), (ComponentFnPtr)&Pos3Analog::clearFlag);
81  bridge->registerMemberFunction(id, this, C_STR("info"), (ComponentFnPtr)&Pos3Analog::info);
82  return E_OK;
83  }
84 
85 private:
86  int read()
87  {
88  bool up = RANGE(analogRead(upPin), ANALOG_INPUT_MIN_THRESHOLD_0, 1000);
89  bool down = RANGE(analogRead(downPin), ANALOG_INPUT_MIN_THRESHOLD_0, 1000);
90  int newDirection = 0;
91  if (up)
92  {
93  newDirection = POS3_DIRECTION::DOWN;
94  }
95  if (down)
96  {
97  newDirection = POS3_DIRECTION::UP;
98  }
99  if (!up && !down)
100  {
101  newDirection = POS3_DIRECTION::MIDDLE;
102  }
103  if (up && down)
104  {
105  newDirection = POS3_DIRECTION::INVALID;
106  }
107  return newDirection;
108  }
109 
110  bool changed()
111  {
112  return last_switch != value;
113  }
114 
115  bool clear()
116  {
117  last_switch = value;
118  return E_OK;
119  }
120 
121 #ifdef HAS_MODBUS_REGISTER_DESCRIPTIONS
122  String getRegisterDescription(short reg)
123  {
124  char buff[255];
125  short s = snprintf(buff, 255, "- Read Position : Address=%d(%X) -> [Up:%d Middle:%d Down:%d]", addr, addr, POS3_DIRECTION::UP, POS3_DIRECTION::MIDDLE, POS3_DIRECTION::DOWN);
126  // snprintf (buff +s, 255 , "- Read Position : Address=%d(%X) -> [Up:%d Middle:%d Down:%d] ", addr, addr, POS3_DIRECTION::UP, POS3_DIRECTION::MIDDLE, POS3_DIRECTION::DOWN);
127  return String(buff);
128  }
129 #endif
130 };
131 
132 #endif
Pos3Analog::MIDDLE
@ MIDDLE
Definition: 3PosAnalog.h:19
Pos3Analog
Definition: 3PosAnalog.h:13
ANALOG_INPUT_MIN_THRESHOLD_0
#define ANALOG_INPUT_MIN_THRESHOLD_0
Definition: config_adv.h:119
Pos3Analog::INVALID
@ INVALID
Definition: 3PosAnalog.h:21
Pos3Analog::setup
short setup()
Definition: 3PosAnalog.h:39
Pos3Analog::value
int value
Definition: 3PosAnalog.h:73
ANALOG_SWITCH_READ_INTERVAL
#define ANALOG_SWITCH_READ_INTERVAL
Definition: config_adv.h:111
Pos3Analog::upPin
short upPin
Definition: 3PosAnalog.h:74
Pos3Analog::Pos3Analog
Pos3Analog(Component *owner, short _upPin, short _downPin, short _id, short _addr)
Definition: 3PosAnalog.h:24
Pos3Analog::info
short info(short val0, short val1)
Definition: 3PosAnalog.h:45
Pos3Analog::debug
short debug()
Definition: 3PosAnalog.h:50
Pos3Analog::read
int read()
Definition: 3PosAnalog.h:86
Pos3Analog::POS3_DIRECTION
POS3_DIRECTION
Definition: 3PosAnalog.h:16
Pos3Analog::loop
short loop()
Definition: 3PosAnalog.h:55
Pos3Analog::clear
bool clear()
Definition: 3PosAnalog.h:115
E_OK
#define E_OK
Definition: enums.h:11
Pos3Analog::onRegisterMethods
short onRegisterMethods(Bridge *bridge)
Definition: 3PosAnalog.h:77
Pos3Analog::downPin
short downPin
Definition: 3PosAnalog.h:75
Pos3Analog::DOWN
@ DOWN
Definition: 3PosAnalog.h:20
Pos3Analog::getRegisterDescription
String getRegisterDescription(short reg)
Definition: 3PosAnalog.h:122
Pos3Analog::changed
bool changed()
Definition: 3PosAnalog.h:110
Pos3Analog::last_switch
int last_switch
Definition: 3PosAnalog.h:72
Pos3Analog::UP
@ UP
Definition: 3PosAnalog.h:18