DCCpp
This is the library version of a program for Arduino to control railroading DCC devices.
SignalGenerator_esp32.h
1 /**********************************************************************
2 DCC++ BASE STATION FOR ESP32
3 
4 COPYRIGHT (c) 2017 Mike Dunston
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see http://www.gnu.org/licenses
16 **********************************************************************/
17 #ifdef ESP32
18 
19 #ifndef _SIGNALGENERATOR_ESP32_H_
20 #define _SIGNALGENERATOR_ESP32_H_
21 
22 #include <Arduino.h>
23 #include <driver/timer.h>
24 #include <vector>
25 #include <queue>
26 #include <stack>
27 
28 #define MAX_BYTES_IN_PACKET 10
29 
30 struct Packet {
31  uint8_t buffer[MAX_BYTES_IN_PACKET];
32  uint8_t numberOfBits;
33  uint8_t numberOfRepeats;
34  uint8_t currentBit;
35 }; // Packet
36 
37 struct SignalGenerator {
38  template<int timerIndex>
39  void configureSignal(String, uint8_t, uint16_t);
40 
41  bool IRAM_ATTR getNextBitToSend();
42  void loadPacket(std::vector<uint8_t>, int);
43  void waitForQueueEmpty();
44  bool isQueueEmpty();
45 
46  hw_timer_t *_fullCycleTimer;
47  hw_timer_t *_pulseTimer;
48  String _name;
49  uint8_t _directionPin;
50  int _currentMonitorPin;
51  std::queue<Packet *> _toSend;
52  std::queue<Packet *> _availablePackets;
53  Packet *_currentPacket;
54  // pre-encoded idle packet that gets sent when the _toSend queue is empty.
55  Packet _idlePacket = {
56  { 0xFF, 0xFF, 0xFD, 0xFE, 0x00, 0x7F, 0x80, 0x00, 0x00, 0x00 }, // packet bytes
57  49, // number of bits
58  0, // number of repeats
59  0 // current bit
60  };
61 };
62 
63 extern SignalGenerator dccSignalGenerators[2];
64 void startDCCSignalGenerators();
65 
66 int16_t readCV(const uint16_t);
67 bool writeProgCVByte(const uint16_t, const uint8_t);
68 bool writeProgCVBit(const uint16_t, const uint8_t, const bool);
69 void writeOpsCVByte(const uint16_t, const uint16_t, const uint8_t);
70 void writeOpsCVBit(const uint16_t, const uint16_t, const uint8_t, const bool);
71 
72 #define SIGNAL_GENERATOR_MAIN 0
73 #define SIGNAL_GENERATOR_PROG 1
74 
75 #endif
76 #endif