Last active
September 15, 2020 17:00
-
-
Save NeoCat/1d012f2a32f367f1fbe456053d8248fc to your computer and use it in GitHub Desktop.
Wireless Analog Terminal Bell using M5Stick-C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <WebServer.h> | |
#define OUTPUT_PIN 26 | |
char ssid[] = "YOUR-WIFI-SSID"; | |
char pass[] = "YOUR-WIFI-PASSWORD"; | |
WebServer server(80); | |
void setup() { | |
pinMode(OUTPUT_PIN, OUTPUT); | |
digitalWrite(OUTPUT_PIN, LOW); | |
Serial.begin(115200); | |
WiFi.begin(); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, pass); | |
unsigned long start = millis(); | |
while (WiFi.status() != WL_CONNECTED && millis() - start < 30000) { | |
Serial.print("."); | |
delay(1000); | |
} | |
if (WiFi.status() != WL_CONNECTED) { | |
Serial.println("failed to connect WiFi."); | |
delay(1000); | |
ESP.restart(); | |
} | |
Serial.println(WiFi.localIP()); | |
server.on("/", []() { | |
int wait = 50; | |
String wait_s = server.arg("wait"); | |
if (wait_s != "") | |
wait = wait_s.toInt(); | |
digitalWrite(OUTPUT_PIN, HIGH); | |
delay(wait); | |
digitalWrite(OUTPUT_PIN, LOW); | |
server.send(200, "text/plain", "OK\n"); | |
}); | |
server.onNotFound([]{ server.send(404, "text/plain", "Not Found"); }); | |
server.begin(); | |
} | |
void loop() { | |
server.handleClient(); | |
if (WiFi.status() != WL_CONNECTED) { | |
Serial.println("disconnected."); | |
delay(1000); | |
ESP.restart(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'pty' | |
require 'open-uri' | |
BELL_URL = 'http://192.168.0.123/' # Change this to your ESP32 address | |
# Avoid double proxy | |
if ENV['PTY_PROXY'] | |
warn "pty-proxy is already active!" | |
exec ENV['SHELL'] | |
end | |
ENV['PTY_PROXY'] = 'ACTIVE' | |
# Track escape sequences to avoid removing "\a" in them | |
@esc_seq = 0 | |
@osc_mode = false | |
# Remove "\a" from buf (unless it is in escape sequences) | |
def remove_bell(buf) | |
buf2 = String.new(capacity: 4096) | |
buf.each_char do |c| | |
if @esc_seq == 0 && c == "\a" | |
Thread.new do | |
open(BELL_URL).close rescue nil | |
end | |
else | |
buf2 << c | |
end | |
if @esc_seq > 0 | |
@osc_mode = true if @esc_seq == 1 && c == "]" | |
if c.match(@osc_mode ? /[\a\x1b]/ : /[^0-9;?\[]/) | |
@esc_seq = 0 | |
@osc_mode = false | |
else | |
@esc_seq += 1 | |
end | |
elsif c == "\x1b" | |
@esc_seq = 1 | |
end | |
end | |
buf2 | |
end | |
# Launch shell in pty | |
Signal.trap(:CHLD, 'IGNORE') | |
system "stty raw -echo 2> /dev/null" | |
size = %x(stty size).split(/\s+/) | |
p = PTY.spawn %(stty rows #{size[0]} cols #{size[1]}; exec '#{ENV['SHELL']}') | |
# When window size is changed, notify it to the shell | |
size_changed = IO.pipe | |
Signal.trap(:WINCH) do | |
size_changed[1].write ' ' | |
end | |
# Proxy input/output | |
@buf = String.new(capacity: 4096) | |
until p[0].closed? || STDIN.closed? | |
s = IO.select [p[0], STDIN, size_changed[0]] | |
if s[0][0] == size_changed[0] | |
size_changed[0].readpartial(4096) | |
size = %x(stty size).split(/\s+/) | |
system("stty rows #{size[0]} cols #{size[1]}", in: p[0]) | |
Process.kill :WINCH, p[2] | |
next | |
end | |
while s0 = s[0][0] | |
s[0].shift | |
out = s0 == STDIN ? out = p[1] : out = STDOUT | |
begin | |
buf = s0.readpartial(4096, @buf) | |
buf = remove_bell(buf) if s0 == p[0] | |
out.write buf | |
out.flush | |
rescue EOFError | |
p[0].close | |
p[1].close | |
system "stty -raw 2> /dev/null" | |
exit | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment