Lydia - Printhead
Relay.h
Go to the documentation of this file.
1 #ifndef RELAY_H
2 #define RELAY_H
3 
4 #include <ArduinoLog.h>
5 #include <osr-base.h>
6 #include <App.h>
7 #include <Component.h>
8 #include "types.h"
9 #include "../enums.h"
10 #include "../config.h"
11 #include <ModbusValue.h>
12 
13 // test relay::set <<300;2;64;300:set:1:0>>
14 // test relay::info <<300;2;64;300:info:1:0>>
15 // test relay::info <<300;2;64;300:info:1:0>>
16 
17 class Relay : public Component, public ModbusValue<bool>
18 {
19 
20 public:
22  Component *owner,
23  short _pin,
24  short _id,
25  short _addr,
26  short _val = 0) : ModbusValue<bool>(_addr, MB_FC::MB_FC_READ_COILS),
27  Component("MB_Relay", _id, Component::COMPONENT_DEFAULT, owner),
28  pin(_pin),
29  value(_val)
30  //registerMode(MB_REGISTER_MODE::E_MB_REGISTER_MODE_READ_WRITE)
31  {
32  //setFlag(OBJECT_RUN_FLAGS::E_OF_DEBUG);
33  SBI(nFlags, OBJECT_NET_CAPS::E_NCAPS_MODBUS);
34  setRegisterMode(MB_REGISTER_MODE::E_MB_REGISTER_MODE_READ_WRITE);
35  }
36 
37  short info(short val0, short val1)
38  {
39  Log.verboseln("Relay::info - Pin=%d | Key=%d | Addr=%d | Val=%d | NetVal=%d ", pin, id, addr, value, netVal());
40  return E_OK;
41  }
42  short debug()
43  {
44  return info(0,0);
45  }
46 
47  short setup()
48  {
49 #ifdef PLATFORM_PORTENTA_H7_M7
50  /*
51  digital_outputs.setLatch();
52  // Uncomment this line to set over current behavior of all
53  // channels to auto retry mode instead of latch mode:
54  // digital_outputs.setRetry();
55  digital_outputs.set(pin, LOW);
56  */
57 #endif
58  return E_OK;
59  }
60 
61  short set(int val)
62  {
63  value = val;
64 #ifdef PLATFORM_PORTENTA_H7_M7
65  if (_value == 1)
66  {
67  digital_outputs.set(pin, HIGH);
68  Log.verbose(F("Relay:set pin=%d value=%d" CR), pin, _value);
69  }
70  else
71  {
72  digital_outputs.set(pin, LOW);
73  Log.verbose(F("Relay:set pin=%d value=%d" CR), pin, _value);
74  }
75 #endif
76 
77 #ifdef PLATFORM_CONTROLLINO_MEGA
78  digitalWrite(pin, val ? HIGH : LOW);
79 #endif
80  if (netVal() != value)
81  {
82  onSet(val);
83  }
84  return val;
85  }
86 
87  short onRegisterMethods(Bridge *bridge)
88  {
89  bridge->registerMemberFunction(id, this, C_STR("set"), (ComponentFnPtr)&Relay::set);
90  bridge->registerMemberFunction(id, this, C_STR("info"), (ComponentFnPtr)&Relay::info);
91  bridge->registerMemberFunction(id, this, C_STR("setFlag"), (ComponentFnPtr)&Relay::setFlag);
92  //bridge->registerMemberFunction(id, this, C_STR("clearFlag"), (ComponentFnPtr)&Relay::clearFlag);
93  return E_OK;
94  }
95 
96  short loop()
97  {
98  bool val = netVal();
99  if (val != value)
100  {
101  set(val);
102  }
103  return E_OK;
104  }
105 
106  short pin;
107  bool value;
108 };
109 
110 #endif
Relay
Definition: Relay.h:17
Relay::debug
short debug()
Definition: Relay.h:42
Relay::value
bool value
Definition: Relay.h:107
Relay::Relay
Relay(Component *owner, short _pin, short _id, short _addr, short _val=0)
Definition: Relay.h:21
Relay::pin
short pin
Definition: Relay.h:106
Relay::set
short set(int val)
Definition: Relay.h:61
Relay::loop
short loop()
Definition: Relay.h:96
Relay::info
short info(short val0, short val1)
Definition: Relay.h:37
Relay::setup
short setup()
Definition: Relay.h:47
E_OK
#define E_OK
Definition: enums.h:11
Relay::onRegisterMethods
short onRegisterMethods(Bridge *bridge)
Definition: Relay.h:87