From 85ce700898392cb92952b9b417de2f1727d5ed0a Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sat, 14 Jun 2025 23:26:52 +0800 Subject: refactor: flipper-ac --- flipper-ac.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 6 deletions(-) mode change 100644 => 100755 flipper-ac.py diff --git a/flipper-ac.py b/flipper-ac.py old mode 100644 new mode 100755 index 73734c2..9c3ab96 --- a/flipper-ac.py +++ b/flipper-ac.py @@ -1,20 +1,84 @@ +#!/usr/bin/env python3 + from pyflipper import PyFlipper from sys import argv +from xdg.BaseDirectory import xdg_cache_home +from os import system +from enum import Enum +from contextlib import suppress + +class status_t(Enum): + ON = "" + OFF = "󱓣" + TURNING_ON = "󱋋" + TURNING_OFF = "󰼩" ac_open = [3538,1661,502,392,474,1257,475,390,450,413,450,414,449,414,449,415,449,413,450,413,475,389,475,389,475,388,475,391,475,1257,474,391,472,391,471,392,470,395,468,420,444,419,444,423,444,1288,443,1289,444,1289,443,419,444,423,443,1290,444,419,444,419,444,419,444,420,444,419,444,419,444,419,444,421,443,419,444,419,444,419,444,420,444,419,444,419,444,420,443,421,443,419,444,420,443,419,444,421,443,420,443,420,443,420,443,421,443,420,443,420,443,420,443,421,443,420,443,423,444,1288,443,1290,444,420,443,420,443,420,443,421,443,423,443,9965,3483,1722,443,423,443,1290,442,421,443,420,443,420,443,420,443,422,442,421,442,421,442,420,443,422,442,421,442,424,443,1290,442,422,442,421,442,421,442,421,442,422,442,421,442,425,442,1289,442,1291,442,1291,441,421,442,425,441,1292,442,421,442,421,442,422,441,423,441,422,441,422,441,422,441,422,442,422,441,422,441,422,441,423,441,425,442,1291,442,422,441,422,441,1290,441,1290,441,1291,442,423,441,422,441,426,441,1290,442,1293,441,426,441,1291,440,1292,441,423,441,422,441,423,440,423,440,424,440,423,440,423,440,423,440,423,440,1291,441,1292,441,423,440,425,439,427,440,1292,439,1293,440,425,439,424,439,425,438,449,414,450,414,449,414,449,414,449,414,450,414,449,414,449,414,449,414,450,414,449,414,450,413,449,414,451,414,450,413,453,413,1318,414,1319,413,1319,414,450,413,450,413,451,413,450,413,451,412,450,413,452,412,451,412,454,413,1319,413,1320,413,1320,412,451,412,451,412,453,411,452,411,452,411,452,411,454,410,453,410,453,410,478,385,479,385,478,385,478,385,478,385,479,385,482,385,1347,385,1348,385,480,384,478,385,479,384,479,385,479,384,1348,385,479,384,479,384,481,383,483,384,1349,384,483,383,1349,384,1349,384,483,383,1350,383,481,383,481,382,482,382,481,358,506,358,534,332,1375,358,531,332,531,332,1375,358,535,331,1400,332,1402,332,533,332] ac_close = [3519,1714,451,417,450,1281,452,414,450,413,450,413,450,414,449,414,450,413,450,412,451,412,451,413,451,412,451,416,475,1256,477,389,475,389,473,392,470,417,445,419,445,419,444,423,443,1288,443,1289,444,1289,444,419,444,423,443,1290,444,420,443,420,443,419,444,421,443,419,444,420,443,420,443,421,443,420,443,420,443,420,443,421,443,420,443,420,443,420,443,421,443,420,443,420,443,420,443,421,443,420,443,421,442,420,443,421,443,421,442,420,443,421,442,422,442,421,442,424,443,1289,442,1292,442,421,442,421,442,421,442,422,442,424,442,9969,3484,1720,440,426,441,1292,440,424,440,423,440,422,441,423,440,424,441,422,441,422,441,422,441,424,441,422,441,426,441,1292,441,424,441,422,441,423,440,423,440,424,440,423,440,426,440,1292,439,1293,440,1293,440,423,440,427,440,1294,439,424,440,424,439,423,440,449,415,448,415,448,415,448,415,449,415,448,415,448,415,425,438,449,415,448,415,448,415,448,415,448,415,1316,415,1317,414,1318,415,450,415,448,415,451,416,1317,415,1319,415,451,416,1292,440,1292,442,422,443,420,444,420,443,419,444,420,444,419,444,419,444,419,444,419,444,1288,444,1288,444,419,444,420,444,422,445,1287,445,1288,445,419,445,418,445,394,470,394,469,395,469,394,469,394,469,393,470,394,471,393,470,393,470,393,470,394,470,393,470,393,470,393,470,394,470,393,470,397,470,1262,469,1263,470,1263,470,394,470,393,470,395,469,394,469,394,469,394,469,395,470,394,469,397,470,1263,469,1264,469,1264,469,394,470,394,469,395,469,394,469,394,469,394,470,395,469,394,469,394,469,395,468,396,469,395,468,395,468,395,468,396,468,422,445,1263,469,1264,469,396,468,396,468,419,444,419,444,395,468,1288,445,419,444,395,468,420,444,422,445,1289,444,423,444,1289,443,1289,444,422,445,1289,444,420,445,419,444,419,444,419,444,420,445,419,444,419,444,419,444,419,444,1289,444,423,444,1288,444,1290,444,421,444] flipper = PyFlipper(com="/dev/serial/by-id/usb-Flipper_Devices_Inc._Flipper_Helper_flip_Helper-if00") +def reloadWaybar(): + system("pkill -RTMIN+6 waybar") + +# stolen from xdg BaseDirectory.save_cache_path +def cachePath(*resource): + import os + resource = os.path.join(*resource) + assert not resource.startswith('/') + path = os.path.join(xdg_cache_home, resource) + if not os.path.isdir(path): + os.close(os.open(path, os.O_CREAT | os.O_WRONLY)) + return path + + +def getStatusFd(): + statusPath = cachePath("flipper-ac-status") + try: + return open(statusPath, "r+") + except FileNotFoundError: + return open(statusPath, "r+") + # we don't care about fd leak, system will cleanup at the end + +def writeStatus(status: status_t): + fd = getStatusFd() + fd.seek(0) + fd.write(str(status.name)) + fd.truncate() + fd.close() + reloadWaybar() + def main(): - eval(argv[1])() + r = eval(argv[1])() + print(r) + return not bool(r) # !r + +def on(): + writeStatus(status_t.TURNING_ON) + with suppress(Exception): + flipper.ir.tx_raw(frequency=38000, duty_cycle=0.33, samples=ac_open) + writeStatus(status_t.ON) + return True + +def off(): + writeStatus(status_t.TURNING_OFF) + with suppress(Exception): + flipper.ir.tx_raw(frequency=38000, duty_cycle=0.33, samples=ac_close) + writeStatus(status_t.OFF) + return False -def open(): - flipper.ir.tx_raw(frequency=38000, duty_cycle=0.33, samples=ac_open) +def statusBool() -> status_t: + return status_t[getStatusFd().read().strip()] -def close(): - flipper.ir.tx_raw(frequency=38000, duty_cycle=0.33, samples=ac_close) +def status() -> str: + return statusBool().value +def toggle(): + if statusBool() == status_t.ON or statusBool() == status_t.TURNING_ON: + off() + else: # status() == status_t.OFF or status() == status_t.TURNING_OFF: + on() + reloadWaybar() if __name__ == "__main__": - main() + exit(main()) -- cgit v1.2.3