Skip to content

Commit 195ee2a

Browse files
committed
Add serial port support
1 parent 642f4c1 commit 195ee2a

File tree

283 files changed

+478622
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

283 files changed

+478622
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616
- Support for WString
1717
- Support for Print
1818
- Support for Stream (backed by a String implementation)
19+
- All the IO stuff (pins, serial port support flags, etc) from the Arduino library
20+
- Support for Serial (backed by GODMODE)
1921

2022
### Changed
2123
- Made `wget` have quieter output

cpp/arduino/Arduino.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ Where possible, variable names from the Arduino library are used to avoid confli
88
// Chars and strings
99

1010
#include "ArduinoDefines.h"
11-
#include "Godmode.h"
1211

1312
#include "WCharacter.h"
1413
#include "WString.h"
1514
#include "Print.h"
1615
#include "Stream.h"
16+
#include "HardwareSerial.h"
1717

1818
typedef bool boolean;
1919
typedef uint8_t byte;
@@ -23,6 +23,9 @@ typedef uint8_t byte;
2323
// Math and Trig
2424
#include "AvrMath.h"
2525

26+
#include "Godmode.h"
27+
28+
2629
// Bits and Bytes
2730
#define bit(b) (1UL << (b))
2831
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))

cpp/arduino/ArduinoDefines.h

+2
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,5 @@
8686
#define TIMER5A 16
8787
#define TIMER5B 17
8888
#define TIMER5C 18
89+
90+
#include "include/io.h"

cpp/arduino/Godmode.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "Godmode.h"
2+
#include "HardwareSerial.h"
23

34
GodmodeState godmode = GodmodeState();
45

@@ -44,3 +45,18 @@ long random(long vmin, long vmax)
4445
{
4546
return vmin < vmax ? (random(vmax - vmin) + vmin) : vmin;
4647
}
48+
49+
// Serial ports
50+
#if defined(HAVE_HWSERIAL0)
51+
HardwareSerial Serial(&godmode.serialPort[0].dataIn, &godmode.serialPort[0].dataOut, &godmode.serialPort[0].readDelayMicros);
52+
#endif
53+
#if defined(HAVE_HWSERIAL1)
54+
HardwareSerial Serial1(&godmode.serialPort[1].dataIn, &godmode.serialPort[1].dataOut, &godmode.serialPort[1].readDelayMicros);
55+
#endif
56+
#if defined(HAVE_HWSERIAL2)
57+
HardwareSerial Serial2(&godmode.serialPort[2].dataIn, &godmode.serialPort[2].dataOut, &godmode.serialPort[2].readDelayMicros);
58+
#endif
59+
#if defined(HAVE_HWSERIAL3)
60+
HardwareSerial Serial3(&godmode.serialPort[3].dataIn, &godmode.serialPort[3].dataOut, &godmode.serialPort[3].readDelayMicros);
61+
#endif
62+

cpp/arduino/Godmode.h

+24
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
11
#pragma once
22
#include "ArduinoDefines.h"
3+
#include "WString.h"
34

45
#define MOCK_PINS_COUNT 256
56

