Lydia - Printhead
POT.h
Go to the documentation of this file.
1 #ifndef POT_H
2 #define POT_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 
12 #ifdef PLATFORM_PORTENTA_H7_M7
13 #include <Arduino_MachineControl.h>
14 using namespace machinecontrol;
15 float res_divider = 0.28057;
16 float reference = 24;
17 #endif
18 // test relay::info <<400;2;64;400:info:1:0>>
19 // test relay::clearFlag <<400;2;64;400:clearFlag:1:0>>
20 
21 class POT : public Component, public ModbusValue<int>
22 {
23 
24 public:
25  POT(
26  Component *owner,
27  short _pin,
28  short _id,
29  short _addr) : ModbusValue<int>(_addr, MB_FC::MB_FC_READ_REGISTERS),
30  Component("POT", _id, Component::COMPONENT_DEFAULT, owner),
31  pin(_pin),
32  last_value(0)
33  //registerMode(MB_REGISTER_MODE::E_MB_REGISTER_MODE_NONE)
34  {
35  SBI(nFlags, OBJECT_NET_CAPS::E_NCAPS_MODBUS);
36  setRegisterMode(MB_REGISTER_MODE::E_MB_REGISTER_MODE_READ);
37  }
38  short setup()
39  {
40 #ifdef PLATFORM_PORTENTA_H7_M7
41  analogReadResolution(16);
42  analog_in.set0_10V();
43 #endif
44  last_value = loop();
45  return E_OK;
46  }
47 
48  short debug()
49  {
50 // Log.verbose(F("POT:debug pin=%d value=%d" CR), pin, value);
51 #ifdef PLATFORM_PORTENTA_H7_M7
52  Log.verbose(F("POT:debug pin=%d value=%d | %d" CR), pin, value, analog_in.read(pin));
53 #else
54  info(0, 0);
55 #endif
56  return E_OK;
57  }
58  short info(short val0, short val1)
59  {
60  Log.verboseln("POT::info - Pin=%d | Key=%d | Addr=%d | Val=%d | NetVal=%d ", pin, id, addr, value, netVal());
61  return E_OK;
62  }
63  short loop()
64  {
65  if (now - last < ANALOG_POT_READ_INTERVAL)
66  {
67  return E_OK;
68  }
69  last = now;
70 #ifdef PLATFORM_PORTENTA_H7_M7
71  float raw_voltage_ch0 = analog_in.read(pin);
72  float voltage_ch0 = (raw_voltage_ch0 * reference) / 65535 / res_divider;
73  value = voltage_ch0;
74 #else
75  value = analogRead(pin);
76 #endif
77  onSet(value);
78  return value;
79  }
80 
81  short onRegisterMethods(Bridge *bridge)
82  {
83  bridge->registerMemberFunction(id, this, C_STR("setFlag"), (ComponentFnPtr)&POT::setFlag);
84  //bridge->registerMemberFunction(id, this, C_STR("clearFlag"), (ComponentFnPtr)&POT::clearFlag);
85  bridge->registerMemberFunction(id, this, C_STR("info"), (ComponentFnPtr)&POT::info);
86  return E_OK;
87  }
88  short pin;
89  int value;
91 };
92 
93 #endif
POT::pin
short pin
Definition: POT.h:88
POT::setup
short setup()
Definition: POT.h:38
POT::info
short info(short val0, short val1)
Definition: POT.h:58
POT::value
int value
Definition: POT.h:89
POT
Definition: POT.h:21
ANALOG_POT_READ_INTERVAL
#define ANALOG_POT_READ_INTERVAL
Definition: config_adv.h:110
POT::onRegisterMethods
short onRegisterMethods(Bridge *bridge)
Definition: POT.h:81
POT::POT
POT(Component *owner, short _pin, short _id, short _addr)
Definition: POT.h:25
E_OK
#define E_OK
Definition: enums.h:11
POT::debug
short debug()
Definition: POT.h:48
POT::loop
short loop()
Definition: POT.h:63
POT::last_value
int last_value
Definition: POT.h:90