-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathArduino.cpp
93 lines (73 loc) · 2.15 KB
/
Arduino.cpp
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
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright (c) 2019 Brian T. Park
*
* Parts derived from the Arduino SDK
* Copyright (c) 2005-2013 Arduino Team
*
* Parts inspired by [Entering raw
* mode](https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html).
*
* Parts inspired by [ESP8266 Host
* Emulation](https://github.com/esp8266/Arduino/tree/master/tests/host).
*
*/
#include <inttypes.h>
#include <unistd.h> // usleep()
#include <time.h> // clock_gettime()
#include "Arduino.h"
// -----------------------------------------------------------------------
// Arduino methods emulated in Unix
// -----------------------------------------------------------------------
static uint32_t digitalPinValues = 0;
void yield() {
usleep(1000); // prevents program from consuming 100% CPU
}
void pinMode(uint8_t /*pin*/, uint8_t /*mode*/) {}
void digitalWrite(uint8_t /*pin*/, uint8_t /*val*/) {}
int digitalRead(uint8_t pin) {
if (pin >= 32) return 0;
return (digitalPinValues & (((uint32_t)0x1) << pin)) != 0;
}
void digitalReadValue(uint8_t pin, uint8_t val) {
if (pin >= 32) return;
if (val == 0) {
digitalPinValues &= ~(((uint32_t)0x1) << pin);
} else {
digitalPinValues |= ((uint32_t)0x1) << pin;
}
}
int analogRead(uint8_t /*pin*/) { return 0; }
void analogWrite(uint8_t /*pin*/, int /*val*/) {}
unsigned long millis() {
struct timespec spec;
clock_gettime(CLOCK_MONOTONIC, &spec);
unsigned long ms = spec.tv_sec * 1000U + spec.tv_nsec / 1000000UL;
return ms;
}
unsigned long micros() {
struct timespec spec;
clock_gettime(CLOCK_MONOTONIC, &spec);
unsigned long us = spec.tv_sec * 1000000UL + spec.tv_nsec / 1000U;
return us;
}
void delay(unsigned long ms) {
usleep(ms * 1000);
}
void delayMicroseconds(unsigned int us) {
usleep(us);
}
unsigned long pulseIn(
uint8_t /*pin*/, uint8_t /*state*/, unsigned long /*timeout*/) {
return 0;
}
unsigned long pulseInLong(
uint8_t /*pin*/, uint8_t /*state*/, unsigned long /*timeout*/) {
return 0;
}
void shiftOut(
uint8_t /*dataPin*/, uint8_t /*clockPin*/, uint8_t /*bitOrder*/,
uint8_t /*val*/) {}
uint8_t shiftIn(
uint8_t /*dataPin*/, uint8_t /*clockPin*/, uint8_t /*bitOrder*/) {
return 0;
}