-
Notifications
You must be signed in to change notification settings - Fork 2
HelloWorld
This example shows the basics of Hapcanuino library usage. Without writing single line of code, Hapcanuino should be visible by Hapcan programmer software already.
Hello World device can perform only one instruction, toggle LED connected to Arduino's port.
For base required hardware see Hardware requirements
- Connected LED with 1kOhm resistor to PIN7 and GND of Arduino Uno.
After proper MCP CAN module connection to Arduino board and Hapcan bus, load the Sketch into Arduino. Next, You may want to configure the device with Hapcan programmer.
First You need to include Onixarts_Hapcanuino library.
#include "HapcanDevice.h"
Hapcanuino library uses C++ namespaces to arrange code, so declare using a Onixarts::HomeAutomationCore
namespace. This line will tell compiler to look into this namespace to find classes and other types.
using namespace Onixarts::HomeAutomationCore;
Next, declare a HapcanDevice class object
Hapcan::HapcanDevice hapcanDevice;
In the simplest way of implementing device all your specific code goes into sketch .ino file. So You want to know when Hapcanuino receives a message that meet criteria defined in boxes. To do so, You first declare a callback function, that will be called by internal Hapcanuino code.
void DoInstruction(Hapcan::HapcanMessage* message, byte instruction, byte param1, byte param2, byte param3);
You can put the function body above setup()
function, but in this example setup()
function is localized in very top, so compiler must know what DoInstruction(..)
function is before use it.
Next, the setup()
function itself. In this function You call hapcanDevice.Begin()
method, which configures MCP CAN module to work with. You also need to pass pointer to the callback DoInstruction(..)
function by hapcanDevice.OnMessageAcceptedEvent(DoInstruction)
method.
void setup()
{
Serial.begin(115200);
Serial.println("Hapcanuino device starting...");
// initializing Hapcanuino device
hapcanDevice.Begin();
//set callback function to be called, when received message match box criteria
hapcanDevice.OnMessageAcceptedEvent(DoInstruction);
// demo example, set pin7 as output
pinMode(PIN7, OUTPUT);
}
There is also a line to set PIN7 as an output to drive LED, but this is example specific code. You should configure the rest of Your device here as all Arduino Sketches do.
loop()
function requires only one line to make Hapcanuino work.
void loop()
{
hapcanDevice.Update();
}
The Update()
method processes the received messages from RX buffer, handle system messages, checks normal messages if any of them match box criteria and if so, calls the DoInstruction(...)
function.
Simplest implementation of DoInstruction(..)
function is to put switch
code and perform operations according to instrucion parameter.
void DoInstruction(Hapcan::HapcanMessage* message, byte instruction, byte param1, byte param2, byte param3)
{
switch (instruction)
{
case 1: digitalWrite(PIN7, digitalRead(PIN7) == LOW);
break;
// TODO: place other instructions here
}
}
In this case, when box configuration calls instruction 1 it toggle LED in PIN7.
Callback function receives pointer to HapcanMessage
which is the whole message receives from CAN bus. You can use its data in your code, for example to get current time send by Ethernet module each minute.
Instruction and three params are defined in box that meets message criteria. These params are stored in EEPROM and can be configured using Hapcan programmer's EEPROM HEX editor. Dedicated solution will be available asap.