forked from Laserlicht/MaerklinMotorola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaerklinMotorola.h
82 lines (67 loc) · 1.61 KB
/
MaerklinMotorola.h
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
/*
MaerklinMotorola.h - Library for decoding the signals from the Märklin-Motorola-protocol.
Created by Laserlicht, Februar 27, 2018.
Released under BSD 2-Clause "Simplified" License.
*/
#ifndef MaerklinMotorola_h
#define MaerklinMotorola_h
#include "Arduino.h"
#define MM_QUEUE_LENGTH 10
enum DataGramState
{
DataGramState_Reading,
DataGramState_ReadyToParse,
DataGramState_Parsed,
DataGramState_Validated,
DataGramState_Finished,
DataGramState_Error,
};
enum MM2DirectionState
{
MM2DirectionState_Unavailable,
MM2DirectionState_Forward,
MM2DirectionState_Backward,
};
enum MM2DecoderState
{
MM2DecoderState_Unavailable,
MM2DecoderState_Red,
MM2DecoderState_Green,
};
struct MaerklinMotorolaData {
byte Trits[9];
int Timings[35];
unsigned long tm_package_delta;
MM2DirectionState MM2Direction;
unsigned char Address;
unsigned char Speed;
unsigned char Step;
unsigned char MM2FunctionIndex;
unsigned char SubAddress;
unsigned char PortAddress; // verbose "port" address (1 to 256 / 320)
DataGramState State;
bool Function;
bool Stop;
bool ChangeDir;
bool MagnetState; //with off normally all are switched off
MM2DecoderState DecoderState; // red (false) or green (true)
bool IsMagnet;
bool IsMM2;
bool IsMM2FunctionOn;
};
class MaerklinMotorola {
public:
MaerklinMotorola(int p);
void PinChange();
MaerklinMotorolaData* GetData();
void Parse();
private:
int pin;
unsigned long last_tm = 0;
unsigned long sync_tm = 0;
bool sync = false;
char timings_pos = 0;
char DataQueueWritePosition = 0;
MaerklinMotorolaData DataQueue[MM_QUEUE_LENGTH];
};
#endif