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
26//
28//
30{
31 uint8_t len; // Value 0-7 or FF for messages handled in CanTransport
32 uint8_t data[8];
33};
34
48
50struct Action
51{
53 union
54 {
56 bool fromENUM;
58 };
59};
60
61class Service;
62
63//
65//
67{
68public:
69// Controller();
71// Controller(std::initializer_list<Service *> services);
73
75
76 Configuration * getModuleConfig() const { return module_config; }
77
78 void setName(const char *mname) { module_config->setName(mname); }
79
80 const ArrayHolder<Service *> & getServices() { return services; }
81
82 void updateParamFlags();
83 void setParamFlag(VlcbParamFlags flag, bool set);
84 Parameters & getParams() { return module_config->getParams(); }
85 unsigned char getParam(VlcbParams param) const { return module_config->getParam(param); }
86
87 bool sendMessage(const VlcbMessage *msg);
88
89 void begin();
90 inline bool sendMessageWithNN(VlcbOpCodes opc);
91 inline bool sendMessageWithNN(VlcbOpCodes opc, byte b1);
92 inline bool sendMessageWithNN(VlcbOpCodes opc, byte b1, byte b2);
93 inline bool sendMessageWithNN(VlcbOpCodes opc, byte b1, byte b2, byte b3);
94 inline bool sendMessageWithNN(VlcbOpCodes opc, byte b1, byte b2, byte b3, byte b4);
95 inline bool sendMessageWithNN(VlcbOpCodes opc, byte b1, byte b2, byte b3, byte b4, byte b5);
96 bool sendWRACK();
97 bool sendCMDERR(byte cerrno);
98 void sendGRSP(VlcbOpCodes opCode, byte serviceType, byte errCode);
99 void sendDGN(byte serviceIndex, byte diagCode, unsigned int counter);
100
101 byte getModuleCANID() const { return module_config->CANID; }
102 void process();
103 void indicateMode(VlcbModeParams mode);
104 void indicateActivity();
105
106 void putAction(const Action & action);
107 void putAction(ACTION action);
108 bool pendingAction();
109 bool pendingTasks();
110
111 void messageActedOn();
112 unsigned int getMessagesActedOn() { return diagMsgsActed; }
113
114 const CircularBuffer<Action, ACTION_QUEUE_SIZE> & getActionQueue() const { return actionQueue; }
115
117
118private:
119 Configuration *module_config;
120 ArrayHolder<Service *> services;
121
123 TimedResponse timedResponses;
124
125 bool sendMessageWithNNandData(VlcbOpCodes opc) { return sendMessageWithNNandData(opc, 0, 0); }
126 bool sendMessageWithNNandData(VlcbOpCodes opc, int len, ...);
127
128 // Really an MNS diagnostic but placed here as its data is collected across all services.
129 unsigned int diagMsgsActed = 0;
130};
131
133{
134 return sendMessageWithNNandData(opc);
135}
136
138{
139 return sendMessageWithNNandData(opc, 1, b1);
140}
141
143{
144 return sendMessageWithNNandData(opc, 2, b1, b2);
145}
146
147bool Controller::sendMessageWithNN(VlcbOpCodes opc, byte b1, byte b2, byte b3)
148{
149 return sendMessageWithNNandData(opc, 3, b1, b2, b3);
150}
151
152bool Controller::sendMessageWithNN(VlcbOpCodes opc, byte b1, byte b2, byte b3, byte b4)
153{
154 return sendMessageWithNNandData(opc, 4, b1, b2, b3, b4);
155}
156
157bool Controller::sendMessageWithNN(VlcbOpCodes opc, byte b1, byte b2, byte b3, byte b4, byte b5)
158{
159 return sendMessageWithNNandData(opc, 5, b1, b2, b3, b4, b5);
160}
161
162}
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:200
bool sendMessageWithNN(VlcbOpCodes opc)
Definition Controller.h:132
void setParamFlag(VlcbParamFlags flag, bool set)
Definition Controller.cpp:130
Controller(Configuration *conf)
Definition Controller.cpp:30
void sendGRSP(VlcbOpCodes opCode, byte serviceType, byte errCode)
Definition Controller.cpp:215
bool sendMessage(const VlcbMessage *msg)
Definition Controller.cpp:174
void begin()
Initialise VLCB.
Definition Controller.cpp:76
void indicateMode(VlcbModeParams mode)
set the Controller LEDs to indicate the current mode
Definition Controller.cpp:120
Configuration * getModuleConfig() const
Definition Controller.h:76
Parameters & getParams()
Definition Controller.h:84
bool pendingTasks()
Definition Controller.cpp:241
void messageActedOn()
Definition Controller.cpp:246
unsigned int getMessagesActedOn()
Definition Controller.h:112
byte getModuleCANID() const
Definition Controller.h:101
void setName(const char *mname)
Definition Controller.h:78
void setServices(std::initializer_list< Service * > services)
Definition Controller.cpp:62
bool sendCMDERR(byte cerrno)
send a CMDERR (command error) message
Definition Controller.cpp:209
const ArrayHolder< Service * > & getServices()
Definition Controller.h:80
bool pendingAction()
Definition Controller.cpp:236
unsigned char getParam(VlcbParams param) const
Definition Controller.h:85
void sendDGN(byte serviceIndex, byte diagCode, unsigned int counter)
Definition Controller.cpp:220
void putAction(const Action &action)
Definition Controller.cpp:225
void addTimedResponseTask(TimedResponse::Task *task)
Definition Controller.cpp:252
void process()
main Controller message processing procedure
Definition Controller.cpp:150
void updateParamFlags()
assign the module parameter set
Definition Controller.cpp:88
void indicateActivity()
Definition Controller.cpp:142
const CircularBuffer< Action, ACTION_QUEUE_SIZE > & getActionQueue() const
Definition Controller.h:114
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
ACTION
Type of Action.
Definition Controller.h:37
@ ACT_INDICATE_MODE
The mode has changed and should be indicated by the user interface.
Definition Controller.h:45
@ ACT_MESSAGE_IN
A message coming in to the node.
Definition Controller.h:38
@ ACT_START_CAN_ENUMERATION
A request to start CAN enumeration from the user interface.
Definition Controller.h:40
@ ACT_INDICATE_ACTIVITY
Some minor activity happened that should be indicated by the user interface.
Definition Controller.h:43
@ ACT_INDICATE_WORK
Some work for the node occurred and should be indicated by the user interface.
Definition Controller.h:44
@ ACT_RENEGOTIATE
A request to change node number from the user interface.
Definition Controller.h:42
@ ACT_MESSAGE_OUT
A message sent out from the node.
Definition Controller.h:39
@ ACT_CHANGE_MODE
A request to change mode from the user interface.
Definition Controller.h:41
const int ACTION_QUEUE_SIZE
Definition Controller.h:24
An action to be performed.
Definition Controller.h:51
VlcbModeParams mode
with ACT_INDICATE_MODE
Definition Controller.h:57
bool fromENUM
with ACT_START_CAN_ENUMERATION
Definition Controller.h:56
enum ACTION actionType
Type of action.
Definition Controller.h:52
VlcbMessage vlcbMessage
with ACT_MESSAGE_IN & ACT_MESSAGE_OUT
Definition Controller.h:55
CAN/Controller message type.
Definition Controller.h:30
uint8_t len
Definition Controller.h:31
uint8_t data[8]
Definition Controller.h:32
VlcbParams
Definition vlcbdefs.hpp:507
VlcbModeParams
Definition vlcbdefs.hpp:562
VlcbParamFlags
Definition vlcbdefs.hpp:544
VlcbOpCodes
Definition vlcbdefs.hpp:188