-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
215 lines (175 loc) · 4.71 KB
/
main.go
File metadata and controls
215 lines (175 loc) · 4.71 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//go:build tinygo
// +build tinygo
package main
import (
"machine"
"time"
"github.com/refaktor/picorye/env"
"github.com/refaktor/picorye/evaldo"
)
// Buffer for incoming data
var rxBuffer = make([]byte, 1024)
var rxIndex = 0
// Command prompt
var prompt = "rye> "
// Global program state
var ps *env.ProgramState
func main() {
// Initialize serial communication
machine.Serial.Configure(machine.UARTConfig{
BaudRate: 115200,
})
// Wait for USB serial to be ready
time.Sleep(3 * time.Second)
// Initialize Rye environment
initRye()
// Print welcome message
printWelcome()
// Main loop
for {
// Read from serial
if machine.Serial.Buffered() > 0 {
data, _ := machine.Serial.ReadByte()
// Handle backspace
if data == 8 || data == 127 { // Backspace or Delete
if rxIndex > 0 {
rxIndex--
machine.Serial.Write([]byte{8, 32, 8}) // Backspace, space, backspace
}
continue
}
// Echo character
machine.Serial.WriteByte(data)
// Handle newline
if data == 13 || data == 10 { // CR or LF
machine.Serial.Write([]byte{10, 13}) // Send CRLF
// Process command if buffer has data
if rxIndex > 0 {
command := string(rxBuffer[:rxIndex])
processCommand(command)
rxIndex = 0 // Reset buffer
}
// Print prompt
machine.Serial.Write([]byte(prompt))
continue
}
// Add to buffer if not full
if rxIndex < len(rxBuffer) {
rxBuffer[rxIndex] = data
rxIndex++
}
}
// Small delay to prevent CPU hogging
time.Sleep(10 * time.Millisecond)
}
}
func printWelcome() {
welcomeMsg := "\r\n" +
"=================================\r\n" +
"PicoRye Language for Raspberry Pi Pico\r\n" +
"=================================\r\n" +
"Type 'help' for available commands\r\n" +
"Type 'exit' to restart\r\n\r\n"
machine.Serial.Write([]byte(welcomeMsg))
machine.Serial.Write([]byte(prompt))
}
func initRye() {
// Initialize program state
ps = env.NewProgramStateNEW()
// Register TinyGo builtins
evaldo.RegisterBuiltinsTinyGo(ps)
// Set up context
ctx := ps.Ctx
ps.Ctx = env.NewEnv(ctx)
// Register Pico-specific builtins
registerPicoBuiltins()
}
func registerPicoBuiltins() {
// Register Pico-specific builtins like GPIO control
// This will be implemented in a separate file
}
func processCommand(cmd string) {
// Trim spaces
cmd = trimSpaces(cmd)
// Handle special commands
if cmd == "exit" {
machine.Serial.Write([]byte("Restarting...\r\n"))
time.Sleep(500 * time.Millisecond)
// Simulate a reset by entering an infinite loop
// The watchdog will eventually reset the device
for {
// Do nothing
}
return
} else if cmd == "help" {
showHelp()
return
} else if cmd == "blink" {
blinkLED()
return
} else if cmd == "gpio-on" {
gpioOn()
return
} else if cmd == "gpio-off" {
gpioOff()
return
}
// Parse and evaluate the input as Rye code
result := evaldo.MinimalEyrEvalString(cmd, ps)
machine.Serial.Write([]byte(result.Inspect(*ps.Idx) + "\r\n"))
}
func trimSpaces(s string) string {
// Simple trim implementation for TinyGo
start := 0
end := len(s)
// Trim leading spaces
for start < end && (s[start] == ' ' || s[start] == '\t' || s[start] == '\n' || s[start] == '\r') {
start++
}
// Trim trailing spaces
for end > start && (s[end-1] == ' ' || s[end-1] == '\t' || s[end-1] == '\n' || s[end-1] == '\r') {
end--
}
return s[start:end]
}
func showHelp() {
helpText := "Available commands:\r\n" +
" help - Show this help\r\n" +
" exit - Restart the system\r\n" +
" blink - Blink the onboard LED 10 times\r\n" +
" gpio-on - Turn on the onboard LED\r\n" +
" gpio-off - Turn off the onboard LED\r\n" +
"\r\nBuiltin functions:\r\n" +
" add - Adds two numbers together (e.g., '3 5 add')\r\n" +
"\r\nAny other input will be parsed and evaluated as Rye code\r\n"
machine.Serial.Write([]byte(helpText))
}
func blinkLED() {
// Configure GPIO 25 (onboard LED on most Pico boards)
led := machine.LED
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.Serial.Write([]byte("Blinking LED 10 times...\r\n"))
// Blink 10 times
for i := 0; i < 10; i++ {
led.High()
time.Sleep(500 * time.Millisecond)
led.Low()
time.Sleep(500 * time.Millisecond)
machine.Serial.Write([]byte("Blink!\r\n"))
}
machine.Serial.Write([]byte("Done blinking\r\n"))
}
func gpioOn() {
// Configure GPIO 25 (onboard LED on most Pico boards)
led := machine.GPIO25
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
led.High()
machine.Serial.Write([]byte("LED turned ON\r\n"))
}
func gpioOff() {
// Configure GPIO 25 (onboard LED on most Pico boards)
led := machine.GPIO25
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
led.Low()
machine.Serial.Write([]byte("LED turned OFF\r\n"))
}