-
Notifications
You must be signed in to change notification settings - Fork 0
/
providers.py
69 lines (51 loc) · 1.69 KB
/
providers.py
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
import abc
from enum import Enum
import lark
class WireMode(Enum):
Input = "INPUT"
Output = "OUTPUT"
class Provider(abc.ABC):
@abc.abstractmethod
def codegen_get_micros(self):
pass
@abc.abstractmethod
def codegen_time_type(self):
pass
@abc.abstractmethod
def codegen_wire_type(self):
pass
@abc.abstractmethod
def codegen_wire_set_mode(self, wire_id: lark.Token, mode: WireMode):
pass
@abc.abstractmethod
def codegen_wire_write_bit(self, wire_id: lark.Token, expr: str):
pass
@abc.abstractmethod
def codegen_wire_read_bit(self, wire_id: lark.Token):
pass
@abc.abstractmethod
def header_header(self):
pass
class ArduinoProvider(Provider):
def __init__(self, denominator=4) -> None:
super().__init__()
self.denominator = denominator
def codegen_get_micros(self):
return "micros()"
def codegen_time_type(self):
return "unsigned long"
def codegen_wire_type(self):
return "int"
def codegen_wire_set_mode(self, wire_id: lark.Token, mode: WireMode):
return f"pinMode({wire_id}, {mode.value})"
def codegen_wire_write_bit(self, wire_id: lark.Token, expr: str):
return f"digitalWrite({wire_id}, {expr})"
def codegen_wire_read_bit(self, wire_id: lark.Token):
return f"digitalRead({wire_id})"
def header_header(self):
return """#include <Arduino.h>
#include <stdint.h>
"""
class NoisyArduinoProvider(ArduinoProvider):
def codegen_wire_write_bit(self, wire_id: lark.Token, expr: str):
return f"digitalWrite({wire_id}, (!!({expr})) ^ (random({self.denominator})==0))"