initial port to ms visual studio...

This commit is contained in:
Nic Newdigate
2023-01-24 18:05:46 +00:00
parent f9b2e9d838
commit 397e438f09
11 changed files with 169 additions and 11 deletions
+6 -2
View File
@@ -4,8 +4,8 @@ set(teensy_x86_stubs_VERSION 1.0.0)
set(CMAKE_CXX_STANDARD 11)
set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/include/teensy_x86_stubs/)
set(LIB_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/lib/teensy_x86_stubs )
set(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/include/teensy_x86_stubs/")
set(LIB_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/lib/teensy_x86_stubs" )
set(SOURCE_FILES
src/hardware_serial.cpp
@@ -16,6 +16,10 @@ set(SOURCE_FILES
src/core_pins.cpp
src/AudioStream.cpp)
if (MSVC)
set(SOURCE_FILES ${SOURCE_FILES} src/ctz_clz.cpp)
endif()
set(HEADER_FILES
src/hardware_serial.h
src/Arduino.h
+5 -2
View File
@@ -1,6 +1,9 @@
set(teensy_x86_stubs_VERSION "@teensy_x86_stubs_VERSION@")
set(teensy_x86_stubs_LIBS teensy_x86_stubs -L@LIB_INSTALL_DIR@)
if (MSVC)
set(teensy_x86_stubs_LIBS -L"@LIB_INSTALL_DIR@/teensy_x86_stubs.lib")
else()
set(teensy_x86_stubs_LIBS teensy_x86_stubs -L"@LIB_INSTALL_DIR@")
endif()
@PACKAGE_INIT@
set_and_check(teensy_x86_stubs_INCLUDE_DIR "@INCLUDE_INSTALL_DIR@")
+6 -2
View File
@@ -1,11 +1,15 @@
#include "Arduino.h"
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include <chrono>
#include <thread>
#include <mutex>
unsigned long t_start;
unsigned tv_start_unsigned;
unsigned t_start = 0;
unsigned tv_start_unsigned = 0;
bool has_initialized_mock_arduino = false;
using namespace std::chrono;
+4
View File
@@ -32,6 +32,10 @@
#include "Arduino.h"
#include "AudioStream.h"
#ifdef _MSC_VER
#include "ctz_clz.cpp"
#endif
#define MAX_AUDIO_MEMORY 229376
#define NUM_MASKS (((MAX_AUDIO_MEMORY / AUDIO_BLOCK_SAMPLES / 2) + 31) / 32)
+1 -1
View File
@@ -104,7 +104,7 @@ private:
size_t printNumberHex(unsigned long n);
size_t printNumberBin(unsigned long n);
size_t printNumberAny(unsigned long n, uint8_t base);
inline size_t printNumber(unsigned long n, uint8_t sign, uint8_t base) __attribute__((always_inline)) {
inline size_t printNumber(unsigned long n, uint8_t sign, uint8_t base) {
// when "base" is a constant (pretty much always), the
// compiler optimizes this to a single function call.
if (base == 0) return write((uint8_t)n);
+2 -2
View File
@@ -259,7 +259,7 @@ String Stream::readString(size_t max)
int c = timedRead();
if (c < 0) {
setReadError();
break; // timeout
continue;//break; // timeout
}
if (c == 0) break;
str += (char)c;
@@ -275,7 +275,7 @@ String Stream::readStringUntil(char terminator, size_t max)
int c = timedRead();
if (c < 0) {
setReadError();
break; // timeout
continue; //break; // timeout
}
if (c == 0 || c == terminator) break;
str += (char)c;
+11
View File
@@ -76,13 +76,24 @@ uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
return 0;
}
#ifdef _MSC_VER
__declspec(noreturn) void _reboot_Teensyduino_(void);
__declspec(noreturn) void _restart_Teensyduino_(void);
#else
void _reboot_Teensyduino_(void) __attribute__((noreturn));
void _restart_Teensyduino_(void) __attribute__((noreturn));
#endif // _MSC_VER
void yield(void);
#ifdef _MSC_VER
time_t t_start;
unsigned tv_start_unsigned;
#else
extern time_t t_start;
extern unsigned tv_start_unsigned;
#endif // _MSC_VER
using namespace std::chrono;
+5
View File
@@ -73,8 +73,13 @@ int touchRead(uint8_t pin);
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value);
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
#ifdef _MSC_VER
__declspec(noreturn) void _reboot_Teensyduino_(void);
__declspec(noreturn) void _restart_Teensyduino_(void);
#else
void _reboot_Teensyduino_(void) __attribute__((noreturn));
void _restart_Teensyduino_(void) __attribute__((noreturn));
#endif // _MSC_VER
void yield(void);
+49
View File
@@ -0,0 +1,49 @@
//https://gist.github.com/pps83/3210a2f980fd02bb2ba2e5a1fc4a2ef0
#ifdef _MSC_VER
#include <intrin.h>
#include <cstdint>
static inline int __builtin_ctz(unsigned x) {
unsigned long ret;
_BitScanForward(&ret, x);
return (int)ret;
}
static inline int __builtin_ctzll(unsigned long long x) {
unsigned long ret;
_BitScanForward64(&ret, x);
return (int)ret;
}
static inline int __builtin_ctzl(unsigned long x) {
return sizeof(x) == 8 ? __builtin_ctzll(x) : __builtin_ctz((uint32_t)x);
}
static inline int __builtin_clz(unsigned x) {
//unsigned long ret;
//_BitScanReverse(&ret, x);
//return (int)(31 ^ ret);
return (int)__lzcnt(x);
}
static inline int __builtin_clzll(unsigned long long x) {
//unsigned long ret;
//_BitScanReverse64(&ret, x);
//return (int)(63 ^ ret);
return (int)__lzcnt64(x);
}
static inline int __builtin_clzl(unsigned long x) {
return sizeof(x) == 8 ? __builtin_clzll(x) : __builtin_clz((uint32_t)x);
}
#ifdef __cplusplus
static inline int __builtin_ctzl(unsigned long long x) {
return __builtin_ctzll(x);
}
static inline int __builtin_clzl(unsigned long long x) {
return __builtin_clzll(x);
}
#endif
#endif
+62 -2
View File
@@ -22,14 +22,19 @@
#include <iostream>
#include <cstdio> // perror(), stderr, stdin, fileno()
#include <cstdarg>
#ifndef _MSC_VER
#include <termios.h>
#include <sys/ioctl.h>
#else
#include <Windows.h>
#include <conio.h>
#endif
#include "hardware_serial.h"
int kbhit(void) {
#ifndef _MSC_VER
static bool initflag = false;
static const int STDIN = 0;
if (!initflag) {
// Use termios to turn off line buffering
struct termios term;
@@ -40,9 +45,29 @@ int kbhit(void) {
initflag = true;
}
int nbbytes;
int nbbytes = 0;
ioctl(STDIN, FIONREAD, &nbbytes); // 0 is STDIN
return nbbytes;
#else
int nbbytes = 0;
nbbytes =_kbhit();
return nbbytes;
#endif
}
void HardwareSerial::InitializeConsole() {
#ifdef _MSC_VER
HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(hstdin, &_consoleMode);
SetConsoleMode(hstdin, 0);
#endif
}
void HardwareSerial::ResetConsole() {
#ifdef _MSC_VER
HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE);
SetConsoleMode(hstdin, _consoleMode);
#endif
}
void HardwareSerial::begin(unsigned long speed) {
@@ -73,10 +98,45 @@ int HardwareSerial::read(void) {
input.pop();
return c;
}
#ifndef _MSC_VER
if (!kbhit())
return -1;
char c = getchar();
return c;
#else
//char c = getchar();
DWORD cNumRead, fdwMode, i;
INPUT_RECORD irInBuf[1];
int counter = 0;
DWORD bufSize = 0;
// Get the standard input handle.
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
if (!GetNumberOfConsoleInputEvents(hStdin, &bufSize))
return -1;
if (bufSize == 0)
return -1;
if (!ReadConsoleInput(
hStdin, // input buffer handle
irInBuf, // buffer to read into
1, // size of read buffer
&cNumRead)) // number of records read
return -1;
if (cNumRead == 0) return -1;
switch (irInBuf[0].EventType)
{
case KEY_EVENT: // keyboard input
if (irInBuf[0].Event.KeyEvent.bKeyDown)
return irInBuf[0].Event.KeyEvent.uChar.AsciiChar;
else
return -1;
default:
return -1;
}
#endif
}
int HardwareSerial::availableForWrite(void) {
+18
View File
@@ -20,12 +20,25 @@
#ifndef HardwareSerial_h
#define HardwareSerial_h
#ifndef _MSC_VER
#include <termios.h> // struct termios, tcgetattr(), tcsetattr()
#endif
#include <queue>
#include "Stream.h"
class HardwareSerial : public Stream {
public:
HardwareSerial() : Stream() {
InitializeConsole();
}
virtual ~HardwareSerial() {
ResetConsole();
};
void InitializeConsole();
void ResetConsole();
void begin(unsigned long baud);
void begin(unsigned long, uint8_t);
void end();
@@ -48,8 +61,13 @@ public:
}
private:
#ifndef _MSC_VER
termios t;
termios t_saved;
#else
unsigned long _consoleMode;
#endif _MSC_VER
std::queue<char> input;
};