New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Analog Terminal Bell Support #428
Conversation
I really have no idea what I'm doing, this is the first time I've modified a real app for macOS
I get that this is a joke, but you just gave me an idea to bake the bell into QMK for those with a buzzer/speaker on their keyboards. Might this PR be modified slightly to instead allow iTerm2 to call a command for bell? That way it could be a script to ring the Analog Terminal Bell (via GPIO) or play a tone on your keyboard (via HID to QMK). Bonus would be not having to ship HID support in iTerm directly. |
Ya, I was thinking that if we wanted to take this patch seriously, maybe adding a way to plug in to the bell logic would make sense. I don't know the best way to do that though. My fear is that shelling out would be too slow, but the code that I wrote for this patch is extremely inefficient (it looks up the USB device on every bell activation!) and as you can see from the video the response time was fine. (Anyway, option specifically for the Analog Terminal Bell ™ is definitely not the best 😆) |
LOL this is awesome. If you sold these I would definitely buy one. I think adding a bell interface to the Python API is a somewhat saner way of adding this feature. Commit cbb7c96 adds a You'll need the current build from master or tomorrow's nightly build (due out in about 24 hours because I missed the deadline).
|
@gnachman LOL!! Thank you so much!!! |
After installing the script (see bottom) in the standard iTerm (not nightly or latest build), with
#!/usr/bin/env python3.7
import iterm2
from PyMCP2221A import PyMCP2221A
import time
gpio = PyMCP2221A.PyMCP2221A()
gpio.GPIO_Init()
gpio.GPIO_0_OutputMode()
gpio.GPIO_0_Output(0)
async def main(connection):
app = await iterm2.async_get_app(connection)
async def my_task(session_id):
async with iterm2.VariableMonitor(connection, iterm2.VariableScopes.SESSION, "bellCount", session_id) as mon:
while True:
await mon.async_get()
gpio.GPIO_0_Output(1)
time.sleep(.1)
gpio.GPIO_0_Output(0)
await (iterm2.EachSessionOnceMonitor.
async_foreach_session_create_task(app, my_task))
iterm2.run_forever(main) We are so close! |
It's hacky, but here's a workaround to start up a Python HTTP server outside of the iTerm script, then have the iTerm script send a request to it at every bell. Server: from http.server import BaseHTTPRequestHandler, HTTPServer
from PyMCP2221A import PyMCP2221A
import time
hostName = "localhost"
serverPort = 8080
gpio = PyMCP2221A.PyMCP2221A()
gpio.GPIO_Init()
gpio.GPIO_0_OutputMode()
gpio.GPIO_0_Output(0)
class MyServer(BaseHTTPRequestHandler):
def do_GET(self):
gpio.GPIO_0_Output(1)
time.sleep(.1)
gpio.GPIO_0_Output(0)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes("<html><head><title>Ring the Bell</title></head>", "utf-8"))
self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8"))
self.wfile.write(bytes("<body>", "utf-8"))
self.wfile.write(bytes("<p>The bell has rang.</p>", "utf-8"))
self.wfile.write(bytes("</body></html>", "utf-8"))
if __name__ == "__main__":
webServer = HTTPServer((hostName, serverPort), MyServer)
print("Server started http://%s:%s" % (hostName, serverPort))
try:
webServer.serve_forever()
except KeyboardInterrupt:
pass
webServer.server_close()
print("Server stopped.") Install dependencies: iTerm Script: #!/usr/bin/env python3
import iterm2
import requests
async def main(connection):
app = await iterm2.async_get_app(connection)
async def my_task(session_id):
async with iterm2.VariableMonitor(connection, iterm2.VariableScopes.SESSION, "bellCount", session_id) as mon:
while True:
await mon.async_get()
x = requests.get('http://localhost:8080/')
print(x.status_code)
await (iterm2.EachSessionOnceMonitor.
async_foreach_session_create_task(app, my_task))
iterm2.run_forever(main) Add |
I filed trezor/cython-hidapi#95. If that doesn't work out someone should fork hidapi and add |
Following the rabbit hole, now we need to get libusb/hidapi#196 fixed. |
libusb/hidapi#198 closes the hidapi issue. Now just waiting for cython-hidapi to pull it in. |
@tenderlove cython-hidapi has bumped the hidapi version in their master. Want to try again? |
FYI trying to build and install into Application Support is a nightmare because Python gets confused by the space in the directory name. Hopefully he bumps the pypi package soon and then it can just be installed by pip3 as usual. |
Just to follow up on this - I finally put together the parts I bought several months ago this current weekend, and with the changes above in place, it does totally work without any additional messing about 🎉 The script @zvakanaka posted basically does it, but with one tweak of changing the sleep time to Thanks to everyone for enabling this, it's quite fun 🛎️ 😄 |
@rufo Awesome! Share a video! |
This PR adds support for the Analog Terminal Bell. Have you ever made a mistake in your shell only to notice that you accidentally missed the terminal bell notification? Never miss a terminal bell notification again with the Analog Terminal Bell!
Here is a short video explaining the Analog Terminal Bell:
This patch adds Analog Terminal Bell support to iTerm2.
(Note: this PR is just a joke. Adding support would be awesome, but I just made this PR as a way to make people smile. Also I have no idea how to correctly do macOS development, so this PR is probably not the best 😅)