summaryrefslogtreecommitdiff
path: root/flipper-ac.py
blob: 19c0328d003c07dc1781751cf764959902a8ca87 (plain)
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
#!/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_max = [3550,1651,506,387,479,1226,507,384,480,382,481,383,479,384,478,386,478,385,478,385,478,386,477,387,477,387,475,391,475,1257,475,392,472,392,470,393,470,394,469,395,470,394,469,398,469,1263,469,1264,468,1264,469,395,468,398,469,1265,469,394,469,395,468,395,468,396,468,395,468,395,468,395,468,396,468,395,468,395,468,395,468,396,468,395,468,395,468,395,468,396,468,395,468,395,468,395,468,396,469,395,468,395,468,396,467,397,467,396,467,396,467,396,467,397,467,419,444,400,467,1265,467,1267,467,419,444,420,443,396,468,420,444,399,468,9969,3489,1717,443,423,444,1289,444,421,443,420,443,420,443,420,443,421,443,420,443,420,444,420,443,421,444,420,443,423,444,1290,443,421,443,420,444,420,443,420,443,421,444,420,443,424,443,1289,443,1290,443,1290,443,421,442,424,443,1291,443,421,443,420,443,420,443,422,442,421,442,421,442,421,442,422,442,421,442,421,442,421,442,422,443,424,443,1291,442,421,442,421,443,1289,443,1290,442,1291,442,422,442,421,442,421,442,421,442,422,443,421,442,425,442,1291,442,423,442,421,442,421,442,421,442,422,442,422,442,421,442,421,442,422,441,1290,442,1291,442,422,441,423,442,425,441,1290,442,1290,442,1292,442,422,441,422,441,422,441,423,441,422,441,422,441,422,442,424,440,423,440,423,440,423,440,424,440,423,440,423,440,423,440,424,440,424,439,427,440,1292,440,1293,440,1294,439,424,439,425,438,427,438,425,438,450,413,450,413,451,413,451,412,454,413,1319,413,1320,413,1320,413,451,412,451,412,453,412,452,411,452,411,452,412,454,410,479,384,479,384,479,384,480,384,480,383,480,383,480,383,482,382,485,357,1374,383,1350,382,483,357,533,330,534,329,534,329,535,328,1404,329,535,328,561,302,564,300,592,274,1432,301,593,273,1433,301,1459,273,622,216,1489,245]
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]

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():
    r = eval(argv[1])()
    print(r)
    return not bool(r) # !r

def on():
    writeStatus(status_t.TURNING_ON)
    # with suppress(Exception):
    PyFlipper(com="/dev/serial/by-id/usb-Flipper_Devices_Inc._Helper_flip_Helper-if00").ir.tx_raw(frequency=38000, duty_cycle=0.33, samples=ac_open)
    writeStatus(status_t.ON)
    return True

def max():
    writeStatus(status_t.TURNING_ON)
    # with suppress(Exception):
    PyFlipper(com="/dev/serial/by-id/usb-Flipper_Devices_Inc._Helper_flip_Helper-if00").ir.tx_raw(frequency=38000, duty_cycle=0.33, samples=ac_max)
    writeStatus(status_t.ON)
    return True

def off():
    writeStatus(status_t.TURNING_OFF)
    # with suppress(Exception):
    PyFlipper(com="/dev/serial/by-id/usb-Flipper_Devices_Inc._Helper_flip_Helper-if00").ir.tx_raw(frequency=38000, duty_cycle=0.33, samples=ac_close)
    writeStatus(status_t.OFF)
    return False

def statusRaw() -> status_t:
    return status_t[getStatusFd().read().strip()]

def statusBool() -> bool:
    return statusRaw() == status_t.ON or statusRaw() == status_t.TURNING_ON

def status() -> str:
    return statusRaw().value

def toggle():
    if statusRaw() == status_t.ON or statusRaw() == status_t.TURNING_ON:
        off()
    else: # status() == status_t.OFF or status() == status_t.TURNING_OFF:
        on()
    reloadWaybar()

if __name__ == "__main__":
    exit(main())