Skip to content

Commit

Permalink
work on examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Hirschenberger committed May 24, 2024
1 parent 4b29973 commit ac7b51e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 120
ColumnLimit: 150
CommentPragmas: "^ IWYU pragma:"
QualifierAlignment: Leave
CompactNamespaces: false
Expand Down
245 changes: 96 additions & 149 deletions examples/PkpInitialConfiguration/PkpInitialConfiguration.ino
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// This sketch provides a basic configuration for your keyboard without using the actual classes from the library

#define USE_MCP2515 // Comment this line to use Arduino CAN (Feather M4 CAN)
#define USE_MCP2515 // Comment this line to use Arduino CAN (Feather M4 CAN)

#include <SPI.h>
#ifdef USE_MCP2515
#include <mcp2515.h>

CS_PIN_MCP 10
MCP_CLOCK_SPEED MCP_8MHZ
#include <SPI.h>
#include <mcp2515.h>
#define CS_PIN_MCP 10
#define MCP_CLOCK_SPEED MCP_8MHZ
MCP2515 mcp2515(CS_PIN_MCP, MCP_CLOCK_SPEED);
#else
#include <Adafruit_CAN.h>
#include <Adafruit_CAN.h>
Adafruit_CAN can;
#endif

enum PKPBaudRate: uint8_t{
enum BaudRate_e : uint8_t {
PKP_BAUDRATE_0020k = 0x07,
PKP_BAUDRATE_0050k = 0x06,
PKP_BAUDRATE_0125k = 0x04,
Expand All @@ -21,31 +22,89 @@ enum PKPBaudRate: uint8_t{
PKP_BAUDRATE_1000k = 0x00
};

//Configuration here:
#define KEYPAD_ACT_CAN_ID 0x15
#define KEYPAD_NEW_CAN_ID 0x16
#define KEYPAD_NEW_BAUD_RATE PKP_BAUDRATE_1000k;
#define KEYPAD_ACTVIE_AT_STARTUP 1 //1: enable, 0: disable
#define KEYPAD_STARTUP_LED_SHOW 1 //1: complete show (default), 0: disable, 2: fast flash
#define KEYPAD_HEARTBEAT_INTERVAL 500 //0: heartbeat production disabled, otherwise hearteat interval [ms]
#define KEYPAD_PERIODIC_TX_INTERVAL 0 //0: eventbased pdo transmission, otherwise pdo interval [ms]
enum keyBacklight_e : uint8_t {
BACKLIGHT_DEFAULT = 0x00,
BACKLIGHT_RED = 0x01,
BACKLIGHT_GREEN = 0x02,
BACKLIGHT_BLUE = 0x03,
BACKLIGHT_YELLOW = 0x04,
BACKLIGHT_CYAN = 0x05,
BACKLIGHT_VIOLET = 0x06,
BACKLIGHT_WHITE = 0x07,
BACKLIGHT_AMBER = 0x08,
BACKLIGHT_YELLOWGREEN = 0x09
};

////////////////////////////
// CONFIGURE THESE VALUES //
////////////////////////////
constexpr uint8_t ACT_CAN_ID = 0x15;
constexpr uint8_t NEW_CAN_ID = 0x16;
constexpr uint8_t NEW_BAUD_RATE = PKP_BAUDRATE_1000k;
constexpr uint8_t ACTIVE_AT_STARTUP = 1; //1: enable, 0: disable
constexpr uint8_t STARTUP_LED_SHOW = 1; //1: complete show (default), 0: disable, 2: fast flash
constexpr uint8_t PERIODIC_KEY_INTERVAL = 0; //0: eventbased pdo transmission, otherwise pdo interval [ms]
constexpr uint8_t PERIODIC_AI_INTERVAL = 0;
constexpr uint8_t DEFAULT_BACKGROUND_COLOR = BACKLIGHT_WHITE;
constexpr uint8_t DEFAULT_BACKGROUND_BRIGHTNESS = 10;
constexpr uint8_t DEFAULT_KEY_LED_BRIGHTNESS = 70;
constexpr uint8_t BOOTUP_SERVICE_MESAGE = 1;
constexpr uint16_t PROD_HEARTBEAT_INTERVAL = 500; // 10 to 65279ms (0 to disable heartbeat production)
constexpr uint8_t DEMO_MODE = 0;
constexpr uint8_t RESTORE_DEFAULTS = 0;

/////////////////////////////////////////////////
// CAN FRAME PAYLOADS - NO CHANGES NEEDED HERE //
/////////////////////////////////////////////////
constexpr uint8_t setCanProtocol[] = {0x04, 0x1B, 0x80, 0x00, 0xFF, 0xFF, 0xFF, 0xFF};
constexpr uint8_t setBaudRate[] = {0x2F, 0x10, 0x20, 0x00, 0x02};
constexpr uint8_t setNodeId[] = {0x2F, 0x13, 0x20, 0x00, NEW_CAN_ID};
constexpr uint8_t startUpShow[] = {0x2F, 0x14, 0x20, 0x00, STARTUP_LED_SHOW};
constexpr uint8_t EventBasedKeyTx[] = {0x2F, 0x00, 0x18, 0x02, 0xFE};
constexpr uint8_t PeriodicKeyTx[] = {0x2F, 0x00, 0x18, 0x05, 0x01};
constexpr uint8_t PeriodicKeyTxTime[] = {0x2B, 0x00, 0x18, 0x05, PERIODIC_TX_INTERVAL & 0xFF, PERIODIC_TX_INTERVAL >> 8};
constexpr uint8_t PeriodicAnalogInTxTime[] = {0x2F, 0x06, 0x20, 0x00, constrain(PERIODIC_AI_INTERVAL / 10, 0x08, 0xC8)};
constexpr uint8_t ActiveAtStartUp[] = {0x2F, 0x12, 0x20, 0x00, ACTIVE_AT_STARTUP};
constexpr uint8_t defaultBackBrightness[] = {0x2F, 0x03, 0x20, 0x06, DEFAULT_BACKGROUND_BRIGHTNESS};
constexpr uint8_t defaultBackColor[] = {0x2F, 0x03, 0x20, 0x04, DEFAULT_BACKGROUND_COLOR};
constexpr uint8_t defaultKeyBrightness[] = {0x2F, 0x03, 0x20, 0x05, DEFAULT_KEY_LED_BRIGHTNESS};
constexpr uint8_t setBootUpService[] = {0x2F, 0x11, 0x20, 0x00, BOOTUP_SERVICE_MESAGE};
constexpr uint8_t setProducerHeartbeat[] = {0x2B, 0x17, 0x10, 0x00, PROD_HEARTBEAT_INTERVAL & 0xFF, PROD_HEARTBEAT_INTERVAL >> 8};
constexpr uint8_t activateDemoMode[] = {0x2F, 0x00, 0x21, 0x00, DEMO_MODE};
constexpr uint8_t restoreDefaults[] = {0x23, 0x11, 0x10, 0x01, 0x6C 0x6F 0x61, 0x64};
constexpr uint8_t resetKeypad[] = {0x81, NEW_CAN_ID}

///////////////////////////////////////
// FUNCTIONS TO CONFIGURE THE KEYPAD //
///////////////////////////////////////
void setup() {
Serial.begin(115200);
#ifdef USE_MCP2515
MCP2515 mcp2515(CS_PIN_MCP, MCP_CLOCK_SPEED);
CAN_SPEED mcpBaudrate = CAN_1000KBPS;
CAN_CLOCK mcpClockSpeed = MCP_8MHZ;
setupMCP2515();
#else
Adafruit_CAN can;
setupArduinoCAN();
#endif

void setup() {
Serial.begin(115200);
#ifdef USE_MCP2515
setupMCP2515();
#else
setupArduinoCAN();
#endif
keypadConfigure(keypadNewBaudrate);

void configureKeypad() {
TRANSMIT_MSG(setCanProtocol);
TRANSMIT_MSG(setBaudRate);
TRANSMIT_MSG(setNodeId);
TRANSMIT_MSG(startUpShow);
TRANSMIT_MSG(EventBasedKeyTx);
TRANSMIT_MSG(PeriodicKeyTx);
TRANSMIT_MSG(PeriodicKeyTxTime);
TRANSMIT_MSG(PeriodicAnalogInTxTime);
TRANSMIT_MSG(ActiveAtStartUp);
TRANSMIT_MSG(defaultBackBrightness);
TRANSMIT_MSG(defaultBackColor);
TRANSMIT_MSG(defaultKeyBrightness);
TRANSMIT_MSG(setBootUpService);
TRANSMIT_MSG(setProducerHeartbeat);
TRANSMIT_MSG(activateDemoMode);
TRANSMIT_MSG(restoreDefaults);
TRANSMIT_MSG(resetKeypad);
}
}

void loop() {
Expand All @@ -67,131 +126,19 @@ void setupMCP2515() {
void setupArduinoCAN() {
if (!can.begin(CAN_1000KBPS)) {
Serial.println("CAN initialization failed");
while (1);
while (1) {
;
}
}
Serial.println("CAN initialized");
}
#endif

void sendMessage(const struct can_frame &msg) {
#ifdef USE_MCP2515
mcp2515.sendMessage(&msg);
#else
can.send(msg);
#endif
}

void keypadConfigure(uint8_t newBaudrate) {
struct can_frame txMsg = {0};
txMsg.can_id = 0x18EF2100 | CAN_EFF_FLAG;
txMsg.can_dlc = 8;
txMsg.data[0] = 0x04;
txMsg.data[1] = 0x1B;
txMsg.data[2] = 0x80;
txMsg.data[3] = 0x00;
txMsg.data[4] = 0xFF;
txMsg.data[5] = 0xFF;
txMsg.data[6] = 0xFF;
txMsg.data[7] = 0xFF;
sendMessage(txMsg);
delay(1000);

configureKeypadNewBaudRate(KEYPAD_NEW_BAUD_RATE);

setKeypadActiveAtStartup(KEYPAD_ACTVIE_AT_STARTUP);
enableCANOpenKeypads(KEYPAD_ACT_CAN_ID);
setStartupLEDShow(KEYPAD_STARTUP_LED_SHOW);
setPeriodicTransmissionTime();
setKeypadModeToPeriodicTransmission();
configureKeypadNewNodeAddress(KEYPAD_NEW_CAN_ID);
}

void configureKeypadNewNodeAddress(uint8_t newCanId)
{
struct can_frame txMsg = {0};
txMsg.can_id = 0x600 + KEYPAD_ACT_CAN_ID;
txMsg.can_dlc = 5;
txMsg.data[0] = 0x2F;
txMsg.data[1] = 0x13;
txMsg.data[2] = 0x20;
txMsg.data[3] = 0x00;
txMsg.data[4] = newCanId;
sendMessage(txMsg);
delay(1000);
}

void configureKeypadNewBaudRate(uint8_t newBaudrate) {
struct can_frame txMsg = {0};
txMsg.can_id = 0x600 + KEYPAD_ACT_CAN_ID;
txMsg.can_dlc = 5;
txMsg.data[0] = 0x2F;
txMsg.data[1] = 0x10;
txMsg.data[2] = 0x20;
txMsg.data[3] = 0x00;
txMsg.data[4] = newBaudrate;
sendMessage(txMsg);
delay(1000);
}

void setKeypadActiveAtStartup(uint8_t StartupActive) {
struct can_frame txMsg = {0};
txMsg.can_id = 0x600 + KEYPAD_ACT_CAN_ID;
txMsg.can_dlc = 5;
txMsg.data[0] = 0x2F;
txMsg.data[1] = 0x12;
txMsg.data[2] = 0x20;
txMsg.data[3] = 0x00;
txMsg.data[4] = StartupActive;
sendMessage(txMsg);
delay(1000);
}

void enableCANOpenKeypads(uint8_t id) {
struct can_frame txMsg = {0};
txMsg.can_id = 0x000;
txMsg.can_dlc = 2;
txMsg.data[0] = 0x01;
txMsg.data[1] = id;
sendMessage(txMsg);
delay(1000);
}

void setStartupLEDShow(uint8_t ledShow) {
struct can_frame txMsg = {0};
txMsg.can_id = 0x600 + KEYPAD_ACT_CAN_ID;
txMsg.can_dlc = 5;
txMsg.data[0] = 0x2F;
txMsg.data[1] = 0x14;
txMsg.data[2] = 0x20;
txMsg.data[3] = 0x00;
txMsg.data[4] = ledShow; // fast flash
sendMessage(txMsg);
delay(1000);
}

void setPeriodicTransmissionTime() {
struct can_frame txMsg = {0};
txMsg.can_id = 0x600 + KEYPAD_ACT_CAN_ID;
txMsg.can_dlc = 6;
txMsg.data[0] = 0x2B;
txMsg.data[1] = 0x00;
txMsg.data[2] = 0x18;
txMsg.data[3] = 0x05;
txMsg.data[4] = 0x28;
txMsg.data[5] = 0x01;
sendMessage(txMsg);
delay(1000);
}

void setKeypadModeToPeriodicTransmission() {
struct can_frame txMsg = {0};
txMsg.can_id = 0x600 + KEYPAD_ACT_CAN_ID;
txMsg.can_dlc = 5;
txMsg.data[0] = 0x2F;
txMsg.data[1] = 0x00;
txMsg.data[2] = 0x18;
txMsg.data[3] = 0x05;
txMsg.data[4] = 0x01;
sendMessage(txMsg);
delay(1000);
void sendMessage(const uint8_t* data, uint8_t length) {
#ifdef USE_MCP2515
mcp2515.sendMessage(&msg);
#else
can.send(msg);
#endif
}
4 changes: 2 additions & 2 deletions src/BlinkMarinePkpCanOpen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,8 @@ Pkp::returnState_e Pkp::_decodeWiredInputs(const uint8_t data[8]) {

uint32_t value;
for (int i = 0; i < PKP_MAX_WIRED_IN_AMOUNT; i++) {
value = data[i * 2] | (data[i * 2 + 1] << 8);
value = min(value, 500);
value = data[i * 2] | (data[i * 2 + 1] << 8);
value = min(value, 500);
_wiredInputValue[i] = (uint8_t)(value * 255uL / 500uL);
}

Expand Down
2 changes: 1 addition & 1 deletion src/BlinkMarinePkpCanOpen.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class Pkp {
uint32_t _lastReconnectTry = 0;
int8_t _overrideKeyState[PKP_MAX_KEY_AMOUNT] = {0};
int8_t _relativeEncoderTicks[PKP_MAX_ROTARY_ENCODER_AMOUNT] = {0};
uint8_t _wiredInputValue[PKP_MAX_WIRED_IN_AMOUNT] = {0};
uint8_t _wiredInputValue[PKP_MAX_WIRED_IN_AMOUNT] = {0};
CanMsgTxCallback _transmitMessage;


Expand Down

0 comments on commit ac7b51e

Please sign in to comment.