VLCB
Loading...
Searching...
No Matches
Controller.h
Go to the documentation of this file.
1// Copyright (C) Sven Rosvall (sven@rosvall.ie)
2// This file is part of VLCB-Arduino project on https://github.com/SvenRosvall/VLCB-Arduino
3// Licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
4// The full licence can be found at: http://creativecommons.org/licenses/by-nc-sa/4.0/
5
6#pragma once
7
8#ifndef DEBUG_SERIAL
9#define DEBUG_SERIAL Serial
10#endif
11
12#include "initializer_list.h"
13#include "ArrayHolder.h"
14#include "CircularBuffer.h"
15#include "Configuration.h"
16#include "TimedResponse.h"
17
18namespace VLCB
19{
20
21// Size of the action queue.
22// Testing shows that the queue often gets up to 4 elements.
23// Set size to 8 to be on the safe side.
24const int ACTION_QUEUE_SIZE = 8;
25
38{
39 uint8_t len; // Value 0-7 or FF for messages handled in CanTransport
40 uint8_t data[8];
41
42 VlcbMessage() = default;
43
45 VlcbMessage(uint8_t len, uint8_t data[])
46 : len(len)
47 {
48 memcpy(this->data, data, len);
49 }
50
53 : len(1)
54 {
55 data[0] = opc;
56 }
57
59 VlcbMessage & addData(byte b);
61 VlcbMessage & add2Bytes(unsigned int n);
63 VlcbMessage & addNN(unsigned int nn) { return add2Bytes(nn); }
65 VlcbMessage & addEN(unsigned int en) { return add2Bytes(en); }
67 VlcbMessage & addNNEN(byte nn_en[EE_HASH_BYTES]);
68};
69
83
85struct Action
86{
88 union
89 {
91 bool fromENUM;
93 };
94
95 Action() = default;
96
98 : actionType(type)
99 , fromENUM(false) // Must include one item from the union.
100 {}
101
102 Action(ACTION type, bool fromENUM)
103 : actionType(type), fromENUM(fromENUM)
104 {}
105
106 Action(ACTION type, const VlcbMessage & msg)
107 : actionType(type), vlcbMessage(msg)
108 {}
109
111 : actionType(type), mode(mode)
112 {}
113};
114
115class Service;
116
117//
119//
121{
122public:
123// Controller();
125// Controller(std::initializer_list<Service *> services);
127
129
130 Configuration * getModuleConfig() const { return module_config; }
131
132 void setName(const char *mname) { module_config->setName(mname); }
133
134 const ArrayHolder<Service *> & getServices() { return services; }
135
136 void updateParamFlags();
137 void setParamFlag(VlcbParamFlags flag, bool set);
138 Parameters & getParams() { return module_config->getParams(); }
139 unsigned char getParam(VlcbParams param) const { return module_config->getParam(param); }
140
141 bool sendMessage(const VlcbMessage &msg);
142 bool sendMessage(const VlcbMessage *msg);
143
144 void begin();
145 inline bool sendMessageWithNN(VlcbOpCodes opc);
146 inline bool sendMessageWithNN(VlcbOpCodes opc, byte b1);
147 inline bool sendMessageWithNN(VlcbOpCodes opc, byte b1, byte b2);
148 inline bool sendMessageWithNN(VlcbOpCodes opc, byte b1, byte b2, byte b3);
149 inline bool sendMessageWithNN(VlcbOpCodes opc, byte b1, byte b2, byte b3, byte b4);
150 inline bool sendMessageWithNN(VlcbOpCodes opc, byte b1, byte b2, byte b3, byte b4, byte b5);
151 bool sendWRACK();
152 bool sendCMDERR(byte cerrno);
153 void sendGRSP(VlcbOpCodes opCode, byte serviceType, byte errCode);
154 void sendDGN(byte serviceIndex, byte diagCode, unsigned int counter);
155
156 byte getModuleCANID() const { return module_config->CANID; }
157 void process();
158 void indicateMode(VlcbModeParams mode);
159 void indicateActivity();
160
161 void putAction(const Action & action);
162 void putAction(ACTION action);
163 bool pendingAction();
164 bool pendingTasks();
165
166 void messageActedOn();
167 unsigned int getMessagesActedOn() { return diagMsgsActed; }
168
169 const CircularBuffer<Action, ACTION_QUEUE_SIZE> & getActionQueue() const { return actionQueue; }
170
172
173private:
174 Configuration *module_config;
175 ArrayHolder<Service *> services;
176
178 TimedResponse timedResponses;
179
180 bool sendMessageWithNNandData(VlcbOpCodes opc) { return sendMessageWithNNandData(opc, 0, 0); }
181 bool sendMessageWithNNandData(VlcbOpCodes opc, int len, ...);
182
183 // Really an MNS diagnostic but placed here as its data is collected across all services.
184 unsigned int diagMsgsActed = 0;
185};
186
188{
189 return sendMessageWithNNandData(opc);
190}
191
193{
194 return sendMessageWithNNandData(opc, 1, b1);
195}
196
198{
199 return sendMessageWithNNandData(opc, 2, b1, b2);
200}
201
202bool Controller::sendMessageWithNN(VlcbOpCodes opc, byte b1, byte b2, byte b3)
203{
204 return sendMessageWithNNandData(opc, 3, b1, b2, b3);
205}
206
207bool Controller::sendMessageWithNN(VlcbOpCodes opc, byte b1, byte b2, byte b3, byte b4)
208{
209 return sendMessageWithNNandData(opc, 4, b1, b2, b3, b4);
210}
211
212bool Controller::sendMessageWithNN(VlcbOpCodes opc, byte b1, byte b2, byte b3, byte b4, byte b5)
213{
214 return sendMessageWithNNandData(opc, 5, b1, b2, b3, b4, b5);
215}
216
217}
Definition ArrayHolder.h:17
Definition CircularBuffer.h:15
a class to encapsulate Controller module configuration, events, NVs, EEPROM, etc
Definition Configuration.h:40
bool sendWRACK()
send a WRACK (write acknowledge) message
Definition Controller.cpp:224
bool sendMessageWithNN(VlcbOpCodes opc)
Definition Controller.h:187
void setParamFlag(VlcbParamFlags flag, bool set)
Definition Controller.cpp:151
Controller(Configuration *conf)
Definition Controller.cpp:52
void sendGRSP(VlcbOpCodes opCode, byte serviceType, byte errCode)
Definition Controller.cpp:239
void indicateMode(VlcbModeParams mode)
set the Controller LEDs to indicate the current mode
Definition Controller.cpp:142
Configuration * getModuleConfig() const
Definition Controller.h:130
Parameters & getParams()
Definition Controller.h:138
bool pendingTasks()
Definition Controller.cpp:265
void messageActedOn()
Definition Controller.cpp:270
unsigned int getMessagesActedOn()
Definition Controller.h:167
byte getModuleCANID() const
Definition Controller.h:156
void setName(const char *mname)
Definition Controller.h:132
void setServices(std::initializer_list< Service * > services)
Definition Controller.cpp:84
bool sendCMDERR(byte cerrno)
send a CMDERR (command error) message
Definition Controller.cpp:233
const ArrayHolder< Service * > & getServices()
Definition Controller.h:134
bool pendingAction()
Definition Controller.cpp:260
unsigned char getParam(VlcbParams param) const
Definition Controller.h:139
void sendDGN(byte serviceIndex, byte diagCode, unsigned int counter)
Definition Controller.cpp:244
void putAction(const Action &action)
Definition Controller.cpp:249
void updateParamFlags()
assign the module parameter set
Definition Controller.cpp:110
void indicateActivity()
Definition Controller.cpp:163
const CircularBuffer< Action, ACTION_QUEUE_SIZE > & getActionQueue() const
Definition Controller.h:169
Definition Parameters.h:16
unsigned char * getParams() const
Definition Parameters.h:69
Definition TimedResponse.h:26
Manage tasks that respond with messages at timed intervals.
Definition TimedResponse.h:21
Convenience class for use with initializer lists, i.e. bracketed list of items: { a,...
Definition initializer_list.h:19
Definition AbstractEventTeachingService.cpp:13
bool sendMessage(const VlcbMessage &msg)
Send a VLCB message to the bus.
Definition VLCB.cpp:141
ACTION
Type of Action.
Definition Controller.h:72
@ ACT_INDICATE_MODE
The mode has changed and should be indicated by the user interface.
Definition Controller.h:80
@ ACT_MESSAGE_IN
A message coming in to the node.
Definition Controller.h:73
@ ACT_START_CAN_ENUMERATION
A request to start CAN enumeration from the user interface.
Definition Controller.h:75
@ ACT_INDICATE_ACTIVITY
Some minor activity happened that should be indicated by the user interface.
Definition Controller.h:78
@ ACT_INDICATE_WORK
Some work for the node occurred and should be indicated by the user interface.
Definition Controller.h:79
@ ACT_RENEGOTIATE
A request to change node number from the user interface.
Definition Controller.h:77
@ ACT_MESSAGE_OUT
A message sent out from the node.
Definition Controller.h:74
@ ACT_CHANGE_MODE
A request to change mode from the user interface.
Definition Controller.h:76
bool sendMessageWithNN(VlcbOpCodes opc)
Definition VLCB.cpp:146
const int ACTION_QUEUE_SIZE
Definition Controller.h:24
void begin()
Definition VLCB.cpp:186
void addTimedResponseTask(TimedResponse::Task *task)
Add a task that produces a number for response messages in a timed fashion.
Definition VLCB.cpp:171
void process()
Definition VLCB.cpp:192
An action to be performed.
Definition Controller.h:86
enum ACTION actionType
Type of action.
Definition Controller.h:87
Action(ACTION type, const VlcbMessage &msg)
Definition Controller.h:106
Action(ACTION type, bool fromENUM)
Definition Controller.h:102
Action()=default
Action(ACTION type, VlcbModeParams mode)
Definition Controller.h:110
Action(ACTION type)
Definition Controller.h:97
VLCB message type.
Definition Controller.h:38
VlcbMessage()=default
uint8_t len
Definition Controller.h:39
VlcbMessage(uint8_t len, uint8_t data[])
Construct a message from existing data.
Definition Controller.h:45
VlcbMessage & addNNEN(byte nn_en[EE_HASH_BYTES])
Add an event identifier (NN/EN) to the message.
Definition Controller.cpp:43
VlcbMessage & addNN(unsigned int nn)
Add a node number (two bytes) to the message.
Definition Controller.h:63
VlcbMessage & add2Bytes(unsigned int n)
Add a number as two bytes of data to the message.
Definition Controller.cpp:36
VlcbMessage(VlcbOpCodes opc)
Construct a message and populate the message OP-code.
Definition Controller.h:52
VlcbMessage & addData(byte b)
Add a data byte to the message.
Definition Controller.cpp:30
VlcbMessage & addEN(unsigned int en)
Add an event number (two bytes) to the message.
Definition Controller.h:65
uint8_t data[8]
Definition Controller.h:40
VlcbMessage vlcbMessage
with ACT_MESSAGE_IN & ACT_MESSAGE_OUT
bool fromENUM
with ACT_START_CAN_ENUMERATION
VlcbModeParams mode
with ACT_INDICATE_MODE
VlcbParams
Definition vlcbdefs.hpp:507
VlcbModeParams
Definition vlcbdefs.hpp:562
VlcbParamFlags
Definition vlcbdefs.hpp:544
VlcbOpCodes
Definition vlcbdefs.hpp:188