Last active
November 30, 2025 01:30
-
-
Save foone/086523ea41d6b9da7caf17dfc0cfd45d to your computer and use it in GitHub Desktop.
Detect if a floppy drive has had a disk inserted/removed, by monitoring the pin34 (/DSKCHG) line
This file contains hidden or 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
| import subprocess,time,sys | |
| from greaseweazle.tools import util | |
| from greaseweazle import error | |
| from greaseweazle import usb as USB | |
| from greaseweazle.flux import Flux | |
| DEVICE=None | |
| DRIVE=util.Drive()('2') | |
| import time | |
| from contextlib import contextmanager | |
| @contextmanager | |
| def report_duration(do_what=None): | |
| now = time.time() | |
| try: | |
| yield | |
| finally: | |
| if do_what is None: | |
| do_what='run' | |
| print(f'it took {time.time() - now:0.2f}s to {do_what}') | |
| def noclick_step(usb: USB.Unit) -> None: | |
| usb._send_cmd(struct.pack("2B", USB.Cmd.NoClickStep, 2)) | |
| def seek(): | |
| try: | |
| usb = util.usb_open(DEVICE) | |
| util.with_drive_selected((lambda:usb.noclick_step()), usb, DRIVE, | |
| motor=False) | |
| except USB.CmdError as err: | |
| print("Command Failed: %s" % err) | |
| class pin_reader: | |
| def __init__(self,usb,pin): | |
| self.usb = usb | |
| self.pin = pin | |
| self.value = None | |
| def __call__(self): | |
| self.value = self.usb.get_pin(self.pin) | |
| def read_pin(pin): | |
| try: | |
| usb = util.usb_open(DEVICE) | |
| pinr = pin_reader(usb, 34) | |
| util.with_drive_selected(pinr, usb, DRIVE,motor=False) | |
| return pinr.value | |
| except USB.CmdError as error: | |
| print("Command Failed: %s" % error) | |
| def bread_pin(pin): | |
| ret=gw('pin','get',pin) | |
| return b'Low' not in ret | |
| def read34(): | |
| return read_pin(34) | |
| def monitor_pins(): | |
| last_status=read34() | |
| while True: | |
| #with report_duration('check for floppy'): | |
| seek() | |
| status=read34() | |
| if status!=last_status: | |
| if status: | |
| print('Floppy detected! YAAAAY! ^_^') | |
| else: | |
| print('No more floppy. I feel so empty ;_;') | |
| last_status=status | |
| if __name__=='__main__': | |
| #wait_for_floppy() | |
| monitor_pins() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment