forked from SvenRosvall/VLCB-Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVLCB_empty.ino
More file actions
142 lines (114 loc) · 4.04 KB
/
VLCB_empty.ino
File metadata and controls
142 lines (114 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (C) Sven Rosvall (sven@rosvall.ie)
// This file is part of VLCB-Arduino project on https://github.com/SvenRosvall/VLCB-Arduino
// Licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
// The full licence can be found at: http://creativecommons.org/licenses/by-nc-sa/4.0
/*
3rd party libraries needed for compilation: (not for binary-only distributions)
Streaming -- C++ stream style output, v5, (http://arduiniana.org/libraries/streaming/)
ACAN2515 -- library to support the MCP2515/25625 CAN controller IC
*/
// 3rd party libraries
#include <Streaming.h>
// VLCB library header files
#include <VLCB.h>
#include <CAN2515.h> // Chosen CAN controller
// forward function declarations
void printConfig();
// constants
const byte VER_MAJ = 1; // code major version
const char VER_MIN = 'a'; // code minor version
const byte VER_BETA = 0; // code beta sub-version
const byte MANUFACTURER = MANU_DEV; // for boards in development.
const byte MODULE_ID = 98; // VLCB module type
// module name, must be at most 7 characters
char mname[] = "EMPTY";
const byte LED_GRN = 4; // VLCB green Unitialised LED pin
const byte LED_YLW = 7; // VLCB yellow Normal LED pin
const byte SWITCH0 = 8; // VLCB push button switch pin
VLCB::CAN2515 can2515; // CAN transport object
// Service objects
VLCB::LEDUserInterface ledUserInterface(LED_GRN, LED_YLW, SWITCH0);
VLCB::SerialUserInterface serialUserInterface;
VLCB::MinimumNodeServiceWithDiagnostics mnService;
VLCB::CanServiceWithDiagnostics canService(&can2515);
//
/// setup VLCB - runs once at power on from setup()
//
void setupVLCB()
{
VLCB::checkStartupAction(LED_GRN, LED_YLW, SWITCH0);
VLCB::setServices({
&mnService, &ledUserInterface, &serialUserInterface, &canService});
// set module parameters
VLCB::setVersion(VER_MAJ, VER_MIN, VER_BETA);
VLCB::setModuleId(MANUFACTURER, MODULE_ID);
// set module name
VLCB::setName(mname);
// configure and start CAN bus and VLCB message processing
can2515.setNumBuffers(2, 1); // more buffers = more memory used, fewer = less
can2515.setOscFreq(16000000UL); // select the crystal frequency of the CAN module
can2515.setPins(10, 2); // select pins for CAN bus CE and interrupt connections
if (!can2515.begin())
{
Serial << F("> error starting VLCB") << endl;
}
// initialise and load configuration
VLCB::begin();
Serial << F("> mode = ") << VLCB::Configuration::modeString(VLCB::getCurrentMode());
Serial << F(", CANID = ") << VLCB::getCANID();
Serial << F(", NN = ") << VLCB::getNodeNum() << endl;
// show code version and copyright notice
printConfig();
}
//
/// setup - runs once at power on
//
void setup()
{
Serial.begin (115200);
Serial << endl << endl << F("> ** VLCB Arduino basic example module ** ") << __FILE__ << endl;
setupVLCB();
// end of setup
Serial << F("> ready") << endl << endl;
}
//
/// loop - runs forever
//
void loop()
{
//
/// do VLCB message, switch and LED processing
//
VLCB::process();
//
/// check CAN message buffers
//
if (can2515.canp->receiveBufferPeakCount() > can2515.canp->receiveBufferSize())
{
Serial << F("> receive buffer overflow") << endl;
}
if (can2515.canp->transmitBufferPeakCount(0) > can2515.canp->transmitBufferSize(0))
{
Serial << F("> transmit buffer overflow") << endl;
}
//
/// check CAN bus state
//
byte s = can2515.canp->errorFlagRegister();
if (s != 0)
{
// Serial << F("> error flag register is non-zero = ") << s << endl;
}
// bottom of loop()
}
//
/// print code version config details and copyright notice
//
void printConfig()
{
// code version
Serial << F("> code version = ") << VER_MAJ << VER_MIN << F(" beta ") << VER_BETA << endl;
Serial << F("> compiled on ") << __DATE__ << F(" at ") << __TIME__ << F(", compiler ver = ") << __cplusplus << endl;
// copyright
Serial << F("> © Sven Rosvall (MERG 3777) 2025") << endl;
}