Initial commit
This commit is contained in:
148
Software/UPSoftware/MeyCan.cpp
Normal file
148
Software/UPSoftware/MeyCan.cpp
Normal file
@@ -0,0 +1,148 @@
|
||||
#include <iterator>
|
||||
#include "esp32-hal-gpio.h"
|
||||
#include "MeyCan.h";
|
||||
#include "driver/twai.h"
|
||||
|
||||
PinState *MeyPin = NULL;
|
||||
uint16_t myDeviceId = 0;
|
||||
byte _deviceTypeId = 0;
|
||||
byte _majorHardwareVersion = 0;
|
||||
byte _minorHardwareVersion = 0;
|
||||
|
||||
|
||||
void SetupMeyPin(PinState *state) {
|
||||
if (state->is_input) {
|
||||
pinMode(state->pin_id, INPUT_PULLUP);
|
||||
state->pin_state = digitalRead(state->pin_id);
|
||||
} else {
|
||||
pinMode(state->pin_id, OUTPUT);
|
||||
digitalWrite(state->pin_id, LOW);
|
||||
state->pin_state = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SetupMeyCan(byte majorHardwareVersion, byte minorHardwareVersion, byte deviceTypeId) {
|
||||
_majorHardwareVersion = majorHardwareVersion;
|
||||
_minorHardwareVersion = minorHardwareVersion;
|
||||
_deviceTypeId = deviceTypeId;
|
||||
|
||||
if (MeyPin != NULL)
|
||||
MeyPin->ForEach(SetupMeyPin);
|
||||
}
|
||||
|
||||
|
||||
void SetDevicedId(byte high, byte low) {
|
||||
myDeviceId = (high << 8) | low;
|
||||
}
|
||||
|
||||
void SetMeyPin(byte meyPinId, byte pinId) {
|
||||
PinState *newState = new PinState();
|
||||
newState->Init(pinId, (byte)meyPinId);
|
||||
|
||||
if (MeyPin == NULL)
|
||||
MeyPin = newState;
|
||||
else
|
||||
MeyPin->AddPinState(newState);
|
||||
}
|
||||
|
||||
|
||||
bool ReadPin(PinState *state) {
|
||||
return digitalRead(state->pin_id);
|
||||
}
|
||||
|
||||
|
||||
void SendSwitchedTriggeredCanPackage(byte pinId, int state) {
|
||||
twai_message_t message;
|
||||
message.extd = 1;
|
||||
message.rtr = 0;
|
||||
message.ss = 0;
|
||||
message.self = 0;
|
||||
message.dlc_non_comp = 0;
|
||||
|
||||
message.identifier = CreateCanId(SWITCH_TRIGGERED_CAN_ID);
|
||||
message.data_length_code = 2;
|
||||
message.data[0] = pinId;
|
||||
message.data[1] = state;
|
||||
|
||||
DoSendCanPkg(&message);
|
||||
}
|
||||
|
||||
void CheckPinStatus(PinState *state) {
|
||||
if (!state->is_input)
|
||||
return;
|
||||
|
||||
bool newValue = ReadPin(state);
|
||||
|
||||
|
||||
if (newValue != state->pin_state) {
|
||||
delay(10);
|
||||
newValue = ReadPin(state);
|
||||
if (newValue != state->pin_state) {
|
||||
state->pin_state = newValue;
|
||||
SendSwitchedTriggeredCanPackage(state->meyPinId, state->pin_state);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void CheckMeyPinsTriggered() {
|
||||
MeyPin->ForEach(CheckPinStatus);
|
||||
}
|
||||
|
||||
|
||||
uint16_t GetDeviceId(uint32_t canFrameId) {
|
||||
return canFrameId & 0xFFFF;
|
||||
}
|
||||
|
||||
uint16_t GetPackageType(uint32_t canFrameId) {
|
||||
return (canFrameId / 0x10000) & 0xFFF;
|
||||
}
|
||||
|
||||
uint32_t CreateCanId(uint16_t commandId) {
|
||||
return ((((uint32_t)commandId) & 0xFFF) * 0x10000) | myDeviceId;
|
||||
}
|
||||
|
||||
void HandleTriggerMeypinCanPackage(twai_message_t *frame) {
|
||||
|
||||
|
||||
if (GetPackageType(frame->identifier) == TRIGGER_SWITCH_CAN_ID) {
|
||||
|
||||
|
||||
uint16_t adressedDeviceId = ((uint16_t)frame->data[0] << 8) | frame->data[1];
|
||||
if (adressedDeviceId != myDeviceId) return;
|
||||
|
||||
|
||||
byte meyPinId = frame->data[2];
|
||||
bool state = frame->data[3] > 0;
|
||||
|
||||
PinState *adressedPin = MeyPin->Find(meyPinId);
|
||||
|
||||
if (adressedPin != NULL) {
|
||||
if (adressedPin->is_input == true) {
|
||||
pinMode(adressedPin->pin_id, OUTPUT);
|
||||
adressedPin->is_input = false;
|
||||
}
|
||||
|
||||
bool pinChanged = adressedPin->pin_state != state;
|
||||
adressedPin->pin_state = state;
|
||||
|
||||
if (pinChanged) {
|
||||
digitalWrite(adressedPin->pin_id, state);
|
||||
SendSwitchedTriggeredCanPackage(adressedPin->meyPinId, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void HandleFrame(twai_message_t *frame) {
|
||||
HandleTriggerMeypinCanPackage(frame);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DoSendCanPkg(twai_message_t *frame) {
|
||||
esp_err_t ret = twai_transmit(frame, pdMS_TO_TICKS(1000));
|
||||
}
|
||||
80
Software/UPSoftware/MeyCan.h
Normal file
80
Software/UPSoftware/MeyCan.h
Normal file
@@ -0,0 +1,80 @@
|
||||
#ifndef MEYCAN_H
|
||||
#define MEYCAN_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <SPI.h>
|
||||
#include "driver/twai.h"
|
||||
|
||||
typedef struct PinState
|
||||
{
|
||||
int pin_id;
|
||||
bool pin_state;
|
||||
bool is_input;
|
||||
byte meyPinId;
|
||||
PinState *next = NULL;
|
||||
|
||||
PinState() {}
|
||||
void Init(int pin_id, byte meyPinId)
|
||||
{
|
||||
this->pin_id = pin_id;
|
||||
this->pin_state = true;
|
||||
this->is_input = true;
|
||||
this->meyPinId = meyPinId;
|
||||
}
|
||||
|
||||
void AddPinState(PinState *nextPinState)
|
||||
{
|
||||
if (next == NULL)
|
||||
{
|
||||
this->next = nextPinState;
|
||||
nextPinState->next = NULL;
|
||||
} else {
|
||||
next->AddPinState(nextPinState);
|
||||
}
|
||||
}
|
||||
|
||||
PinState* Find(byte meyPinId)
|
||||
{
|
||||
if (this->meyPinId == meyPinId)
|
||||
return this;
|
||||
|
||||
if (this->next != NULL)
|
||||
return this->next->Find(meyPinId);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void ForEach(void (*handle)(PinState *theState))
|
||||
{
|
||||
handle(this);
|
||||
if (this->next != NULL)
|
||||
this->next->ForEach(handle);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const byte SOFTWARE_VERSION_HIGH = 6;
|
||||
const byte SOFTWARE_VERSION_LOW = 0;
|
||||
const byte HARDWARE_VERSION_HIGH = 8;
|
||||
const byte HARDWARE_VERSION_LOW = 1;
|
||||
|
||||
const uint16_t HELP_PACKAGE_CAN_ID = 0x0FFFUL;
|
||||
const uint16_t SWITCH_TRIGGERED_CAN_ID = 0x0050;
|
||||
const uint16_t TRIGGER_SWITCH_CAN_ID = 0x0055;
|
||||
|
||||
|
||||
|
||||
void SetupMeyCan(byte majorHardwareVersion, byte minorHardwareVersion, byte deviceTypeId);
|
||||
uint32_t CreateCanId(uint16_t commandId);
|
||||
void SetDevicedId(byte high, byte low);
|
||||
void SetMeyPin(byte meyPinId, byte pinId);
|
||||
uint16_t GetDeviceId(uint32_t canFrameId);
|
||||
uint16_t GetPackageType(uint32_t canFrameId);
|
||||
void SendSwitchedTriggeredCanPackage(byte pinId, int state);
|
||||
|
||||
void HandleFrame(twai_message_t *frame);
|
||||
void CheckPinStatus(PinState *state);
|
||||
void CheckMeyPinsTriggered(); /* checks weather a meypin triggered and sends a can pkg is neccessary */
|
||||
|
||||
void DoSendCanPkg(twai_message_t *frame) ;
|
||||
#endif
|
||||
82
Software/UPSoftware/UPSoftware.ino
Normal file
82
Software/UPSoftware/UPSoftware.ino
Normal file
@@ -0,0 +1,82 @@
|
||||
#include <Arduino.h>
|
||||
#include "driver/twai.h"
|
||||
#include "MeyCan.h"
|
||||
|
||||
// https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/TWAI/TWAIreceive/TWAIreceive.ino
|
||||
|
||||
#define RX_PIN 2
|
||||
#define TX_PIN 3
|
||||
|
||||
void DebugBlink(int d) {
|
||||
pinMode(20, OUTPUT);
|
||||
while (true) {
|
||||
|
||||
digitalWrite(20, HIGH);
|
||||
delay(d);
|
||||
digitalWrite(20, LOW);
|
||||
delay(d);
|
||||
}
|
||||
}
|
||||
|
||||
bool driver_installed = false;
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
SPI.begin();
|
||||
|
||||
// Explicit GND for LED and Input
|
||||
pinMode(21, OUTPUT);
|
||||
digitalWrite(21, LOW);
|
||||
|
||||
// Initialize configuration structures using macro initializers
|
||||
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)TX_PIN, (gpio_num_t)RX_PIN, TWAI_MODE_NORMAL);
|
||||
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_1MBITS(); //Look in the api-reference for other speed sets.
|
||||
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
|
||||
|
||||
|
||||
if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
|
||||
Serial.println("Driver installed");
|
||||
} else {
|
||||
DebugBlink(100);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
esp_err_t e = twai_start();
|
||||
// Start TWAI driver
|
||||
if (e == ESP_OK) {
|
||||
driver_installed = true;
|
||||
} else {
|
||||
DebugBlink(500);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
SetDevicedId(0x05, 0x1F);
|
||||
|
||||
SetMeyPin(1, 5);
|
||||
SetMeyPin(2, 6);
|
||||
SetMeyPin(3, 7);
|
||||
SetMeyPin(4, 8);
|
||||
SetMeyPin(5, 9);
|
||||
SetMeyPin(6, 10);
|
||||
SetMeyPin(7, 20);
|
||||
|
||||
SetupMeyCan(8, 1, 3);
|
||||
}
|
||||
|
||||
twai_message_t frame;
|
||||
void loop() {
|
||||
|
||||
if (!driver_installed) {
|
||||
// Driver not installed
|
||||
DebugBlink(2000);
|
||||
return;
|
||||
}
|
||||
|
||||
CheckMeyPinsTriggered();
|
||||
if (twai_receive(&frame, 0) == ESP_OK) {
|
||||
|
||||
HandleFrame(&frame);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user