Skip to content

Commit

Permalink
inital release
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Schneider committed Jun 10, 2020
1 parent 352b4a0 commit 8e1bcf1
Show file tree
Hide file tree
Showing 16 changed files with 1,701 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
8 changes: 8 additions & 0 deletions .vscode/arduino.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"board": "esp32:esp32:nodemcu-32s",
"configuration": "FlashFreq=80,UploadSpeed=921600",
"port": "/dev/ttyUSB0",
"programmer": "ArduinoISP",
"sketch": "Zusi_HLL_HLB-gross.ino",
"output": "./build/"
}
143 changes: 143 additions & 0 deletions Arduino.ino.foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Author: Sebastian Wolf
* Created: August 2018
*/

#include <Arduino.h>
#include "Zusi3Schnittstelle.h"

//Bitte die #define der Zusi3Schnittstelle.h nutzen
#if defined(ESP8266_Wifi) || defined(ESP32_Wifi)
const char *ssid = "InternetOfShit";
const char *password = "Atlas2012";
#endif
#ifdef ESP32_Ethernet
//nothing
#endif
#ifdef Ethernet_Shield //Arduino Uno hat zu wenig RAM für Datenpakete
byte *mac = new byte[6]{0x8c, 0x16, 0x45, 0x81, 0xfc, 0x72};
#endif
#ifdef AVR_Wifi
const char *ssid = "InternetOfShit";
const char *password = "Atlas2012";
#endif

Zusi3Schnittstelle *zusi;

void setup()
{
Serial.begin(115200);

#if defined(ESP8266_Wifi) || defined(ESP32_Wifi)
Serial.print("Verbinde mit ");
Serial.println(ssid);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}

Serial.println("\nVerbunden");
Serial.print("IP-Adresse: ");
Serial.println(WiFi.localIP());
#endif

#ifdef ESP32_Ethernet
ETH.begin();
#endif

#ifdef Ethernet_Shield
if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
return;
}
else
{
Serial.print("IP-Adresse: ");
Serial.println(Ethernet.localIP());
}
#endif

#ifdef AVR_Wifi
if (WiFi.status() == WL_NO_SHIELD)
{
Serial.println("WiFi shield nicht vorhanden");
return;
}
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("\nVerbunden");
Serial.print("IP-Adresse: ");
Serial.println(WiFi.localIP());
#endif

zusi = new Zusi3Schnittstelle("192.168.0.2", 1436, "ESP32");
zusi->reqFstAnz(Druck_Hauptluftleitung);
zusi->reqFstAnz(Druck_Hauptluftbehaelter);
zusi->reqFstAnz(Druck_Bremszylinder);
zusi->requestFuehrerstandsbedienung(false);
zusi->requestProgrammdaten(false);
uint32_t i = 0;
while (!zusi->connect())
{
Serial.print("Verbindung zu Zusi fehlgeschlagen (");
Serial.print(++i);
Serial.println(")");
delay(1000);
}
Serial.println("Verbunden mit Zusi");
}

void loop()
{
Node *node = zusi->update();
if (node != NULL)
{
for (int i = 0; i < node->getNodes()->size(); i++)
{
Node *subNode = node->getNodes()->get(i);
if (subNode->getIDAsInt() == 0x0A)
{
for (int j = 0; j < subNode->getAttribute()->size(); j++)
{
Attribute *attr = subNode->getAttribute()->get(j);
if (attr->getIDAsInt() == Druck_Hauptluftleitung)
{
Serial.print("Druck_Hauptluftleitung: ");
Serial.print((int)(attr->getDATAAsFloat()));
Serial.println(" bar");
}
}
for (int j = 0; j < subNode->getAttribute()->size(); j++)
{
Attribute *attr = subNode->getAttribute()->get(j);
if (attr->getIDAsInt() == Druck_Hauptluftbehaelter)
{
Serial.print("Druck_Hauptluftbehaelter: ");
Serial.print((int)(attr->getDATAAsFloat()));
Serial.println(" bar");
}
}
for (int j = 0; j < subNode->getAttribute()->size(); j++)
{
Attribute *attr = subNode->getAttribute()->get(j);
if (attr->getIDAsInt() == Druck_Bremszylinder)
{
Serial.print("Druck_Bremszylinder: ");
Serial.print((int)(attr->getDATAAsFloat()));
Serial.println(" bar");
}
}
}
}
}
}
157 changes: 157 additions & 0 deletions Attribute.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Author: Sebastian Wolf
* Created: August 2018
*/

#include "Attribute.h"