7+
#if defined(UBRR3H)
8+
#define NUM_SERIAL_PORTS 4
9+
#elif defined(UBRR2H)
10+
#define NUM_SERIAL_PORTS 3
11+
#elif defined(UBRR1H)
12+
#define NUM_SERIAL_PORTS 2
13+
#elif defined(UBRRH) || defined(UBRR0H)
14+
#define NUM_SERIAL_PORTS 1
15+
#else
16+
#define NUM_SERIAL_PORTS 0
17+
#endif
18+
19+
620
class GodmodeState {
21+
struct PortDef {
22+
String dataIn;
23+
String dataOut;
24+
unsigned long readDelayMicros;
25+
};
26+
727
public:
828
unsigned long micros;
929
unsigned long seed;
1030
// not going to put pinmode here unless its really needed. can't think of why it would be
1131
bool digitalPin[MOCK_PINS_COUNT];
1232
int analogPin[MOCK_PINS_COUNT];
33+
struct PortDef serialPort[NUM_SERIAL_PORTS];
1334

1435
void resetPins() {
1536
for (int i = 0; i < MOCK_PINS_COUNT; ++i) {
@@ -30,6 +51,9 @@ class GodmodeState {
3051

3152
GodmodeState() {
3253
reset();
54+
for (int i = 0; i < NUM_SERIAL_PORTS; ++i) {
55+
serialPort[i] = {"", "", 0};
56+
}
3357
}
3458

3559
};

cpp/arduino/HardwareSerial.h

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#pragma once
2+
3+
//#include <inttypes.h>
4+
#include "Stream.h"
5+
6+
// definitions neeeded for Serial.begin's config arg
7+
#define SERIAL_5N1 0x00
8+
#define SERIAL_6N1 0x02
9+
#define SERIAL_7N1 0x04
10+
#define SERIAL_8N1 0x06
11+
#define SERIAL_5N2 0x08
12+
#define SERIAL_6N2 0x0A
13+
#define SERIAL_7N2 0x0C
14+
#define SERIAL_8N2 0x0E
15+
#define SERIAL_5E1 0x20
16+
#define SERIAL_6E1 0x22
17+
#define SERIAL_7E1 0x24
18+
#define SERIAL_8E1 0x26
19+
#define SERIAL_5E2 0x28
20+
#define SERIAL_6E2 0x2A
21+
#define SERIAL_7E2 0x2C
22+
#define SERIAL_8E2 0x2E
23+
#define SERIAL_5O1 0x30
24+
#define SERIAL_6O1 0x32
25+
#define SERIAL_7O1 0x34
26+
#define SERIAL_8O1 0x36
27+
#define SERIAL_5O2 0x38
28+
#define SERIAL_6O2 0x3A
29+
#define SERIAL_7O2 0x3C
30+
#define SERIAL_8O2 0x3E
31+
32+
class HardwareSerial : public Stream
33+
{
34+
protected:
35+
String* mGodmodeDataOut;
36+
37+
public:
38+
HardwareSerial(String* dataIn, String* dataOut, unsigned long* delay): Stream() {
39+
mGodmodeDataIn = dataIn;
40+
mGodmodeDataOut = dataOut;
41+
mGodmodeMicrosDelay = delay;
42+
}
43+
void begin(unsigned long baud) { begin(baud, SERIAL_8N1); }
44+
void begin(unsigned long baud, uint8_t config) {
45+
*mGodmodeMicrosDelay = 1000000 / baud;
46+
}
47+
void end() {}
48+
49+
// virtual int available(void);
50+
// virtual int peek(void);
51+
// virtual int read(void);
52+
// virtual int availableForWrite(void);
53+
// virtual void flush(void);
54+
virtual size_t write(uint8_t aChar) { mGodmodeDataOut->append(String((char)aChar)); return 1; }
55+
56+
inline size_t write(unsigned long n) { return write((uint8_t)n); }
57+
inline size_t write(long n) { return write((uint8_t)n); }
58+
inline size_t write(unsigned int n) { return write((uint8_t)n); }
59+
inline size_t write(int n) { return write((uint8_t)n); }
60+
// using Print::write; // pull in write(str) and write(buf, size) from Print
61+
operator bool() { return true; }
62+
63+
};
64+
65+
#if defined(UBRRH) || defined(UBRR0H)
66+
extern HardwareSerial Serial;
67+
#define HAVE_HWSERIAL0
68+
#endif
69+
#if defined(UBRR1H)
70+
extern HardwareSerial Serial1;
71+
#define HAVE_HWSERIAL1
72+
#endif
73+
#if defined(UBRR2H)
74+
extern HardwareSerial Serial2;
75+
#define HAVE_HWSERIAL2
76+
#endif
77+
#if defined(UBRR3H)
78+
extern HardwareSerial Serial3;
79+
#define HAVE_HWSERIAL3
80+
#endif
81+

cpp/arduino/Stream.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ class Stream : public Print
4848
virtual int peek() { return available() ? (int)((*mGodmodeDataIn)[0]) : -1; }
4949
virtual int read() {
5050
int ret = peek();
51-
if (ret != -1) fastforward(1);
51+
if (ret != -1) {
52+
fastforward(1);
53+
if (mGodmodeMicrosDelay) delayMicroseconds(*mGodmodeMicrosDelay);
54+
}
5255
return ret;
5356
}
5457

@@ -152,6 +155,7 @@ class Stream : public Print
152155
// returns the number of characters placed in the buffer (0 means no valid data found)
153156
size_t readBytes(char *buffer, size_t length) {
154157
size_t ret = mGodmodeDataIn->copy(buffer, length);
158+
if (mGodmodeMicrosDelay) delayMicroseconds(*mGodmodeMicrosDelay * ret);
155159
fastforward(ret);
156160
return ret;
157161
}

cpp/arduino/include/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
IO defintions.
2+
3+
All of it copied from `arduino-1.8.5/hardware/tools/avr/avr/include/avr`

0 commit comments

Comments
 (0)