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
89struct Action
90{
92 union
93 {
95 bool fromENUM;
97 };
98
99 Action() = default;
100
102 : actionType(type)
103 , fromENUM(false) // Must include one item from the union.
104 {}
105
106 Action(ACTION type, bool fromENUM)
107 : actionType(type), fromENUM(fromENUM)
108 {}
109
110 Action(ACTION type, const VlcbMessage & msg)
111 : actionType(type), vlcbMessage(msg)
112 {}
113
115 : actionType(type), mode(mode)
116 {}
117};
118
119class Service;
120
121//
123//
125{
126public:
127// Controller();
128 explicit Controller(Configuration *conf);
129// Controller(std::initializer_list<Service *> services);
131
133
134 Configuration * getModuleConfig() const { return module_config; }
135
136 void setName(const char *mname) { module_config->setName(mname); }
137
138 const ArrayHolder<Service *> & getServices() { return services; }
139
140 void updateParamFlags();
141 void setParamFlag(VlcbParamFlags flag, bool set);
142 Parameters & getParams() { return module_config->getParams(); }
143 unsigned char getParam(VlcbParams param) const { return module_config->getParam(param); }
144
145 bool sendMessage(const VlcbMessage &msg);
146 bool sendMessage(const VlcbMessage *msg);
147
148 void begin();
150 bool sendMessageWithNN(VlcbOpCodes opc, byte b1);
151 bool sendMessageWithNN(VlcbOpCodes opc, byte b1, byte b2);
152 bool sendMessageWithNN(VlcbOpCodes opc, byte b1, byte b2, byte b3);
153 bool sendMessageWithNN(VlcbOpCodes opc, byte b1, byte b2, byte b3, byte b4);
154 bool sendMessageWithNN(VlcbOpCodes opc, byte b1, byte b2, byte b3, byte b4, byte b5);
155 bool sendWRACK();
156 bool sendCMDERR(byte cerrno);
157 void sendGRSP(VlcbOpCodes opCode, byte serviceType, byte errCode);
158 void sendDGN(byte serviceIndex, byte diagCode, unsigned int counter);
159
160 byte getModuleCANID() const { return module_config->CANID; }
161 void process();
162 void indicateMode(VlcbModeParams mode);
163 void indicateActivity();
164
165 void putAction(const Action & action);
166 void putAction(ACTION action);
167 bool pendingAction();
168 bool pendingTasks();
169
170 void messageActedOn();
171 unsigned int getMessagesActedOn() { return diagMsgsActed; }
172
173 const CircularBuffer<Action, ACTION_QUEUE_SIZE> & getActionQueue() const { return actionQueue; }
174
176
177private:
178 Configuration *module_config;
179 ArrayHolder<Service *> services;
180
182 TimedResponse timedResponses;
183
184 // Really an MNS diagnostic but placed here as its data is collected across all services.
185 unsigned int diagMsgsActed = 0;
186};
187
188}
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:267
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:282
void indicateMode(VlcbModeParams mode)
set the Controller LEDs to indicate the current mode
Definition Controller.cpp:142
Configuration * getModuleConfig() const
Definition Controller.h:134
Parameters & getParams()
Definition Controller.h:142
bool pendingTasks()
Definition Controller.cpp:308
void messageActedOn()
Definition Controller.cpp:313
unsigned int getMessagesActedOn()
Definition Controller.h:171
byte getModuleCANID() const
Definition Controller.h:160
void setName(const char *mname)
Definition Controller.h:136
void setServices(std::initializer_list< Service * > services)
Definition Controller.cpp:84
bool sendCMDERR(byte cerrno)
send a CMDERR (command error) message
Definition Controller.cpp:276
const ArrayHolder< Service * > & getServices()
Definition Controller.h:138
bool pendingAction()
Definition Controller.cpp:303
unsigned char getParam(VlcbParams param) const
Definition Controller.h:143
void sendDGN(byte serviceIndex, byte diagCode, unsigned int counter)
Definition Controller.cpp:287
void putAction(const Action &action)
Definition Controller.cpp:292
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:173
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:90
enum ACTION actionType
Type of action.
Definition Controller.h:91
Action(ACTION type, const VlcbMessage &msg)
Definition Controller.h:110
Action(ACTION type, bool fromENUM)
Definition Controller.h:106
Action()=default
Action(ACTION type, VlcbModeParams mode)
Definition Controller.h:114
Action(ACTION type)
Definition Controller.h:101
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
message used with ACT_MESSAGE_IN & ACT_MESSAGE_OUT
bool fromENUM
used with ACT_START_CAN_ENUMERATION to tell if the action came from an ENUM message
VlcbModeParams mode
requested new mode used with ACT_INDICATE_MODE
VlcbParams
Definition vlcbdefs.hpp:507
VlcbModeParams
Definition vlcbdefs.hpp:562
VlcbParamFlags
Definition vlcbdefs.hpp:544
VlcbOpCodes
Definition vlcbdefs.hpp:188