Attribute::Attribute(int ID)
{
this->ID = new byte[2];
this->ID[0] = (byte)ID;
this->ID[1] = (byte)(ID >> 8);
}

Attribute::Attribute(int ID, byte *DATA, int sizeData)
{
this->ID = new byte[2];
this->ID[0] = (byte)ID;
this->ID[1] = (byte)(ID >> 8);

this->DATA = DATA;
this->sizeDATA = sizeData;
}

Attribute::Attribute(int ID, int DATA)
{
this->ID = new byte[2];
this->ID[0] = (byte)ID;
this->ID[1] = (byte)(ID >> 8);

this->DATA = new byte[2];
this->DATA[0] = (byte)DATA;
this->DATA[1] = (byte)(DATA >> 8);
sizeDATA = 2;
}

Attribute::Attribute(int ID, String DATA)
{
this->ID = new byte[2];
this->ID[0] = (byte)ID;
this->ID[1] = (byte)(ID >> 8);

this->DATA = new byte[DATA.length()]();
DATA.getBytes(this->DATA, DATA.length() + 1);
sizeDATA = DATA.length();
}

Attribute::Attribute(byte *ID, byte *DATA, int sizeData)
{
this->ID = ID;
this->DATA = DATA;
this->sizeDATA = sizeData;
}

Attribute::~Attribute()
{
if (ID != NULL)
{
delete ID;
}
if (DATA != NULL)
{
delete DATA;
}
}

byte *Attribute::get()
{
PACKET_SIZE = sizeID + sizeDATA;
byte *result = new byte[4 + PACKET_SIZE]();
result[0] = (byte)(PACKET_SIZE & 0xFF);
result[1] = (byte)((PACKET_SIZE >> 8) & 0xFF);
result[2] = (byte)((PACKET_SIZE >> 16) & 0xFF);
result[3] = (byte)((PACKET_SIZE >> 24) & 0xFF);
result[4] = ID[0];
result[5] = ID[1];
for (int i = 0; i < sizeDATA; i++)
{
result[i + 6] = DATA[i];
}
Serial.println((char *)result);
return result;
}

int Attribute::getSize()
{
PACKET_SIZE = sizeID + sizeDATA;
return (4 + PACKET_SIZE);
}

int Attribute::getDATASize()
{
return sizeDATA;
}

byte *Attribute::getID()
{
return ID;
}

int Attribute::getIDAsInt()
{
return (ID[0] & 0xFF) | ((ID[1] & 0xFF) << 8);
}
byte *Attribute::getDATA()
{
return DATA;
}

String Attribute::getDATAAsString()
{
String tmp = (char *)DATA;
return tmp.substring(0, sizeDATA);
}

int Attribute::getDATAAsInt()
{
switch (sizeDATA)
{
case 1:
return (DATA[0] & 0xFF);
break;
case 2:
return ((DATA[0] & 0xFF) | (DATA[1] & 0xFF) << 8);
break;
case 3:
return ((DATA[0] & 0xFF) | (DATA[1] & 0xFF) << 8 | (DATA[2] & 0xFF) << 16);
break;
case 4:
return ((DATA[0] & 0xFF) | (DATA[1] & 0xFF) << 8 | (DATA[2] & 0xFF) << 16 | (DATA[3] & 0xFF) << 24);
}
}

float Attribute::getDATAAsFloat()
{
union {
byte b[4];
float f;
} tmp;
for (int i = 0; i < sizeDATA; i++)
{
tmp.b[i] = DATA[i];
}
return tmp.f;
}

boolean Attribute::getDATAAsBoolean()
{
if (getDATAAsInt() == 0)
{
return false;
}
else
{
return true;
}
}
39 changes: 39 additions & 0 deletions Attribute.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Author: Sebastian Wolf
* Created: August 2018
*/

#ifndef ATTRIBUTE_H
#define ATTRIBUTE_H
#include <Arduino.h>

class Attribute {
public:
Attribute(int ID);
Attribute(int ID, byte *DATA, int sizeData);
Attribute(int ID, int DATA);
Attribute(int ID, String DATA);
Attribute(byte *ID, byte *DATA, int sizeData);
~Attribute();

byte *get();
int getSize();
int getDATASize();

byte *getID();
int getIDAsInt();
byte *getDATA();
String getDATAAsString();
int getDATAAsInt();
float getDATAAsFloat();
boolean getDATAAsBoolean();

private:
byte * ID;
byte *DATA;

int sizeID = 2;
int sizeDATA = -1;
int PACKET_SIZE;
};
#endif
Loading

0 comments on commit 8e1bcf1

Please sign in to comment.