Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/libsnes/bsnes/nall/snes/usart.hpp
2 views
1
#ifndef NALL_SNES_USART_HPP
2
#define NALL_SNES_USART_HPP
3
4
#include <nall/platform.hpp>
5
#include <nall/function.hpp>
6
#include <nall/serial.hpp>
7
#include <nall/stdint.hpp>
8
9
#define usartproc dllexport
10
11
static nall::function<void (unsigned milliseconds)> usart_usleep;
12
static nall::function<uint8_t ()> usart_read;
13
static nall::function<void (uint8_t data)> usart_write;
14
15
extern "C" usartproc void usart_init(
16
nall::function<void (unsigned milliseconds)> usleep,
17
nall::function<uint8_t ()> read,
18
nall::function<void (uint8_t data)> write
19
) {
20
usart_usleep = usleep;
21
usart_read = read;
22
usart_write = write;
23
}
24
25
extern "C" usartproc void usart_main();
26
27
//
28
29
static nall::serial usart;
30
static bool usart_is_virtual = true;
31
32
static bool usart_virtual() {
33
return usart_is_virtual;
34
}
35
36
//
37
38
static void usarthw_usleep(unsigned milliseconds) {
39
usleep(milliseconds);
40
}
41
42
static uint8_t usarthw_read() {
43
while(true) {
44
uint8_t buffer[1];
45
signed length = usart.read((uint8_t*)&buffer, 1);
46
if(length > 0) return buffer[0];
47
}
48
}
49
50
static void usarthw_write(uint8_t data) {
51
uint8_t buffer[1] = { data };
52
usart.write((uint8_t*)&buffer, 1);
53
}
54
55
int main(int argc, char **argv) {
56
bool result = false;
57
if(argc == 1) result = usart.open("/dev/ttyACM0", 57600, true);
58
if(argc == 2) result = usart.open(argv[1], 57600, true);
59
if(result == false) {
60
printf("error: unable to open USART hardware device\n");
61
return 0;
62
}
63
usart_is_virtual = false;
64
usart_init(usarthw_usleep, usarthw_read, usarthw_write);
65
usart_main();
66
usart.close();
67
return 0;
68
}
69
70
#endif
71
72