Compare commits
30 Commits
4c7646b2fe
...
fbebe9f4f9
| Author | SHA1 | Date | |
|---|---|---|---|
| fbebe9f4f9 | |||
| a79c6f4dd3 | |||
|
|
2fcaf2f064 | ||
|
|
3b38264b70 | ||
| 3ee89ce3b4 | |||
| 74b4b495f9 | |||
| 4575ef16ad | |||
| a342187635 | |||
| 428ed8b884 | |||
| a22702df4d | |||
| 5a8866add7 | |||
| a2cd2f8dc2 | |||
| c47725e31a | |||
| 22b1a8a6d6 | |||
| 45a38c05b7 | |||
| 87bd0338bd | |||
| 0a33f399e1 | |||
|
|
ded6e3d360 | ||
|
|
a64457a0d5 | ||
|
|
fea4e69140 | ||
|
|
aaaf660e9d | ||
|
|
cef9e00819 | ||
|
|
7e3aca491c | ||
|
|
7bfdcd9bee | ||
|
|
dc19877132 | ||
| fb53f900fb | |||
| 044dd815dc | |||
| f3bcc89320 | |||
| 4b74f3ef02 | |||
| 8403f36a1f |
1
Pipfile
1
Pipfile
@@ -11,6 +11,7 @@ watchfiles = "*"
|
|||||||
fastapi = "*"
|
fastapi = "*"
|
||||||
uvicorn = "*"
|
uvicorn = "*"
|
||||||
flask = "*"
|
flask = "*"
|
||||||
|
serial = "*"
|
||||||
|
|
||||||
[dev-packages]
|
[dev-packages]
|
||||||
|
|
||||||
|
|||||||
215
Pipfile.lock
generated
215
Pipfile.lock
generated
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"_meta": {
|
"_meta": {
|
||||||
"hash": {
|
"hash": {
|
||||||
"sha256": "921fc0268aaeb27ac977902942dd25f0f84ea35bcbbee0412a4d7c801652eb67"
|
"sha256": "5d970f8c0ea9e8ffa98cf0ea5f791161589a97d953d2629da026d01fa7a8bce7"
|
||||||
},
|
},
|
||||||
"pipfile-spec": 6,
|
"pipfile-spec": 6,
|
||||||
"requires": {
|
"requires": {
|
||||||
@@ -151,11 +151,11 @@
|
|||||||
},
|
},
|
||||||
"bitstring": {
|
"bitstring": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:69d1587f0ac18dc7d93fc7e80d5f447161a33e57027e726dc18a0a8bacf1711a",
|
"sha256:e682ac522bb63e041d16cbc9d0ca86a4f00194db16d0847c7efe066f836b2e37",
|
||||||
"sha256:a08bc09d3857216d4c0f412a1611056f1cc2b64fd254fb1e8a0afba7cfa1a95a"
|
"sha256:feac49524fcf3ef27e6081e86f02b10d2adf6c3773bf22fbe0e7eea9534bc737"
|
||||||
],
|
],
|
||||||
"markers": "python_version >= '3.8'",
|
"markers": "python_version >= '3.8'",
|
||||||
"version": "==4.3.1"
|
"version": "==4.4.0"
|
||||||
},
|
},
|
||||||
"blinker": {
|
"blinker": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
@@ -265,84 +265,89 @@
|
|||||||
},
|
},
|
||||||
"cryptography": {
|
"cryptography": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:01df4f50f314fbe7009f54046e908d1754f19d0c6d3070df1e6268c5a4af09fa",
|
"sha256:02f547fce831f5096c9a567fd41bc12ca8f11df260959ecc7c3202555cc47a72",
|
||||||
"sha256:0563655cb3c6d05fb2afe693340bc050c30f9f34e15763361cf08e94749401fc",
|
"sha256:039917b0dc418bb9f6edce8a906572d69e74bd330b0b3fea4f79dab7f8ddd235",
|
||||||
"sha256:078e5f06bd2fa5aea5a324f2a09f914b1484f1d0c2a4d6a8a28c74e72f65f2da",
|
"sha256:1abfdb89b41c3be0365328a410baa9df3ff8a9110fb75e7b52e66803ddabc9a9",
|
||||||
"sha256:0a9ad24359fee86f131836a9ac3bffc9329e956624a2d379b613f8f8abaf5255",
|
"sha256:2ae6971afd6246710480e3f15824ed3029a60fc16991db250034efd0b9fb4356",
|
||||||
"sha256:2067461c80271f422ee7bdbe79b9b4be54a5162e90345f86a23445a0cf3fd8a2",
|
"sha256:2b7a67c9cd56372f3249b39699f2ad479f6991e62ea15800973b956f4b73e257",
|
||||||
"sha256:281526e865ed4166009e235afadf3a4c4cba6056f99336a99efba65336fd5485",
|
"sha256:351695ada9ea9618b3500b490ad54c739860883df6c1f555e088eaf25b1bbaad",
|
||||||
"sha256:2d08bc22efd73e8854b0b7caff402d735b354862f1145d7be3b9c0f740fef6a0",
|
"sha256:38946c54b16c885c72c4f59846be9743d699eee2b69b6988e0a00a01f46a61a4",
|
||||||
"sha256:3c268a3490df22270955966ba236d6bc4a8f9b6e4ffddb78aac535f1a5ea471d",
|
"sha256:3b4995dc971c9fb83c25aa44cf45f02ba86f71ee600d81091c2f0cbae116b06c",
|
||||||
"sha256:3d425eacbc9aceafd2cb429e42f4e5d5633c6f873f5e567077043ef1b9bbf616",
|
"sha256:3ce58ba46e1bc2aac4f7d9290223cead56743fa6ab94a5d53292ffaac6a91614",
|
||||||
"sha256:44cc0675b27cadb71bdbb96099cca1fa051cd11d2ade09e5cd3a2edb929ed947",
|
"sha256:3ee190460e2fbe447175cda91b88b84ae8322a104fc27766ad09428754a618ed",
|
||||||
"sha256:47bcd19517e6389132f76e2d5303ded6cf3f78903da2158a671be8de024f4cd0",
|
"sha256:4108d4c09fbbf2789d0c926eb4152ae1760d5a2d97612b92d508d96c861e4d31",
|
||||||
"sha256:485e2b65d25ec0d901bca7bcae0f53b00133bf3173916d8e421f6fddde103908",
|
"sha256:420d0e909050490d04359e7fdb5ed7e667ca5c3c402b809ae2563d7e66a92229",
|
||||||
"sha256:5aa3e463596b0087b3da0dbe2b2487e9fc261d25da85754e30e3b40637d61f81",
|
"sha256:47fb8a66058b80e509c47118ef8a75d14c455e81ac369050f20ba0d23e77fee0",
|
||||||
"sha256:5f14fba5bf6f4390d7ff8f086c566454bff0411f6d8aa7af79c88b6f9267aecc",
|
"sha256:4c3341037c136030cb46e4b1e17b7418ea4cbd9dd207e4a6f3b2b24e0d4ac731",
|
||||||
"sha256:62217ba44bf81b30abaeda1488686a04a702a261e26f87db51ff61d9d3510abd",
|
"sha256:4d7e3d356b8cd4ea5aff04f129d5f66ebdc7b6f8eae802b93739ed520c47c79b",
|
||||||
"sha256:6225d3ebe26a55dbc8ead5ad1265c0403552a63336499564675b29eb3184c09b",
|
"sha256:4d8ae8659ab18c65ced284993c2265910f6c9e650189d4e3f68445ef82a810e4",
|
||||||
"sha256:6bb5157bf6a350e5b28aee23beb2d84ae6f5be390b2f8ee7ea179cda077e1019",
|
"sha256:4e817a8920bfbcff8940ecfd60f23d01836408242b30f1a708d93198393a80b4",
|
||||||
"sha256:728fedc529efc1439eb6107b677f7f7558adab4553ef8669f0d02d42d7b959a7",
|
"sha256:50bfb6925eff619c9c023b967d5b77a54e04256c4281b0e21336a130cd7fc263",
|
||||||
"sha256:766330cce7416c92b5e90c3bb71b1b79521760cdcfc3a6a1a182d4c9fab23d2b",
|
"sha256:556e106ee01aa13484ce9b0239bca667be5004efb0aabbed28d353df86445595",
|
||||||
"sha256:812815182f6a0c1d49a37893a303b44eaac827d7f0d582cecfc81b6427f22973",
|
"sha256:582f5fcd2afa31622f317f80426a027f30dc792e9c80ffee87b993200ea115f1",
|
||||||
"sha256:829c2b12bbc5428ab02d6b7f7e9bbfd53e33efd6672d21341f2177470171ad8b",
|
"sha256:5be7bf2fb40769e05739dd0046e7b26f9d4670badc7b032d6ce4db64dddc0678",
|
||||||
"sha256:82a62483daf20b8134f6e92898da70d04d0ef9a75829d732ea1018678185f4f5",
|
"sha256:60ee7e19e95104d4c03871d7d7dfb3d22ef8a9b9c6778c94e1c8fcc8365afd48",
|
||||||
"sha256:8a15fb869670efa8f83cbffbc8753c1abf236883225aed74cd179b720ac9ec80",
|
"sha256:61aa400dce22cb001a98014f647dc21cda08f7915ceb95df0c9eaf84b4b6af76",
|
||||||
"sha256:8bf75b0259e87fa70bddc0b8b4078b76e7fd512fd9afae6c1193bcf440a4dbef",
|
"sha256:68f68d13f2e1cb95163fa3b4db4bf9a159a418f5f6e7242564fc75fcae667fd0",
|
||||||
"sha256:91627ebf691d1ea3976a031b61fb7bac1ccd745afa03602275dda443e11c8de0",
|
"sha256:7d1f30a86d2757199cb2d56e48cce14deddf1f9c95f1ef1b64ee91ea43fe2e18",
|
||||||
"sha256:93d8291da8d71024379ab2cb0b5c57915300155ad42e07f76bea6ad838d7e59b",
|
"sha256:7d731d4b107030987fd61a7f8ab512b25b53cef8f233a97379ede116f30eb67d",
|
||||||
"sha256:9b34d8ba84454641a6bf4d6762d15847ecbd85c1316c0a7984e6e4e9f748ec2e",
|
"sha256:803812e111e75d1aa73690d2facc295eaefd4439be1023fefc4995eaea2af90d",
|
||||||
"sha256:9b4d17bc7bd7cdd98e3af40b441feaea4c68225e2eb2341026c84511ad246c0c",
|
"sha256:80a8d7bfdf38f87ca30a5391c0c9ce4ed2926918e017c29ddf643d0ed2778ea1",
|
||||||
"sha256:9c2da296c8d3415b93e6053f5a728649a87a48ce084a9aaf51d6e46c87c7f2d2",
|
"sha256:8293f3dea7fc929ef7240796ba231413afa7b68ce38fd21da2995549f5961981",
|
||||||
"sha256:a05177ff6296644ef2876fce50518dffb5bcdf903c85250974fc8bc85d54c0af",
|
"sha256:8456928655f856c6e1533ff59d5be76578a7157224dbd9ce6872f25055ab9ab7",
|
||||||
"sha256:a90e43e3ef65e6dcf969dfe3bb40cbf5aef0d523dff95bfa24256be172a845f4",
|
"sha256:890bcb4abd5a2d3f852196437129eb3667d62630333aacc13dfd470fad3aaa82",
|
||||||
"sha256:a9556ba711f7c23f77b151d5798f3ac44a13455cc68db7697a1096e6d0563cab",
|
"sha256:94a76daa32eb78d61339aff7952ea819b1734b46f73646a07decb40e5b3448e2",
|
||||||
"sha256:b1de0ebf7587f28f9190b9cb526e901bf448c9e6a99655d2b07fff60e8212a82",
|
"sha256:9f16fbdf4da055efb21c22d81b89f155f02ba420558db21288b3d0035bafd5f4",
|
||||||
"sha256:be8c01a7d5a55f9a47d1888162b76c8f49d62b234d88f0ff91a9fbebe32ffbc3",
|
"sha256:a3d1fae9863299076f05cb8a778c467578262fae09f9dc0ee9b12eb4268ce663",
|
||||||
"sha256:bfd019f60f8abc2ed1b9be4ddc21cfef059c841d86d710bb69909a688cbb8f59",
|
"sha256:a3d507bb6a513ca96ba84443226af944b0f7f47dcc9a399d110cd6146481d24c",
|
||||||
"sha256:c236a44acfb610e70f6b3e1c3ca20ff24459659231ef2f8c48e879e2d32b73da",
|
"sha256:abace499247268e3757271b2f1e244b36b06f8515cf27c4d49468fc9eb16e93d",
|
||||||
"sha256:c411f16275b0dea722d76544a61d6421e2cc829ad76eec79280dbdc9ddf50061",
|
"sha256:ba2a27ff02f48193fc4daeadf8ad2590516fa3d0adeeb34336b96f7fa64c1e3a",
|
||||||
"sha256:c92010b58a51196a5f41c3795190203ac52edfd5dc3ff99149b4659eba9d2085",
|
"sha256:bc84e875994c3b445871ea7181d424588171efec3e185dced958dad9e001950a",
|
||||||
"sha256:d5a45ddc256f492ce42a4e35879c5e5528c09cd9ad12420828c972951d8e016b",
|
"sha256:bfd56bb4b37ed4f330b82402f6f435845a5f5648edf1ad497da51a8452d5d62d",
|
||||||
"sha256:daa392191f626d50f1b136c9b4cf08af69ca8279d110ea24f5c2700054d2e263",
|
"sha256:c18ff11e86df2e28854939acde2d003f7984f721eba450b56a200ad90eeb0e6b",
|
||||||
"sha256:dc1272e25ef673efe72f2096e92ae39dea1a1a450dd44918b15351f72c5a168e",
|
"sha256:c3bcce8521d785d510b2aad26ae2c966092b7daa8f45dd8f44734a104dc0bc1a",
|
||||||
"sha256:dce1e4f068f03008da7fa51cc7abc6ddc5e5de3e3d1550334eaf8393982a5829",
|
"sha256:c4143987a42a2397f2fc3b4d7e3a7d313fbe684f67ff443999e803dd75a76826",
|
||||||
"sha256:dd5aba870a2c40f87a3af043e0dee7d9eb02d4aff88a797b48f2b43eff8c3ab4",
|
"sha256:c69fd885df7d089548a42d5ec05be26050ebcd2283d89b3d30676eb32ff87dee",
|
||||||
"sha256:de0f5f4ec8711ebc555f54735d4c673fc34b65c44283895f1a08c2b49d2fd99c",
|
"sha256:ced80795227d70549a411a4ab66e8ce307899fad2220ce5ab2f296e687eacde9",
|
||||||
"sha256:df4a817fa7138dd0c96c8c8c20f04b8aaa1fac3bbf610913dcad8ea82e1bfd3f",
|
"sha256:d66e421495fdb797610a08f43b05269e0a5ea7f5e652a89bfd5a7d3c1dee3648",
|
||||||
"sha256:e07ea39c5b048e085f15923511d8121e4a9dc45cee4e3b970ca4f0d338f23095",
|
"sha256:d861ee9e76ace6cf36a6a89b959ec08e7bc2493ee39d07ffe5acb23ef46d27da",
|
||||||
"sha256:eeeb2e33d8dbcccc34d64651f00a98cb41b2dc69cef866771a5717e6734dfa32",
|
"sha256:e9251e3be159d1020c4030bd2e5f84d6a43fe54b6c19c12f51cde9542a2817b2",
|
||||||
"sha256:fa0900b9ef9c49728887d1576fd8d9e7e3ea872fa9b25ef9b64888adc434e976",
|
"sha256:f145bba11b878005c496e93e257c1e88f154d278d2638e6450d17e0f31e558d2",
|
||||||
"sha256:fdc3daab53b212472f1524d070735b2f0c214239df131903bae1d598016fa822"
|
"sha256:fe346b143ff9685e40192a4960938545c699054ba11d4f9029f94751e3f71d87"
|
||||||
],
|
],
|
||||||
"markers": "python_version >= '3.8' and python_full_version not in '3.9.0, 3.9.1'",
|
"markers": "python_version >= '3.8' and python_full_version not in '3.9.0, 3.9.1'",
|
||||||
"version": "==46.0.4"
|
"version": "==46.0.5"
|
||||||
},
|
},
|
||||||
"esptool": {
|
"esptool": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:2ea9bcd7eb263d380a4fe0170856a10e4c65e3f38c757ebdc73584c8dd8322da"
|
"sha256:9c355b7d6331cc92979cc710ae5c41f59830d1ea29ec24c467c6005a092c06d6"
|
||||||
],
|
],
|
||||||
"index": "pypi",
|
"index": "pypi",
|
||||||
"markers": "python_version >= '3.10'",
|
"version": "==5.2.0"
|
||||||
"version": "==5.1.0"
|
|
||||||
},
|
},
|
||||||
"fastapi": {
|
"fastapi": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:c8cdf7c2182c9a06bf9cfa3329819913c189dc86389b90d5709892053582db29",
|
"sha256:46e2fc5745924b7c840f71ddd277382af29ce1cdb7d5eab5bf697e3fb9999c9e",
|
||||||
"sha256:ed99383fd96063447597d5aa2a9ec3973be198e3b4fc10c55f15c62efdb21c60"
|
"sha256:d04115b508d936d254cea545b7312ecaa58a7b3a0f84952535b4c9afae7668cd"
|
||||||
],
|
],
|
||||||
"index": "pypi",
|
"index": "pypi",
|
||||||
"markers": "python_version >= '3.9'",
|
"version": "==0.135.1"
|
||||||
"version": "==0.128.3"
|
|
||||||
},
|
},
|
||||||
"flask": {
|
"flask": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:bf656c15c80190ed628ad08cdfd3aaa35beb087855e2f494910aa3774cc4fd87",
|
"sha256:0ef0e52b8a9cd932855379197dd8f94047b359ca0a78695144304cb45f87c9eb",
|
||||||
"sha256:ca1d8112ec8a6158cc29ea4858963350011b5c846a414cdb7a954aa9e967d03c"
|
"sha256:f4bcbefc124291925f1a26446da31a5178f9483862233b23c0c96a20701f670c"
|
||||||
],
|
],
|
||||||
"index": "pypi",
|
"index": "pypi",
|
||||||
"markers": "python_version >= '3.9'",
|
"version": "==3.1.3"
|
||||||
"version": "==3.1.2"
|
},
|
||||||
|
"future": {
|
||||||
|
"hashes": [
|
||||||
|
"sha256:929292d34f5872e70396626ef385ec22355a1fae8ad29e1a734c3e43f9fbc216",
|
||||||
|
"sha256:bd2968309307861edae1458a4f8a4f3598c03be43b97521076aebf5d94c07b05"
|
||||||
|
],
|
||||||
|
"markers": "python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2, 3.3'",
|
||||||
|
"version": "==1.0.0"
|
||||||
},
|
},
|
||||||
"h11": {
|
"h11": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
@@ -367,6 +372,14 @@
|
|||||||
],
|
],
|
||||||
"version": "==2.3.0"
|
"version": "==2.3.0"
|
||||||
},
|
},
|
||||||
|
"iso8601": {
|
||||||
|
"hashes": [
|
||||||
|
"sha256:6b1d3829ee8921c4301998c909f7829fa9ed3cbdac0d3b16af2d743aed1ba8df",
|
||||||
|
"sha256:aac4145c4dcb66ad8b648a02830f5e2ff6c24af20f4f482689be402db2429242"
|
||||||
|
],
|
||||||
|
"markers": "python_version >= '3.7' and python_version < '4.0'",
|
||||||
|
"version": "==2.1.0"
|
||||||
|
},
|
||||||
"itsdangerous": {
|
"itsdangerous": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:c6242fc49e35958c8b15141343aa660db5fc54d4f13a1db01a3f5891b98700ef",
|
"sha256:c6242fc49e35958c8b15141343aa660db5fc54d4f13a1db01a3f5891b98700ef",
|
||||||
@@ -500,16 +513,15 @@
|
|||||||
"sha256:6bb75774648091dad6833af4f86c5bf6505f8d7aec211380f9e6996c01d23cb5"
|
"sha256:6bb75774648091dad6833af4f86c5bf6505f8d7aec211380f9e6996c01d23cb5"
|
||||||
],
|
],
|
||||||
"index": "pypi",
|
"index": "pypi",
|
||||||
"markers": "python_version >= '3.4'",
|
|
||||||
"version": "==1.27.0"
|
"version": "==1.27.0"
|
||||||
},
|
},
|
||||||
"platformdirs": {
|
"platformdirs": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:61d5cdcc6065745cdd94f0f878977f8de9437be93de97c1c12f853c9c0cdcbda",
|
"sha256:1ec356301b7dc906d83f371c8f487070e99d3ccf9e501686456394622a01a934",
|
||||||
"sha256:d03afa3963c806a9bed9d5125c8f4cb2fdaf74a55ab60e5d59b3fde758104d31"
|
"sha256:68a9a4619a666ea6439f2ff250c12a853cd1cbd5158d258bd824a7df6be2f868"
|
||||||
],
|
],
|
||||||
"markers": "python_version >= '3.10'",
|
"markers": "python_version >= '3.10'",
|
||||||
"version": "==4.5.1"
|
"version": "==4.9.4"
|
||||||
},
|
},
|
||||||
"pycparser": {
|
"pycparser": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
@@ -758,11 +770,11 @@
|
|||||||
},
|
},
|
||||||
"rich": {
|
"rich": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:08e67c3e90884651da3239ea668222d19bea7b589149d8014a21c633420dbb69",
|
"sha256:793431c1f8619afa7d3b52b2cdec859562b950ea0d4b6b505397612db8d5362d",
|
||||||
"sha256:e712f11c1a562a11843306f5ed999475f09ac31ffb64281f73ab29ffdda8b3b8"
|
"sha256:b8daa0b9e4eef54dd8cf7c86c03713f53241884e814f4e2f5fb342fe520f639b"
|
||||||
],
|
],
|
||||||
"markers": "python_full_version >= '3.8.0'",
|
"markers": "python_full_version >= '3.8.0'",
|
||||||
"version": "==14.3.2"
|
"version": "==14.3.3"
|
||||||
},
|
},
|
||||||
"rich-click": {
|
"rich-click": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
@@ -772,6 +784,14 @@
|
|||||||
"markers": "python_version >= '3.8'",
|
"markers": "python_version >= '3.8'",
|
||||||
"version": "==1.9.7"
|
"version": "==1.9.7"
|
||||||
},
|
},
|
||||||
|
"serial": {
|
||||||
|
"hashes": [
|
||||||
|
"sha256:542150a127ddbf5ed2acc3a6ac4ce807cbcdae3b197acf785bbda6565c94f848",
|
||||||
|
"sha256:e887f06e07e190e39174b694eee6724e3c48bd361be1d97964caef5d5b61c73b"
|
||||||
|
],
|
||||||
|
"index": "pypi",
|
||||||
|
"version": "==0.0.97"
|
||||||
|
},
|
||||||
"starlette": {
|
"starlette": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:0029d43eb3d273bc4f83a08720b4912ea4b071087a3b48db01b7c839f7954d74",
|
"sha256:0029d43eb3d273bc4f83a08720b4912ea4b071087a3b48db01b7c839f7954d74",
|
||||||
@@ -780,6 +800,43 @@
|
|||||||
"markers": "python_version >= '3.10'",
|
"markers": "python_version >= '3.10'",
|
||||||
"version": "==0.52.1"
|
"version": "==0.52.1"
|
||||||
},
|
},
|
||||||
|
"tibs": {
|
||||||
|
"hashes": [
|
||||||
|
"sha256:01ea5258bdf942d21560dc07d532082cd04f07cfef65fedd58ae84f7d0d2562a",
|
||||||
|
"sha256:0a7ce857ef05c59dc61abadc31c4b9b1e3c62f9e5fb29217988c308936aea71e",
|
||||||
|
"sha256:130bc68ff500fc8185677df7a97350b5d5339e6ba7e325bc3031337f6424ede7",
|
||||||
|
"sha256:173dfbecb2309edd9771f453580c88cf251e775613461566b23dbd756b3d54cb",
|
||||||
|
"sha256:1906729038b85c3b4c040aa28a456d85bc976d0c5007177350eb73374ffa0fd0",
|
||||||
|
"sha256:1b56583db148e5094d781c3d746815dbcbb6378c6f813c8ce291efd4ab21da8b",
|
||||||
|
"sha256:1d5521cc6768bfa6282a0c591ba06b079ab91b5c7d5696925ad2abac59779a54",
|
||||||
|
"sha256:1f95d5db62960205a1e9eba73ce67dc14e7366ae080cd4e5b6f005ebd90faf02",
|
||||||
|
"sha256:29480bf03e3372a5f9cc59ea0541f76f8efd696d4f0d214715e94247c342a037",
|
||||||
|
"sha256:2a618de62004d9217d2d2ab0f7f9bbdd098c12642dc01f07b3fb00f0b5f3131a",
|
||||||
|
"sha256:42725200f1b02687ed6e6a1c01e0ec150dc829d21d901ffc74cc0ac4d821f57f",
|
||||||
|
"sha256:477608f9b87e24a22ab6d50b81da04a5cb59bfa49598ff7ec5165035a18fb392",
|
||||||
|
"sha256:4b7510235379368b7523f624d46e0680f3706e3a3965877a6583cdcb598b8bac",
|
||||||
|
"sha256:501728d096e10d9a165aa526743d47418a6bbfd7b084fa47ecb22be7641d3edb",
|
||||||
|
"sha256:63255749f937c5e6fedcc7d54e7bd359aef711017e6855f373b0510a14ee2215",
|
||||||
|
"sha256:6a9feed5931b881809a950eca0e01e757113e2383a2af06a3e6982f110c869e2",
|
||||||
|
"sha256:76746f01b3db9dbd802f5e615f11f68df7a29ecef521b082dca53f3fa7d0084f",
|
||||||
|
"sha256:77103a9f1af72ac4cf5006828d0fb21578d19ce55fd990e9a1c8e46fd549561f",
|
||||||
|
"sha256:7d6592ed93c6748acd39df484c1ee24d40ee247c2a20ca38ba03363506fd24f3",
|
||||||
|
"sha256:847709c108800ad6a45efaf9a040628278956938a4897f7427a2587013dc3b98",
|
||||||
|
"sha256:859f05315ffb307d3474c505d694f3a547f00730a024c982f5f60316a5505b3c",
|
||||||
|
"sha256:a61d36155f8ab8642e1b6744e13822f72050fc7ec4f86ec6965295afa04949e2",
|
||||||
|
"sha256:a883ca13a922a66b2c1326a9c188123a574741a72510a4bf52fd6f97db191e44",
|
||||||
|
"sha256:ac0aa2aae38f7325c91c261ce1d18f769c4c7033c98d6ea3ea5534585cf16452",
|
||||||
|
"sha256:ace018a057459e3dccd06a4aae1c5c8cd57e352b263dcef534ae39bf3e03b5cf",
|
||||||
|
"sha256:ad61df93b50f875b277ab736c5d37b6bce56f9abce489a22f4e02d9daa2966e3",
|
||||||
|
"sha256:b9535dc7b7484904a58b51bd8e64da7efbf1d8466ff7e84ed1d78f4ddc561c99",
|
||||||
|
"sha256:d4f3ff613d486650816bc5516760c0382a2cc0ca8aeddd8914d011bc3b81d9a2",
|
||||||
|
"sha256:e13b9c7ff2604b0146772025e1ac6f85c8c625bf6ac73736ff671eaf357dda41",
|
||||||
|
"sha256:f5eea45851c960628a2bd29847765d55e19a687c5374456ad2c8cf6410eb1efa",
|
||||||
|
"sha256:f70bd250769381c73110d6f24feaf8b6fcd44f680b3cb28a20ea06db3d04fb6f"
|
||||||
|
],
|
||||||
|
"markers": "python_version >= '3.8'",
|
||||||
|
"version": "==0.5.7"
|
||||||
|
},
|
||||||
"typing-extensions": {
|
"typing-extensions": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:0cea48d173cc12fa28ecabc3b837ea3cf6f38c6d1136f85cbaaf598984861466",
|
"sha256:0cea48d173cc12fa28ecabc3b837ea3cf6f38c6d1136f85cbaaf598984861466",
|
||||||
@@ -798,12 +855,11 @@
|
|||||||
},
|
},
|
||||||
"uvicorn": {
|
"uvicorn": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:839676675e87e73694518b5574fd0f24c9d97b46bea16df7b8c05ea1a51071ea",
|
"sha256:96c30f5c7abe6f74ae8900a70e92b85ad6613b745d4879eb9b16ccad15645359",
|
||||||
"sha256:c6c8f55bc8bf13eb6fa9ff87ad62308bbbc33d0b67f84293151efe87e0d5f2ee"
|
"sha256:9b1f190ce15a2dd22e7758651d9b6d12df09a13d51ba5bf4fc33c383a48e1775"
|
||||||
],
|
],
|
||||||
"index": "pypi",
|
"index": "pypi",
|
||||||
"markers": "python_version >= '3.10'",
|
"version": "==0.42.0"
|
||||||
"version": "==0.40.0"
|
|
||||||
},
|
},
|
||||||
"watchfiles": {
|
"watchfiles": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
@@ -918,16 +974,15 @@
|
|||||||
"sha256:f9a2ae5c91cecc9edd47e041a930490c31c3afb1f5e6d71de3dc671bfaca02bf"
|
"sha256:f9a2ae5c91cecc9edd47e041a930490c31c3afb1f5e6d71de3dc671bfaca02bf"
|
||||||
],
|
],
|
||||||
"index": "pypi",
|
"index": "pypi",
|
||||||
"markers": "python_version >= '3.9'",
|
|
||||||
"version": "==1.1.1"
|
"version": "==1.1.1"
|
||||||
},
|
},
|
||||||
"werkzeug": {
|
"werkzeug": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:5111e36e91086ece91f93268bb39b4a35c1e6f1feac762c9c822ded0a4e322dc",
|
"sha256:210c6bede5a420a913956b4791a7f4d6843a43b6fcee4dfa08a65e93007d0d25",
|
||||||
"sha256:6a548b0e88955dd07ccb25539d7d0cc97417ee9e179677d22c7041c8f078ce67"
|
"sha256:7ddf3357bb9564e407607f988f683d72038551200c704012bb9a4c523d42f131"
|
||||||
],
|
],
|
||||||
"markers": "python_version >= '3.9'",
|
"markers": "python_version >= '3.9'",
|
||||||
"version": "==3.1.5"
|
"version": "==3.1.6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"develop": {}
|
"develop": {}
|
||||||
|
|||||||
44
README.md
44
README.md
@@ -1,36 +1,52 @@
|
|||||||
# LED Driver - MicroPython
|
# LED Driver — MicroPython
|
||||||
|
|
||||||
MicroPython-based LED driver application for ESP32 microcontrollers.
|
MicroPython LED driver for ESP32: presets, patterns, **Wi-Fi** (TCP + UDP discovery) or **ESP-NOW** transport, optional HTTP polling, and dynamic pattern modules under `src/patterns/`.
|
||||||
|
|
||||||
## Prerequisites
|
## Prerequisites
|
||||||
|
|
||||||
- MicroPython firmware installed on ESP32
|
- MicroPython firmware on the ESP32
|
||||||
- USB cable for programming
|
- USB cable for programming
|
||||||
- Python 3 with pipenv
|
- Python 3 with pipenv (on the host, for `dev.py` / tests)
|
||||||
|
|
||||||
## Setup
|
## Setup
|
||||||
|
|
||||||
1. Install dependencies:
|
1. Install dependencies:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pipenv install
|
pipenv install
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Deploy to device:
|
2. Deploy to the device:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pipenv run dev
|
pipenv run dev
|
||||||
```
|
```
|
||||||
|
|
||||||
## Project Structure
|
## Project layout
|
||||||
|
|
||||||
```
|
```
|
||||||
led-driver/
|
led-driver/
|
||||||
├── src/
|
├── src/
|
||||||
│ ├── main.py # Main application code
|
│ ├── main.py # Entry: Wi-Fi/TCP or ESP-NOW path, process_data(), manifest OTA
|
||||||
│ ├── presets.py # LED pattern implementations (includes Preset and Presets classes)
|
│ ├── presets.py # Preset runtime + Presets class
|
||||||
│ ├── settings.py # Settings management
|
│ ├── preset.py # Single preset helpers
|
||||||
│ └── p2p.py # Peer-to-peer communication
|
│ ├── settings.py # settings.json
|
||||||
├── test/ # Pattern tests
|
│ ├── hello.py # UDP discovery (port 8766) / hello payloads
|
||||||
├── web_app.py # Web interface
|
│ ├── http_poll.py # Optional HTTP polling helper
|
||||||
├── dev.py # Development tools
|
│ ├── utils.py # Colour conversion / ordering
|
||||||
└── Pipfile # Python dependencies
|
│ ├── presets.json # Default preset file (on device)
|
||||||
|
│ └── patterns/ # Pattern modules (.py), loaded dynamically
|
||||||
|
├── tests/ # Host-side helpers (e.g. udp_client.py, test_mdns.py)
|
||||||
|
├── test/ # On-device style pattern tests (all.py, patterns/)
|
||||||
|
├── dev.py # Deploy / sync to serial device
|
||||||
|
├── docs/API.md # Wire format (long keys); Pi app docs short keys
|
||||||
|
├── msg.json # Sample message
|
||||||
|
├── Pipfile
|
||||||
|
└── LICENSE
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**Transport:** `settings.json` **`transport_type`** is typically **`wifi`** (TCP to the Pi on port **8765**, discovery on **8766**) or **`espnow`**. ESP-NOW code paths are loaded only when needed so a Wi-Fi-only image stays smaller.
|
||||||
|
|
||||||
|
## Further reading
|
||||||
|
|
||||||
|
- **`docs/API.md`** — JSON message fields as used in examples (`pattern`, `colors`, …). The Pi app may send **short keys** (`p`, `c`, …); behaviour matches once normalised on device.
|
||||||
|
|||||||
50
dev.py
50
dev.py
@@ -1,13 +1,24 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import serial
|
import serial
|
||||||
import sys
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
def mpremote_base():
|
||||||
|
"""mpremote on PATH, or same interpreter as this script (e.g. pipenv venv)."""
|
||||||
|
exe = shutil.which("mpremote")
|
||||||
|
if exe:
|
||||||
|
return [exe]
|
||||||
|
return [sys.executable, "-m", "mpremote"]
|
||||||
|
|
||||||
|
|
||||||
print(sys.argv)
|
print(sys.argv)
|
||||||
|
|
||||||
# Extract port (first arg if it's not a command)
|
# Extract port (first arg if it's not a command)
|
||||||
commands = ["src", "lib", "ls", "reset", "follow", "db"]
|
commands = ["src", "lib", "ls", "reset", "follow", "db", "test"]
|
||||||
port = None
|
port = None
|
||||||
if len(sys.argv) > 1 and sys.argv[1] not in commands:
|
if len(sys.argv) > 1 and sys.argv[1] not in commands:
|
||||||
port = sys.argv[1]
|
port = sys.argv[1]
|
||||||
@@ -18,17 +29,20 @@ for cmd in sys.argv[1:]:
|
|||||||
match cmd:
|
match cmd:
|
||||||
case "src":
|
case "src":
|
||||||
if port:
|
if port:
|
||||||
subprocess.call(["mpremote", "connect", port, "fs", "cp", "-r", ".", ":" ], cwd="src")
|
subprocess.call(
|
||||||
|
[*mpremote_base(), "connect", port, "fs", "cp", "-r", ".", ":"],
|
||||||
|
cwd="src",
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
print("Error: Port required for 'src' command")
|
print("Error: Port required for 'src' command")
|
||||||
case "lib":
|
case "lib":
|
||||||
if port:
|
if port:
|
||||||
subprocess.call(["mpremote", "connect", port, "fs", "cp", "-r", "lib", ":" ])
|
subprocess.call([*mpremote_base(), "connect", port, "fs", "cp", "-r", "lib", ":"])
|
||||||
else:
|
else:
|
||||||
print("Error: Port required for 'lib' command")
|
print("Error: Port required for 'lib' command")
|
||||||
case "ls":
|
case "ls":
|
||||||
if port:
|
if port:
|
||||||
subprocess.call(["mpremote", "connect", port, "fs", "ls", ":" ])
|
subprocess.call([*mpremote_base(), "connect", port, "fs", "ls", ":"])
|
||||||
else:
|
else:
|
||||||
print("Error: Port required for 'ls' command")
|
print("Error: Port required for 'ls' command")
|
||||||
case "reset":
|
case "reset":
|
||||||
@@ -48,6 +62,32 @@ for cmd in sys.argv[1:]:
|
|||||||
print("Error: Port required for 'follow' command")
|
print("Error: Port required for 'follow' command")
|
||||||
case "db":
|
case "db":
|
||||||
if port:
|
if port:
|
||||||
subprocess.call(["mpremote", "connect", port, "fs", "cp", "-r", "db", ":" ])
|
subprocess.call([*mpremote_base(), "connect", port, "fs", "cp", "-r", "db", ":"])
|
||||||
else:
|
else:
|
||||||
print("Error: Port required for 'db' command")
|
print("Error: Port required for 'db' command")
|
||||||
|
case "test":
|
||||||
|
if port:
|
||||||
|
if "all" in sys.argv[1:]:
|
||||||
|
test_files = sorted(
|
||||||
|
str(path)
|
||||||
|
for path in Path("test").rglob("*.py")
|
||||||
|
if path.is_file()
|
||||||
|
)
|
||||||
|
failed = []
|
||||||
|
for test_file in test_files:
|
||||||
|
print(f"Running {test_file}")
|
||||||
|
code = subprocess.call(
|
||||||
|
[*mpremote_base(), "connect", port, "run", test_file]
|
||||||
|
)
|
||||||
|
if code != 0:
|
||||||
|
failed.append((test_file, code))
|
||||||
|
if failed:
|
||||||
|
print("Some tests failed:")
|
||||||
|
for test_file, code in failed:
|
||||||
|
print(f" {test_file} (exit {code})")
|
||||||
|
else:
|
||||||
|
subprocess.call(
|
||||||
|
[*mpremote_base(), "connect", port, "run", "test/all.py"]
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
print("Error: Port required for 'test' command")
|
||||||
|
|||||||
34
docs/API.md
34
docs/API.md
@@ -1,10 +1,10 @@
|
|||||||
# LED Driver ESPNow API Documentation
|
# LED Driver API (message format)
|
||||||
|
|
||||||
This document describes the ESPNow message format for controlling LED driver devices.
|
This document describes the **JSON message format** for controlling LED driver devices. The same object is accepted from **ESP-NOW** (when that transport is enabled) and as **one JSON value per line** over **TCP** in **Wi-Fi** mode (see `src/main.py` on the device).
|
||||||
|
|
||||||
## Message Format
|
## Message Format
|
||||||
|
|
||||||
All messages are JSON objects sent via ESPNow with the following structure:
|
All messages are JSON objects with the following structure:
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
@@ -48,17 +48,17 @@ Presets define LED patterns with their configuration. Each preset has a name and
|
|||||||
|
|
||||||
- **`pattern`** (required): Pattern type. Options:
|
- **`pattern`** (required): Pattern type. Options:
|
||||||
- `"off"` - Turn off all LEDs
|
- `"off"` - Turn off all LEDs
|
||||||
- `"on"` - Solid color
|
- `"on"` - Solid colour
|
||||||
- `"blink"` - Blinking pattern
|
- `"blink"` - Blinking pattern
|
||||||
- `"rainbow"` - Rainbow color cycle
|
- `"rainbow"` - Rainbow colour cycle
|
||||||
- `"pulse"` - Pulse/fade pattern
|
- `"pulse"` - Pulse/fade pattern
|
||||||
- `"transition"` - Color transition
|
- `"transition"` - Colour transition
|
||||||
- `"chase"` - Chasing pattern
|
- `"chase"` - Chasing pattern
|
||||||
- `"circle"` - Circle loading pattern
|
- `"circle"` - Circle loading pattern
|
||||||
|
|
||||||
- **`colors`** (optional): Array of hex color strings (e.g., `"#FF0000"` for red). Default: `["#FFFFFF"]`
|
- **`colors`** (optional): Array of hex colour strings (e.g., `"#FF0000"` for red). Default: `["#FFFFFF"]`
|
||||||
- Colors are automatically converted from hex to RGB and reordered based on device color order setting
|
- Colours are automatically converted from hex to RGB and reordered based on device colour order setting
|
||||||
- Supports multiple colors for patterns that use them
|
- Supports multiple colours for patterns that use them
|
||||||
|
|
||||||
- **`delay`** (optional): Delay in milliseconds between pattern updates. Default: `100`
|
- **`delay`** (optional): Delay in milliseconds between pattern updates. Default: `100`
|
||||||
|
|
||||||
@@ -74,7 +74,7 @@ Presets define LED patterns with their configuration. Each preset has a name and
|
|||||||
### Pattern-Specific Parameters
|
### Pattern-Specific Parameters
|
||||||
|
|
||||||
#### Rainbow
|
#### Rainbow
|
||||||
- **`n1`**: Step increment (how many color wheel positions to advance per update). Default: `1`
|
- **`n1`**: Step increment (how many colour wheel positions to advance per update). Default: `1`
|
||||||
|
|
||||||
#### Pulse
|
#### Pulse
|
||||||
- **`n1`**: Attack time in milliseconds (fade in)
|
- **`n1`**: Attack time in milliseconds (fade in)
|
||||||
@@ -86,8 +86,8 @@ Presets define LED patterns with their configuration. Each preset has a name and
|
|||||||
- **`delay`**: Transition duration in milliseconds
|
- **`delay`**: Transition duration in milliseconds
|
||||||
|
|
||||||
#### Chase
|
#### Chase
|
||||||
- **`n1`**: Number of LEDs with first color
|
- **`n1`**: Number of LEDs with first colour
|
||||||
- **`n2`**: Number of LEDs with second color
|
- **`n2`**: Number of LEDs with second colour
|
||||||
- **`n3`**: Movement amount on even steps (can be negative)
|
- **`n3`**: Movement amount on even steps (can be negative)
|
||||||
- **`n4`**: Movement amount on odd steps (can be negative)
|
- **`n4`**: Movement amount on odd steps (can be negative)
|
||||||
|
|
||||||
@@ -235,7 +235,7 @@ All devices will start at step 10 and advance together on subsequent beats.
|
|||||||
|
|
||||||
1. **Version Check**: Messages with `v != "1"` are rejected
|
1. **Version Check**: Messages with `v != "1"` are rejected
|
||||||
2. **Preset Processing**: Presets are created or updated (upsert behavior)
|
2. **Preset Processing**: Presets are created or updated (upsert behavior)
|
||||||
3. **Color Conversion**: Hex colors are converted to RGB tuples and reordered based on device color order
|
3. **Colour Conversion**: Hex colours are converted to RGB tuples and reordered based on device colour order
|
||||||
4. **Selection**: Devices select their assigned preset, optionally with step value
|
4. **Selection**: Devices select their assigned preset, optionally with step value
|
||||||
|
|
||||||
## Best Practices
|
## Best Practices
|
||||||
@@ -244,20 +244,20 @@ All devices will start at step 10 and advance together on subsequent beats.
|
|||||||
2. **Use "off" for sync**: Select "off" pattern to synchronize devices before starting patterns
|
2. **Use "off" for sync**: Select "off" pattern to synchronize devices before starting patterns
|
||||||
3. **Beats for manual mode**: Send select messages repeatedly with same preset name to advance manual patterns
|
3. **Beats for manual mode**: Send select messages repeatedly with same preset name to advance manual patterns
|
||||||
4. **Step for precision**: Use step parameter when exact synchronization is required
|
4. **Step for precision**: Use step parameter when exact synchronization is required
|
||||||
5. **Color format**: Always use hex strings (`"#RRGGBB"`), conversion is automatic
|
5. **Colour format**: Always use hex strings (`"#RRGGBB"`), conversion is automatic
|
||||||
|
|
||||||
## Error Handling
|
## Error Handling
|
||||||
|
|
||||||
- Invalid version: Message is ignored
|
- Invalid version: Message is ignored
|
||||||
- Missing preset: Selection fails, device keeps current preset
|
- Missing preset: Selection fails, device keeps current preset
|
||||||
- Invalid pattern: Selection fails, device keeps current preset
|
- Invalid pattern: Selection fails, device keeps current preset
|
||||||
- Missing colors: Pattern uses default white color
|
- Missing colours: Pattern uses default white colour
|
||||||
- Invalid step: Step value is used as-is (may cause unexpected behavior)
|
- Invalid step: Step value is used as-is (may cause unexpected behavior)
|
||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
- Colors are automatically converted from hex strings to RGB tuples
|
- Colours are automatically converted from hex strings to RGB tuples
|
||||||
- Color order reordering happens automatically based on device settings
|
- Colour order reordering happens automatically based on device settings
|
||||||
- Step counter wraps around (0-255 for rainbow, unbounded for others)
|
- Step counter wraps around (0-255 for rainbow, unbounded for others)
|
||||||
- Manual mode patterns stop after one step/cycle, waiting for next beat
|
- Manual mode patterns stop after one step/cycle, waiting for next beat
|
||||||
- Auto mode patterns run continuously until changed
|
- Auto mode patterns run continuously until changed
|
||||||
|
|||||||
51
docs/pattern-contract.md
Normal file
51
docs/pattern-contract.md
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
# Pattern Contract (Important)
|
||||||
|
|
||||||
|
Pattern classes are loaded dynamically by `Presets._load_dynamic_patterns()`.
|
||||||
|
|
||||||
|
Patterns must follow this contract exactly.
|
||||||
|
|
||||||
|
## Required class shape
|
||||||
|
|
||||||
|
- File name is the pattern id (for example `blink.py` -> pattern name `blink`).
|
||||||
|
- Module exports a class with:
|
||||||
|
- `__init__(self, driver)` where `driver` is the `Presets` instance.
|
||||||
|
- `run(self, preset)` that returns a generator.
|
||||||
|
|
||||||
|
`Presets` binds patterns like this:
|
||||||
|
|
||||||
|
- `pattern_class(self).run`
|
||||||
|
- then calls `self.patterns[preset.p](preset)` and stores that generator.
|
||||||
|
- every frame, `Presets.tick()` does `next(self.generator)`.
|
||||||
|
|
||||||
|
## `run()` generator rules
|
||||||
|
|
||||||
|
- `run()` must `yield` frequently (normally once per tick loop).
|
||||||
|
- Do not block inside `run()`:
|
||||||
|
- no `sleep()` / `sleep_ms()` / long loops without `yield`.
|
||||||
|
- no network or file I/O.
|
||||||
|
- Use time checks (`utime.ticks_ms()` + `utime.ticks_diff(...)`) to schedule updates.
|
||||||
|
- Keep pattern state inside local variables in `run()` (or object fields if needed).
|
||||||
|
|
||||||
|
## Drawing and brightness
|
||||||
|
|
||||||
|
- Use `self.driver.apply_brightness(color, preset.b)` for per-preset brightness.
|
||||||
|
- Write pixels through `self.driver.n[...]` / `self.driver.n.fill(...)`.
|
||||||
|
- Flush frame with `self.driver.n.write()`.
|
||||||
|
- If a pattern needs to clear, use black `(0, 0, 0)`.
|
||||||
|
|
||||||
|
## Step semantics
|
||||||
|
|
||||||
|
- `self.driver.step` is shared pattern state managed by `Presets.select(...)` and patterns.
|
||||||
|
- Patterns that use step-based progression should update `self.driver.step` themselves.
|
||||||
|
- `select(..., step=...)` may set an explicit starting step.
|
||||||
|
|
||||||
|
## Error handling
|
||||||
|
|
||||||
|
- Let unexpected errors raise inside the generator.
|
||||||
|
- `Presets.tick()` catches exceptions, logs, and stops the active generator.
|
||||||
|
- Pattern code should not swallow broad exceptions unless there is a clear recovery path.
|
||||||
|
|
||||||
|
## Built-ins
|
||||||
|
|
||||||
|
- `off` and `on` are built-in methods on `Presets`, not loaded from this folder.
|
||||||
|
- `__init__.py` is ignored by dynamic loader.
|
||||||
2
lib/microdot/__init__.py
Normal file
2
lib/microdot/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
from microdot.microdot import Microdot, Request, Response, abort, redirect, \
|
||||||
|
send_file # noqa: F401
|
||||||
8
lib/microdot/helpers.py
Normal file
8
lib/microdot/helpers.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
try:
|
||||||
|
from functools import wraps
|
||||||
|
except ImportError: # pragma: no cover
|
||||||
|
# MicroPython does not currently implement functools.wraps
|
||||||
|
def wraps(wrapped):
|
||||||
|
def _(wrapper):
|
||||||
|
return wrapper
|
||||||
|
return _
|
||||||
1450
lib/microdot/microdot.py
Normal file
1450
lib/microdot/microdot.py
Normal file
File diff suppressed because it is too large
Load Diff
225
lib/microdot/session.py
Normal file
225
lib/microdot/session.py
Normal file
@@ -0,0 +1,225 @@
|
|||||||
|
try:
|
||||||
|
import jwt
|
||||||
|
HAS_JWT = True
|
||||||
|
except ImportError:
|
||||||
|
HAS_JWT = False
|
||||||
|
try:
|
||||||
|
import ubinascii
|
||||||
|
except ImportError:
|
||||||
|
import binascii as ubinascii
|
||||||
|
try:
|
||||||
|
import uhashlib as hashlib
|
||||||
|
except ImportError:
|
||||||
|
import hashlib
|
||||||
|
try:
|
||||||
|
import uhmac as hmac
|
||||||
|
except ImportError:
|
||||||
|
try:
|
||||||
|
import hmac
|
||||||
|
except ImportError:
|
||||||
|
hmac = None
|
||||||
|
import json
|
||||||
|
|
||||||
|
from microdot.microdot import invoke_handler
|
||||||
|
from microdot.helpers import wraps
|
||||||
|
|
||||||
|
|
||||||
|
class SessionDict(dict):
|
||||||
|
"""A session dictionary.
|
||||||
|
|
||||||
|
The session dictionary is a standard Python dictionary that has been
|
||||||
|
extended with convenience ``save()`` and ``delete()`` methods.
|
||||||
|
"""
|
||||||
|
def __init__(self, request, session_dict):
|
||||||
|
super().__init__(session_dict)
|
||||||
|
self.request = request
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
"""Update the session cookie."""
|
||||||
|
self.request.app._session.update(self.request, self)
|
||||||
|
|
||||||
|
def delete(self):
|
||||||
|
"""Delete the session cookie."""
|
||||||
|
self.request.app._session.delete(self.request)
|
||||||
|
|
||||||
|
|
||||||
|
class Session:
|
||||||
|
"""Session handling
|
||||||
|
|
||||||
|
:param app: The application instance.
|
||||||
|
:param secret_key: The secret key, as a string or bytes object.
|
||||||
|
:param cookie_options: A dictionary with cookie options to pass as
|
||||||
|
arguments to :meth:`Response.set_cookie()
|
||||||
|
<microdot.Response.set_cookie>`.
|
||||||
|
"""
|
||||||
|
secret_key = None
|
||||||
|
|
||||||
|
def __init__(self, app=None, secret_key=None, cookie_options=None):
|
||||||
|
self.secret_key = secret_key
|
||||||
|
self.cookie_options = cookie_options or {}
|
||||||
|
if app is not None:
|
||||||
|
self.initialize(app)
|
||||||
|
|
||||||
|
def initialize(self, app, secret_key=None, cookie_options=None):
|
||||||
|
if secret_key is not None:
|
||||||
|
self.secret_key = secret_key
|
||||||
|
if cookie_options is not None:
|
||||||
|
self.cookie_options = cookie_options
|
||||||
|
if 'path' not in self.cookie_options:
|
||||||
|
self.cookie_options['path'] = '/'
|
||||||
|
if 'http_only' not in self.cookie_options:
|
||||||
|
self.cookie_options['http_only'] = True
|
||||||
|
app._session = self
|
||||||
|
|
||||||
|
def get(self, request):
|
||||||
|
"""Retrieve the user session.
|
||||||
|
|
||||||
|
:param request: The client request.
|
||||||
|
|
||||||
|
The return value is a session dictionary with the data stored in the
|
||||||
|
user's session, or ``{}`` if the session data is not available or
|
||||||
|
invalid.
|
||||||
|
"""
|
||||||
|
if not self.secret_key:
|
||||||
|
raise ValueError('The session secret key is not configured')
|
||||||
|
if hasattr(request.g, '_session'):
|
||||||
|
return request.g._session
|
||||||
|
session = request.cookies.get('session')
|
||||||
|
if session is None:
|
||||||
|
request.g._session = SessionDict(request, {})
|
||||||
|
return request.g._session
|
||||||
|
request.g._session = SessionDict(request, self.decode(session))
|
||||||
|
return request.g._session
|
||||||
|
|
||||||
|
def update(self, request, session):
|
||||||
|
"""Update the user session.
|
||||||
|
|
||||||
|
:param request: The client request.
|
||||||
|
:param session: A dictionary with the update session data for the user.
|
||||||
|
|
||||||
|
Applications would normally not call this method directly, instead they
|
||||||
|
would use the :meth:`SessionDict.save` method on the session
|
||||||
|
dictionary, which calls this method. For example::
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
@with_session
|
||||||
|
def index(request, session):
|
||||||
|
session['foo'] = 'bar'
|
||||||
|
session.save()
|
||||||
|
return 'Hello, World!'
|
||||||
|
|
||||||
|
Calling this method adds a cookie with the updated session to the
|
||||||
|
request currently being processed.
|
||||||
|
"""
|
||||||
|
if not self.secret_key:
|
||||||
|
raise ValueError('The session secret key is not configured')
|
||||||
|
|
||||||
|
encoded_session = self.encode(session)
|
||||||
|
|
||||||
|
@request.after_request
|
||||||
|
def _update_session(request, response):
|
||||||
|
response.set_cookie('session', encoded_session,
|
||||||
|
**self.cookie_options)
|
||||||
|
return response
|
||||||
|
|
||||||
|
def delete(self, request):
|
||||||
|
"""Remove the user session.
|
||||||
|
|
||||||
|
:param request: The client request.
|
||||||
|
|
||||||
|
Applications would normally not call this method directly, instead they
|
||||||
|
would use the :meth:`SessionDict.delete` method on the session
|
||||||
|
dictionary, which calls this method. For example::
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
@with_session
|
||||||
|
def index(request, session):
|
||||||
|
session.delete()
|
||||||
|
return 'Hello, World!'
|
||||||
|
|
||||||
|
Calling this method adds a cookie removal header to the request
|
||||||
|
currently being processed.
|
||||||
|
"""
|
||||||
|
@request.after_request
|
||||||
|
def _delete_session(request, response):
|
||||||
|
response.delete_cookie('session', **self.cookie_options)
|
||||||
|
return response
|
||||||
|
|
||||||
|
def encode(self, payload, secret_key=None):
|
||||||
|
"""Encode session data using JWT if available, otherwise use simple HMAC."""
|
||||||
|
if HAS_JWT:
|
||||||
|
return jwt.encode(payload, secret_key or self.secret_key,
|
||||||
|
algorithm='HS256')
|
||||||
|
else:
|
||||||
|
# Simple encoding for MicroPython: base64(json) + HMAC signature
|
||||||
|
key = (secret_key or self.secret_key).encode() if isinstance(secret_key or self.secret_key, str) else (secret_key or self.secret_key)
|
||||||
|
payload_json = json.dumps(payload)
|
||||||
|
payload_b64 = ubinascii.b2a_base64(payload_json.encode()).decode().strip()
|
||||||
|
|
||||||
|
# Create HMAC signature
|
||||||
|
if hmac:
|
||||||
|
# Use hmac module if available
|
||||||
|
h = hmac.new(key, payload_json.encode(), hashlib.sha256)
|
||||||
|
else:
|
||||||
|
# Fallback: simple SHA256(key + message)
|
||||||
|
h = hashlib.sha256(key + payload_json.encode())
|
||||||
|
signature = ubinascii.b2a_base64(h.digest()).decode().strip()
|
||||||
|
|
||||||
|
return f"{payload_b64}.{signature}"
|
||||||
|
|
||||||
|
def decode(self, session, secret_key=None):
|
||||||
|
"""Decode session data using JWT if available, otherwise use simple HMAC."""
|
||||||
|
if HAS_JWT:
|
||||||
|
try:
|
||||||
|
payload = jwt.decode(session, secret_key or self.secret_key,
|
||||||
|
algorithms=['HS256'])
|
||||||
|
except jwt.exceptions.PyJWTError: # pragma: no cover
|
||||||
|
return {}
|
||||||
|
return payload
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
# Simple decoding for MicroPython
|
||||||
|
if '.' not in session:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
payload_b64, signature = session.rsplit('.', 1)
|
||||||
|
payload_json = ubinascii.a2b_base64(payload_b64).decode()
|
||||||
|
|
||||||
|
# Verify HMAC signature
|
||||||
|
key = (secret_key or self.secret_key).encode() if isinstance(secret_key or self.secret_key, str) else (secret_key or self.secret_key)
|
||||||
|
if hmac:
|
||||||
|
# Use hmac module if available
|
||||||
|
h = hmac.new(key, payload_json.encode(), hashlib.sha256)
|
||||||
|
else:
|
||||||
|
# Fallback: simple SHA256(key + message)
|
||||||
|
h = hashlib.sha256(key + payload_json.encode())
|
||||||
|
expected_signature = ubinascii.b2a_base64(h.digest()).decode().strip()
|
||||||
|
|
||||||
|
if signature != expected_signature:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
return json.loads(payload_json)
|
||||||
|
except Exception:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
|
def with_session(f):
|
||||||
|
"""Decorator that passes the user session to the route handler.
|
||||||
|
|
||||||
|
The session dictionary is passed to the decorated function as an argument
|
||||||
|
after the request object. Example::
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
@with_session
|
||||||
|
def index(request, session):
|
||||||
|
return 'Hello, World!'
|
||||||
|
|
||||||
|
Note that the decorator does not save the session. To update the session,
|
||||||
|
call the :func:`session.save() <microdot.session.SessionDict.save>` method.
|
||||||
|
"""
|
||||||
|
@wraps(f)
|
||||||
|
async def wrapper(request, *args, **kwargs):
|
||||||
|
return await invoke_handler(
|
||||||
|
f, request, request.app._session.get(request), *args, **kwargs)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
70
lib/microdot/utemplate.py
Normal file
70
lib/microdot/utemplate.py
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
from utemplate import recompile
|
||||||
|
|
||||||
|
_loader = None
|
||||||
|
|
||||||
|
|
||||||
|
class Template:
|
||||||
|
"""A template object.
|
||||||
|
|
||||||
|
:param template: The filename of the template to render, relative to the
|
||||||
|
configured template directory.
|
||||||
|
"""
|
||||||
|
@classmethod
|
||||||
|
def initialize(cls, template_dir='templates',
|
||||||
|
loader_class=recompile.Loader):
|
||||||
|
"""Initialize the templating subsystem.
|
||||||
|
|
||||||
|
:param template_dir: the directory where templates are stored. This
|
||||||
|
argument is optional. The default is to load
|
||||||
|
templates from a *templates* subdirectory.
|
||||||
|
:param loader_class: the ``utemplate.Loader`` class to use when loading
|
||||||
|
templates. This argument is optional. The default
|
||||||
|
is the ``recompile.Loader`` class, which
|
||||||
|
automatically recompiles templates when they
|
||||||
|
change.
|
||||||
|
"""
|
||||||
|
global _loader
|
||||||
|
_loader = loader_class(None, template_dir)
|
||||||
|
|
||||||
|
def __init__(self, template):
|
||||||
|
if _loader is None: # pragma: no cover
|
||||||
|
self.initialize()
|
||||||
|
#: The name of the template
|
||||||
|
self.name = template
|
||||||
|
self.template = _loader.load(template)
|
||||||
|
|
||||||
|
def generate(self, *args, **kwargs):
|
||||||
|
"""Return a generator that renders the template in chunks, with the
|
||||||
|
given arguments."""
|
||||||
|
return self.template(*args, **kwargs)
|
||||||
|
|
||||||
|
def render(self, *args, **kwargs):
|
||||||
|
"""Render the template with the given arguments and return it as a
|
||||||
|
string."""
|
||||||
|
return ''.join(self.generate(*args, **kwargs))
|
||||||
|
|
||||||
|
def generate_async(self, *args, **kwargs):
|
||||||
|
"""Return an asynchronous generator that renders the template in
|
||||||
|
chunks, using the given arguments."""
|
||||||
|
class sync_to_async_iter():
|
||||||
|
def __init__(self, iter):
|
||||||
|
self.iter = iter
|
||||||
|
|
||||||
|
def __aiter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def __anext__(self):
|
||||||
|
try:
|
||||||
|
return next(self.iter)
|
||||||
|
except StopIteration:
|
||||||
|
raise StopAsyncIteration
|
||||||
|
|
||||||
|
return sync_to_async_iter(self.generate(*args, **kwargs))
|
||||||
|
|
||||||
|
async def render_async(self, *args, **kwargs):
|
||||||
|
"""Render the template with the given arguments asynchronously and
|
||||||
|
return it as a string."""
|
||||||
|
response = ''
|
||||||
|
async for chunk in self.generate_async(*args, **kwargs):
|
||||||
|
response += chunk
|
||||||
|
return response
|
||||||
231
lib/microdot/websocket.py
Normal file
231
lib/microdot/websocket.py
Normal file
@@ -0,0 +1,231 @@
|
|||||||
|
import binascii
|
||||||
|
import hashlib
|
||||||
|
from microdot import Request, Response
|
||||||
|
from microdot.microdot import MUTED_SOCKET_ERRORS, print_exception
|
||||||
|
from microdot.helpers import wraps
|
||||||
|
|
||||||
|
|
||||||
|
class WebSocketError(Exception):
|
||||||
|
"""Exception raised when an error occurs in a WebSocket connection."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class WebSocket:
|
||||||
|
"""A WebSocket connection object.
|
||||||
|
|
||||||
|
An instance of this class is sent to handler functions to manage the
|
||||||
|
WebSocket connection.
|
||||||
|
"""
|
||||||
|
CONT = 0
|
||||||
|
TEXT = 1
|
||||||
|
BINARY = 2
|
||||||
|
CLOSE = 8
|
||||||
|
PING = 9
|
||||||
|
PONG = 10
|
||||||
|
|
||||||
|
#: Specify the maximum message size that can be received when calling the
|
||||||
|
#: ``receive()`` method. Messages with payloads that are larger than this
|
||||||
|
#: size will be rejected and the connection closed. Set to 0 to disable
|
||||||
|
#: the size check (be aware of potential security issues if you do this),
|
||||||
|
#: or to -1 to use the value set in
|
||||||
|
#: ``Request.max_body_length``. The default is -1.
|
||||||
|
#:
|
||||||
|
#: Example::
|
||||||
|
#:
|
||||||
|
#: WebSocket.max_message_length = 4 * 1024 # up to 4KB messages
|
||||||
|
max_message_length = -1
|
||||||
|
|
||||||
|
def __init__(self, request):
|
||||||
|
self.request = request
|
||||||
|
self.closed = False
|
||||||
|
|
||||||
|
async def handshake(self):
|
||||||
|
response = self._handshake_response()
|
||||||
|
await self.request.sock[1].awrite(
|
||||||
|
b'HTTP/1.1 101 Switching Protocols\r\n')
|
||||||
|
await self.request.sock[1].awrite(b'Upgrade: websocket\r\n')
|
||||||
|
await self.request.sock[1].awrite(b'Connection: Upgrade\r\n')
|
||||||
|
await self.request.sock[1].awrite(
|
||||||
|
b'Sec-WebSocket-Accept: ' + response + b'\r\n\r\n')
|
||||||
|
|
||||||
|
async def receive(self):
|
||||||
|
"""Receive a message from the client."""
|
||||||
|
while True:
|
||||||
|
opcode, payload = await self._read_frame()
|
||||||
|
send_opcode, data = self._process_websocket_frame(opcode, payload)
|
||||||
|
if send_opcode: # pragma: no cover
|
||||||
|
await self.send(data, send_opcode)
|
||||||
|
elif data: # pragma: no branch
|
||||||
|
return data
|
||||||
|
|
||||||
|
async def send(self, data, opcode=None):
|
||||||
|
"""Send a message to the client.
|
||||||
|
|
||||||
|
:param data: the data to send, given as a string or bytes.
|
||||||
|
:param opcode: a custom frame opcode to use. If not given, the opcode
|
||||||
|
is ``TEXT`` or ``BINARY`` depending on the type of the
|
||||||
|
data.
|
||||||
|
"""
|
||||||
|
frame = self._encode_websocket_frame(
|
||||||
|
opcode or (self.TEXT if isinstance(data, str) else self.BINARY),
|
||||||
|
data)
|
||||||
|
await self.request.sock[1].awrite(frame)
|
||||||
|
|
||||||
|
async def close(self):
|
||||||
|
"""Close the websocket connection."""
|
||||||
|
if not self.closed: # pragma: no cover
|
||||||
|
self.closed = True
|
||||||
|
await self.send(b'', self.CLOSE)
|
||||||
|
|
||||||
|
def _handshake_response(self):
|
||||||
|
connection = False
|
||||||
|
upgrade = False
|
||||||
|
websocket_key = None
|
||||||
|
for header, value in self.request.headers.items():
|
||||||
|
h = header.lower()
|
||||||
|
if h == 'connection':
|
||||||
|
connection = True
|
||||||
|
if 'upgrade' not in value.lower():
|
||||||
|
return self.request.app.abort(400)
|
||||||
|
elif h == 'upgrade':
|
||||||
|
upgrade = True
|
||||||
|
if not value.lower() == 'websocket':
|
||||||
|
return self.request.app.abort(400)
|
||||||
|
elif h == 'sec-websocket-key':
|
||||||
|
websocket_key = value
|
||||||
|
if not connection or not upgrade or not websocket_key:
|
||||||
|
return self.request.app.abort(400)
|
||||||
|
d = hashlib.sha1(websocket_key.encode())
|
||||||
|
d.update(b'258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
|
||||||
|
return binascii.b2a_base64(d.digest())[:-1]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _parse_frame_header(cls, header):
|
||||||
|
fin = header[0] & 0x80
|
||||||
|
opcode = header[0] & 0x0f
|
||||||
|
if fin == 0 or opcode == cls.CONT: # pragma: no cover
|
||||||
|
raise WebSocketError('Continuation frames not supported')
|
||||||
|
has_mask = header[1] & 0x80
|
||||||
|
length = header[1] & 0x7f
|
||||||
|
if length == 126:
|
||||||
|
length = -2
|
||||||
|
elif length == 127:
|
||||||
|
length = -8
|
||||||
|
return fin, opcode, has_mask, length
|
||||||
|
|
||||||
|
def _process_websocket_frame(self, opcode, payload):
|
||||||
|
if opcode == self.TEXT:
|
||||||
|
payload = payload.decode()
|
||||||
|
elif opcode == self.BINARY:
|
||||||
|
pass
|
||||||
|
elif opcode == self.CLOSE:
|
||||||
|
raise WebSocketError('Websocket connection closed')
|
||||||
|
elif opcode == self.PING:
|
||||||
|
return self.PONG, payload
|
||||||
|
elif opcode == self.PONG: # pragma: no branch
|
||||||
|
return None, None
|
||||||
|
return None, payload
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _encode_websocket_frame(cls, opcode, payload):
|
||||||
|
frame = bytearray()
|
||||||
|
frame.append(0x80 | opcode)
|
||||||
|
if opcode == cls.TEXT:
|
||||||
|
payload = payload.encode()
|
||||||
|
if len(payload) < 126:
|
||||||
|
frame.append(len(payload))
|
||||||
|
elif len(payload) < (1 << 16):
|
||||||
|
frame.append(126)
|
||||||
|
frame.extend(len(payload).to_bytes(2, 'big'))
|
||||||
|
else:
|
||||||
|
frame.append(127)
|
||||||
|
frame.extend(len(payload).to_bytes(8, 'big'))
|
||||||
|
frame.extend(payload)
|
||||||
|
return frame
|
||||||
|
|
||||||
|
async def _read_frame(self):
|
||||||
|
header = await self.request.sock[0].read(2)
|
||||||
|
if len(header) != 2: # pragma: no cover
|
||||||
|
raise WebSocketError('Websocket connection closed')
|
||||||
|
fin, opcode, has_mask, length = self._parse_frame_header(header)
|
||||||
|
if length == -2:
|
||||||
|
length = await self.request.sock[0].read(2)
|
||||||
|
length = int.from_bytes(length, 'big')
|
||||||
|
elif length == -8:
|
||||||
|
length = await self.request.sock[0].read(8)
|
||||||
|
length = int.from_bytes(length, 'big')
|
||||||
|
max_allowed_length = Request.max_body_length \
|
||||||
|
if self.max_message_length == -1 else self.max_message_length
|
||||||
|
if length > max_allowed_length:
|
||||||
|
raise WebSocketError('Message too large')
|
||||||
|
if has_mask: # pragma: no cover
|
||||||
|
mask = await self.request.sock[0].read(4)
|
||||||
|
payload = await self.request.sock[0].read(length)
|
||||||
|
if has_mask: # pragma: no cover
|
||||||
|
payload = bytes(x ^ mask[i % 4] for i, x in enumerate(payload))
|
||||||
|
return opcode, payload
|
||||||
|
|
||||||
|
|
||||||
|
async def websocket_upgrade(request):
|
||||||
|
"""Upgrade a request handler to a websocket connection.
|
||||||
|
|
||||||
|
This function can be called directly inside a route function to process a
|
||||||
|
WebSocket upgrade handshake, for example after the user's credentials are
|
||||||
|
verified. The function returns the websocket object::
|
||||||
|
|
||||||
|
@app.route('/echo')
|
||||||
|
async def echo(request):
|
||||||
|
if not authenticate_user(request):
|
||||||
|
abort(401)
|
||||||
|
ws = await websocket_upgrade(request)
|
||||||
|
while True:
|
||||||
|
message = await ws.receive()
|
||||||
|
await ws.send(message)
|
||||||
|
"""
|
||||||
|
ws = WebSocket(request)
|
||||||
|
await ws.handshake()
|
||||||
|
|
||||||
|
@request.after_request
|
||||||
|
async def after_request(request, response):
|
||||||
|
return Response.already_handled
|
||||||
|
|
||||||
|
return ws
|
||||||
|
|
||||||
|
|
||||||
|
def websocket_wrapper(f, upgrade_function):
|
||||||
|
@wraps(f)
|
||||||
|
async def wrapper(request, *args, **kwargs):
|
||||||
|
ws = await upgrade_function(request)
|
||||||
|
try:
|
||||||
|
await f(request, ws, *args, **kwargs)
|
||||||
|
except OSError as exc:
|
||||||
|
if exc.errno not in MUTED_SOCKET_ERRORS: # pragma: no cover
|
||||||
|
raise
|
||||||
|
except WebSocketError:
|
||||||
|
pass
|
||||||
|
except Exception as exc:
|
||||||
|
print_exception(exc)
|
||||||
|
finally: # pragma: no cover
|
||||||
|
try:
|
||||||
|
await ws.close()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return Response.already_handled
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def with_websocket(f):
|
||||||
|
"""Decorator to make a route a WebSocket endpoint.
|
||||||
|
|
||||||
|
This decorator is used to define a route that accepts websocket
|
||||||
|
connections. The route then receives a websocket object as a second
|
||||||
|
argument that it can use to send and receive messages::
|
||||||
|
|
||||||
|
@app.route('/echo')
|
||||||
|
@with_websocket
|
||||||
|
async def echo(request, ws):
|
||||||
|
while True:
|
||||||
|
message = await ws.receive()
|
||||||
|
await ws.send(message)
|
||||||
|
"""
|
||||||
|
return websocket_wrapper(f, websocket_upgrade)
|
||||||
0
lib/utemplate/__init__.py
Normal file
0
lib/utemplate/__init__.py
Normal file
14
lib/utemplate/compiled.py
Normal file
14
lib/utemplate/compiled.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
class Loader:
|
||||||
|
|
||||||
|
def __init__(self, pkg, dir):
|
||||||
|
if dir == ".":
|
||||||
|
dir = ""
|
||||||
|
else:
|
||||||
|
dir = dir.replace("/", ".") + "."
|
||||||
|
if pkg and pkg != "__main__":
|
||||||
|
dir = pkg + "." + dir
|
||||||
|
self.p = dir
|
||||||
|
|
||||||
|
def load(self, name):
|
||||||
|
name = name.replace(".", "_")
|
||||||
|
return __import__(self.p + name, None, None, (name,)).render
|
||||||
21
lib/utemplate/recompile.py
Normal file
21
lib/utemplate/recompile.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
# (c) 2014-2020 Paul Sokolovsky. MIT license.
|
||||||
|
try:
|
||||||
|
from uos import stat, remove
|
||||||
|
except:
|
||||||
|
from os import stat, remove
|
||||||
|
from . import source
|
||||||
|
|
||||||
|
|
||||||
|
class Loader(source.Loader):
|
||||||
|
|
||||||
|
def load(self, name):
|
||||||
|
o_path = self.pkg_path + self.compiled_path(name)
|
||||||
|
i_path = self.pkg_path + self.dir + "/" + name
|
||||||
|
try:
|
||||||
|
o_stat = stat(o_path)
|
||||||
|
i_stat = stat(i_path)
|
||||||
|
if i_stat[8] > o_stat[8]:
|
||||||
|
# input file is newer, remove output to force recompile
|
||||||
|
remove(o_path)
|
||||||
|
finally:
|
||||||
|
return super().load(name)
|
||||||
188
lib/utemplate/source.py
Normal file
188
lib/utemplate/source.py
Normal file
@@ -0,0 +1,188 @@
|
|||||||
|
# (c) 2014-2019 Paul Sokolovsky. MIT license.
|
||||||
|
from . import compiled
|
||||||
|
|
||||||
|
|
||||||
|
class Compiler:
|
||||||
|
|
||||||
|
START_CHAR = "{"
|
||||||
|
STMNT = "%"
|
||||||
|
STMNT_END = "%}"
|
||||||
|
EXPR = "{"
|
||||||
|
EXPR_END = "}}"
|
||||||
|
|
||||||
|
def __init__(self, file_in, file_out, indent=0, seq=0, loader=None):
|
||||||
|
self.file_in = file_in
|
||||||
|
self.file_out = file_out
|
||||||
|
self.loader = loader
|
||||||
|
self.seq = seq
|
||||||
|
self._indent = indent
|
||||||
|
self.stack = []
|
||||||
|
self.in_literal = False
|
||||||
|
self.flushed_header = False
|
||||||
|
self.args = "*a, **d"
|
||||||
|
|
||||||
|
def indent(self, adjust=0):
|
||||||
|
if not self.flushed_header:
|
||||||
|
self.flushed_header = True
|
||||||
|
self.indent()
|
||||||
|
self.file_out.write("def render%s(%s):\n" % (str(self.seq) if self.seq else "", self.args))
|
||||||
|
self.stack.append("def")
|
||||||
|
self.file_out.write(" " * (len(self.stack) + self._indent + adjust))
|
||||||
|
|
||||||
|
def literal(self, s):
|
||||||
|
if not s:
|
||||||
|
return
|
||||||
|
if not self.in_literal:
|
||||||
|
self.indent()
|
||||||
|
self.file_out.write('yield """')
|
||||||
|
self.in_literal = True
|
||||||
|
self.file_out.write(s.replace('"', '\\"'))
|
||||||
|
|
||||||
|
def close_literal(self):
|
||||||
|
if self.in_literal:
|
||||||
|
self.file_out.write('"""\n')
|
||||||
|
self.in_literal = False
|
||||||
|
|
||||||
|
def render_expr(self, e):
|
||||||
|
self.indent()
|
||||||
|
self.file_out.write('yield str(' + e + ')\n')
|
||||||
|
|
||||||
|
def parse_statement(self, stmt):
|
||||||
|
tokens = stmt.split(None, 1)
|
||||||
|
if tokens[0] == "args":
|
||||||
|
if len(tokens) > 1:
|
||||||
|
self.args = tokens[1]
|
||||||
|
else:
|
||||||
|
self.args = ""
|
||||||
|
elif tokens[0] == "set":
|
||||||
|
self.indent()
|
||||||
|
self.file_out.write(stmt[3:].strip() + "\n")
|
||||||
|
elif tokens[0] == "include":
|
||||||
|
if not self.flushed_header:
|
||||||
|
# If there was no other output, we still need a header now
|
||||||
|
self.indent()
|
||||||
|
tokens = tokens[1].split(None, 1)
|
||||||
|
args = ""
|
||||||
|
if len(tokens) > 1:
|
||||||
|
args = tokens[1]
|
||||||
|
if tokens[0][0] == "{":
|
||||||
|
self.indent()
|
||||||
|
# "1" as fromlist param is uPy hack
|
||||||
|
self.file_out.write('_ = __import__(%s.replace(".", "_"), None, None, 1)\n' % tokens[0][2:-2])
|
||||||
|
self.indent()
|
||||||
|
self.file_out.write("yield from _.render(%s)\n" % args)
|
||||||
|
return
|
||||||
|
|
||||||
|
with self.loader.input_open(tokens[0][1:-1]) as inc:
|
||||||
|
self.seq += 1
|
||||||
|
c = Compiler(inc, self.file_out, len(self.stack) + self._indent, self.seq)
|
||||||
|
inc_id = self.seq
|
||||||
|
self.seq = c.compile()
|
||||||
|
self.indent()
|
||||||
|
self.file_out.write("yield from render%d(%s)\n" % (inc_id, args))
|
||||||
|
elif len(tokens) > 1:
|
||||||
|
if tokens[0] == "elif":
|
||||||
|
assert self.stack[-1] == "if"
|
||||||
|
self.indent(-1)
|
||||||
|
self.file_out.write(stmt + ":\n")
|
||||||
|
else:
|
||||||
|
self.indent()
|
||||||
|
self.file_out.write(stmt + ":\n")
|
||||||
|
self.stack.append(tokens[0])
|
||||||
|
else:
|
||||||
|
if stmt.startswith("end"):
|
||||||
|
assert self.stack[-1] == stmt[3:]
|
||||||
|
self.stack.pop(-1)
|
||||||
|
elif stmt == "else":
|
||||||
|
assert self.stack[-1] == "if"
|
||||||
|
self.indent(-1)
|
||||||
|
self.file_out.write("else:\n")
|
||||||
|
else:
|
||||||
|
assert False
|
||||||
|
|
||||||
|
def parse_line(self, l):
|
||||||
|
while l:
|
||||||
|
start = l.find(self.START_CHAR)
|
||||||
|
if start == -1:
|
||||||
|
self.literal(l)
|
||||||
|
return
|
||||||
|
self.literal(l[:start])
|
||||||
|
self.close_literal()
|
||||||
|
sel = l[start + 1]
|
||||||
|
#print("*%s=%s=" % (sel, EXPR))
|
||||||
|
if sel == self.STMNT:
|
||||||
|
end = l.find(self.STMNT_END)
|
||||||
|
assert end > 0
|
||||||
|
stmt = l[start + len(self.START_CHAR + self.STMNT):end].strip()
|
||||||
|
self.parse_statement(stmt)
|
||||||
|
end += len(self.STMNT_END)
|
||||||
|
l = l[end:]
|
||||||
|
if not self.in_literal and l == "\n":
|
||||||
|
break
|
||||||
|
elif sel == self.EXPR:
|
||||||
|
# print("EXPR")
|
||||||
|
end = l.find(self.EXPR_END)
|
||||||
|
assert end > 0
|
||||||
|
expr = l[start + len(self.START_CHAR + self.EXPR):end].strip()
|
||||||
|
self.render_expr(expr)
|
||||||
|
end += len(self.EXPR_END)
|
||||||
|
l = l[end:]
|
||||||
|
else:
|
||||||
|
self.literal(l[start])
|
||||||
|
l = l[start + 1:]
|
||||||
|
|
||||||
|
def header(self):
|
||||||
|
self.file_out.write("# Autogenerated file\n")
|
||||||
|
|
||||||
|
def compile(self):
|
||||||
|
self.header()
|
||||||
|
for l in self.file_in:
|
||||||
|
self.parse_line(l)
|
||||||
|
self.close_literal()
|
||||||
|
return self.seq
|
||||||
|
|
||||||
|
|
||||||
|
class Loader(compiled.Loader):
|
||||||
|
|
||||||
|
def __init__(self, pkg, dir):
|
||||||
|
super().__init__(pkg, dir)
|
||||||
|
self.dir = dir
|
||||||
|
if pkg == "__main__":
|
||||||
|
# if pkg isn't really a package, don't bother to use it
|
||||||
|
# it means we're running from "filesystem directory", not
|
||||||
|
# from a package.
|
||||||
|
pkg = None
|
||||||
|
|
||||||
|
self.pkg_path = ""
|
||||||
|
if pkg:
|
||||||
|
p = __import__(pkg)
|
||||||
|
if isinstance(p.__path__, str):
|
||||||
|
# uPy
|
||||||
|
self.pkg_path = p.__path__
|
||||||
|
else:
|
||||||
|
# CPy
|
||||||
|
self.pkg_path = p.__path__[0]
|
||||||
|
self.pkg_path += "/"
|
||||||
|
|
||||||
|
def input_open(self, template):
|
||||||
|
path = self.pkg_path + self.dir + "/" + template
|
||||||
|
return open(path)
|
||||||
|
|
||||||
|
def compiled_path(self, template):
|
||||||
|
return self.dir + "/" + template.replace(".", "_") + ".py"
|
||||||
|
|
||||||
|
def load(self, name):
|
||||||
|
try:
|
||||||
|
return super().load(name)
|
||||||
|
except (OSError, ImportError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
compiled_path = self.pkg_path + self.compiled_path(name)
|
||||||
|
|
||||||
|
f_in = self.input_open(name)
|
||||||
|
f_out = open(compiled_path, "w")
|
||||||
|
c = Compiler(f_in, f_out, loader=self)
|
||||||
|
c.compile()
|
||||||
|
f_in.close()
|
||||||
|
f_out.close()
|
||||||
|
return super().load(name)
|
||||||
1
presets.json
Normal file
1
presets.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"15": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 0, 0], [0, 255, 0]], "b": 255, "n2": 0, "n1": 0, "p": "blink", "n3": 0, "d": 500}, "40": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 255, 0]], "b": 255, "n2": 2600, "n1": 35, "p": "flame", "n3": 0, "d": 50}, "41": {"n5": 0, "n4": 5, "a": true, "n6": 0, "c": [[120, 200, 255], [80, 140, 255], [180, 120, 255], [100, 220, 232], [160, 200, 255]], "b": 255, "n2": 10, "n1": 72, "p": "twinkle", "n3": 5, "d": 500}, "42": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[166, 0, 255], [0, 10, 10]], "b": 255, "n2": 900, "n1": 30, "p": "radiate", "n3": 4000, "d": 5000}, "6": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[0, 255, 0]], "b": 255, "n2": 500, "n1": 1000, "p": "pulse", "n3": 1000, "d": 500}, "10": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[230, 242, 255]], "b": 200, "n2": 0, "n1": 0, "p": "on", "n3": 0, "d": 100}, "13": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 255, 255]], "b": 255, "n2": 0, "n1": 1, "p": "rainbow", "n3": 0, "d": 150}, "3": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 255, 255]], "b": 255, "n2": 0, "n1": 2, "p": "rainbow", "n3": 0, "d": 100}, "2": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 255, 255]], "b": 0, "n2": 0, "n1": 0, "p": "off", "n3": 0, "d": 100}, "38": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 0, 0], [0, 0, 255]], "b": 255, "n2": 0, "n1": 1, "p": "colour_cycle", "n3": 0, "d": 100}, "11": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 0, 0]], "b": 255, "n2": 0, "n1": 0, "p": "on", "n3": 0, "d": 100}, "12": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[0, 0, 255]], "b": 255, "n2": 0, "n1": 0, "p": "on", "n3": 0, "d": 100}, "1": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 255, 255]], "b": 255, "n2": 0, "n1": 0, "p": "on", "n3": 0, "d": 100}, "9": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 245, 230]], "b": 200, "n2": 0, "n1": 0, "p": "on", "n3": 0, "d": 100}, "8": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0]], "b": 255, "n2": 0, "n1": 0, "p": "blink", "n3": 0, "d": 1000}, "39": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 184, 77]], "b": 255, "n2": 0, "n1": 30, "p": "flicker", "n3": 0, "d": 80}, "14": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 102, 0]], "b": 255, "n2": 1000, "n1": 2000, "p": "pulse", "n3": 2000, "d": 800}, "5": {"n5": 0, "n4": 1, "a": true, "n6": 0, "c": [[255, 0, 0], [0, 0, 255]], "b": 255, "n2": 5, "n1": 5, "p": "chase", "n3": 1, "d": 200}, "4": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 255], [0, 0, 255], [255, 255, 0]], "b": 255, "n2": 0, "n1": 0, "p": "transition", "n3": 0, "d": 5000}, "7": {"n5": 0, "n4": 5, "a": true, "n6": 0, "c": [[255, 165, 0], [128, 0, 128]], "b": 255, "n2": 10, "n1": 2, "p": "circle", "n3": 2, "d": 200}}
|
||||||
42
src/background_tasks.py
Normal file
42
src/background_tasks.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import asyncio
|
||||||
|
import gc
|
||||||
|
import utime
|
||||||
|
|
||||||
|
from hello import broadcast_hello_udp
|
||||||
|
|
||||||
|
|
||||||
|
async def presets_loop(presets, wdt):
|
||||||
|
last_mem_log = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
presets.tick()
|
||||||
|
wdt.feed()
|
||||||
|
if bool(getattr(presets, "debug", False)):
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last_mem_log) >= 5000:
|
||||||
|
gc.collect()
|
||||||
|
print("mem runtime:", {"free": gc.mem_free(), "alloc": gc.mem_alloc()})
|
||||||
|
last_mem_log = now
|
||||||
|
# tick() does not await; yield so UDP hello and HTTP/WebSocket can run.
|
||||||
|
await asyncio.sleep(0)
|
||||||
|
|
||||||
|
|
||||||
|
async def udp_hello_loop_after_http_ready(sta_if, settings, wdt, runtime_state):
|
||||||
|
"""Broadcast hello at startup-fast cadence, then slower cadence."""
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
started_ms = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
if runtime_state.hello:
|
||||||
|
print("UDP hello: broadcasting...")
|
||||||
|
try:
|
||||||
|
broadcast_hello_udp(
|
||||||
|
sta_if,
|
||||||
|
settings.get("name", ""),
|
||||||
|
wait_reply=False,
|
||||||
|
wdt=wdt,
|
||||||
|
dual_destinations=True,
|
||||||
|
)
|
||||||
|
except Exception as ex:
|
||||||
|
print("UDP hello broadcast failed:", ex)
|
||||||
|
elapsed_ms = utime.ticks_diff(utime.ticks_ms(), started_ms)
|
||||||
|
interval_s = 5 if elapsed_ms < 60000 else 60
|
||||||
|
await asyncio.sleep(interval_s)
|
||||||
209
src/binary_envelope.py
Normal file
209
src/binary_envelope.py
Normal file
@@ -0,0 +1,209 @@
|
|||||||
|
"""Decode compact binary controller envelopes — v2 native binary, v1 legacy JSON blobs."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import struct
|
||||||
|
|
||||||
|
BINARY_ENVELOPE_VERSION_1 = 1
|
||||||
|
BINARY_ENVELOPE_VERSION_2 = 2
|
||||||
|
HEADER_LEN = 5
|
||||||
|
|
||||||
|
|
||||||
|
def _brightness_0_255_from_wire(wire):
|
||||||
|
w = max(0, min(127, int(wire)))
|
||||||
|
return min(255, (w * 255) // 127)
|
||||||
|
|
||||||
|
|
||||||
|
def _decode_preset_record(buf, off):
|
||||||
|
nl = buf[off]
|
||||||
|
off += 1
|
||||||
|
name = buf[off : off + nl].decode("utf-8")
|
||||||
|
off += nl
|
||||||
|
pl = buf[off]
|
||||||
|
off += 1
|
||||||
|
pattern = buf[off : off + pl].decode("utf-8")
|
||||||
|
off += pl
|
||||||
|
nc = buf[off]
|
||||||
|
off += 1
|
||||||
|
colors = []
|
||||||
|
for _ in range(nc):
|
||||||
|
r, g, b = buf[off], buf[off + 1], buf[off + 2]
|
||||||
|
off += 3
|
||||||
|
colors.append("#%02x%02x%02x" % (r, g, b))
|
||||||
|
if off + 16 > len(buf):
|
||||||
|
raise ValueError("truncated")
|
||||||
|
delay, br, auto, n1, n2, n3, n4, n5, n6 = struct.unpack_from(
|
||||||
|
"<HBBhhhhhh", buf, off
|
||||||
|
)
|
||||||
|
off += 16
|
||||||
|
preset = {
|
||||||
|
"p": pattern,
|
||||||
|
"c": colors,
|
||||||
|
"d": delay,
|
||||||
|
"b": br,
|
||||||
|
"a": bool(auto),
|
||||||
|
"n1": n1,
|
||||||
|
"n2": n2,
|
||||||
|
"n3": n3,
|
||||||
|
"n4": n4,
|
||||||
|
"n5": n5,
|
||||||
|
"n6": n6,
|
||||||
|
}
|
||||||
|
return name, preset, off
|
||||||
|
|
||||||
|
|
||||||
|
def _decode_presets_blob(chunk):
|
||||||
|
if not chunk:
|
||||||
|
return {}
|
||||||
|
off = 0
|
||||||
|
count = chunk[off]
|
||||||
|
off += 1
|
||||||
|
out = {}
|
||||||
|
for _ in range(count):
|
||||||
|
name, preset, off = _decode_preset_record(chunk, off)
|
||||||
|
out[name] = preset
|
||||||
|
if off != len(chunk):
|
||||||
|
raise ValueError("presets blob mismatch")
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def _decode_select_blob(chunk):
|
||||||
|
if not chunk:
|
||||||
|
return {}
|
||||||
|
off = 0
|
||||||
|
count = chunk[off]
|
||||||
|
off += 1
|
||||||
|
out = {}
|
||||||
|
for _ in range(count):
|
||||||
|
dl = chunk[off]
|
||||||
|
off += 1
|
||||||
|
device = chunk[off : off + dl].decode("utf-8")
|
||||||
|
off += dl
|
||||||
|
pl = chunk[off]
|
||||||
|
off += 1
|
||||||
|
pname = chunk[off : off + pl].decode("utf-8")
|
||||||
|
off += pl
|
||||||
|
has_step = chunk[off]
|
||||||
|
off += 1
|
||||||
|
if has_step:
|
||||||
|
step = struct.unpack_from("<H", chunk, off)[0]
|
||||||
|
off += 2
|
||||||
|
out[device] = [pname, step]
|
||||||
|
else:
|
||||||
|
out[device] = [pname]
|
||||||
|
if off != len(chunk):
|
||||||
|
raise ValueError("select blob mismatch")
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def _decode_default_blob(chunk):
|
||||||
|
if not chunk:
|
||||||
|
return "", []
|
||||||
|
off = 0
|
||||||
|
nl = chunk[off]
|
||||||
|
off += 1
|
||||||
|
default_name = chunk[off : off + nl].decode("utf-8") if nl else ""
|
||||||
|
off += nl
|
||||||
|
nt = chunk[off]
|
||||||
|
off += 1
|
||||||
|
targets = []
|
||||||
|
for _ in range(nt):
|
||||||
|
tl = chunk[off]
|
||||||
|
off += 1
|
||||||
|
targets.append(chunk[off : off + tl].decode("utf-8"))
|
||||||
|
off += tl
|
||||||
|
if off != len(chunk):
|
||||||
|
raise ValueError("default blob mismatch")
|
||||||
|
return default_name, targets
|
||||||
|
|
||||||
|
|
||||||
|
def parse_binary_envelope_v2(buf):
|
||||||
|
if not isinstance(buf, (bytes, bytearray)) or len(buf) < HEADER_LEN:
|
||||||
|
return None
|
||||||
|
if buf[0] != BINARY_ENVELOPE_VERSION_2:
|
||||||
|
return None
|
||||||
|
lp = buf[2]
|
||||||
|
ls = buf[3]
|
||||||
|
ld = buf[4]
|
||||||
|
need = HEADER_LEN + lp + ls + ld
|
||||||
|
if len(buf) != need:
|
||||||
|
return None
|
||||||
|
|
||||||
|
off = HEADER_LEN
|
||||||
|
presets_chunk = buf[off : off + lp]
|
||||||
|
off += lp
|
||||||
|
select_chunk = buf[off : off + ls]
|
||||||
|
off += ls
|
||||||
|
default_chunk = buf[off : off + ld]
|
||||||
|
|
||||||
|
data = {"v": "1"}
|
||||||
|
br = buf[1]
|
||||||
|
if br < 128:
|
||||||
|
data["b"] = _brightness_0_255_from_wire(br)
|
||||||
|
|
||||||
|
try:
|
||||||
|
if lp:
|
||||||
|
data["presets"] = _decode_presets_blob(presets_chunk)
|
||||||
|
if ls:
|
||||||
|
data["select"] = _decode_select_blob(select_chunk)
|
||||||
|
if ld:
|
||||||
|
dname, targets = _decode_default_blob(default_chunk)
|
||||||
|
data["default"] = dname
|
||||||
|
data["targets"] = targets
|
||||||
|
except (ValueError, UnicodeError, TypeError, struct.error):
|
||||||
|
return None
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def parse_binary_envelope_v1(buf):
|
||||||
|
if not isinstance(buf, (bytes, bytearray)) or len(buf) < HEADER_LEN:
|
||||||
|
return None
|
||||||
|
if buf[0] != BINARY_ENVELOPE_VERSION_1:
|
||||||
|
return None
|
||||||
|
lp = buf[2]
|
||||||
|
ls = buf[3]
|
||||||
|
ld = buf[4]
|
||||||
|
need = HEADER_LEN + lp + ls + ld
|
||||||
|
if len(buf) != need:
|
||||||
|
return None
|
||||||
|
|
||||||
|
off = HEADER_LEN
|
||||||
|
presets_chunk = buf[off : off + lp]
|
||||||
|
off += lp
|
||||||
|
select_chunk = buf[off : off + ls]
|
||||||
|
off += ls
|
||||||
|
default_chunk = buf[off : off + ld]
|
||||||
|
|
||||||
|
data = {"v": "1"}
|
||||||
|
|
||||||
|
br = buf[1]
|
||||||
|
if br < 128:
|
||||||
|
data["b"] = _brightness_0_255_from_wire(br)
|
||||||
|
|
||||||
|
if lp:
|
||||||
|
try:
|
||||||
|
data["presets"] = json.loads(presets_chunk.decode("utf-8"))
|
||||||
|
except (ValueError, UnicodeError):
|
||||||
|
return None
|
||||||
|
if ls:
|
||||||
|
try:
|
||||||
|
data["select"] = json.loads(select_chunk.decode("utf-8"))
|
||||||
|
except (ValueError, UnicodeError):
|
||||||
|
return None
|
||||||
|
if ld:
|
||||||
|
try:
|
||||||
|
extra = json.loads(default_chunk.decode("utf-8"))
|
||||||
|
except (ValueError, UnicodeError):
|
||||||
|
return None
|
||||||
|
if isinstance(extra, dict):
|
||||||
|
for k, v in extra.items():
|
||||||
|
data[k] = v
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def parse_binary_envelope(buf):
|
||||||
|
d = parse_binary_envelope_v2(buf)
|
||||||
|
if d is not None:
|
||||||
|
return d
|
||||||
|
return parse_binary_envelope_v1(buf)
|
||||||
251
src/controller_messages.py
Normal file
251
src/controller_messages.py
Normal file
@@ -0,0 +1,251 @@
|
|||||||
|
"""Parse controller JSON (v1) and apply brightness, presets, OTA patterns, etc."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import socket
|
||||||
|
|
||||||
|
from binary_envelope import parse_binary_envelope
|
||||||
|
from utils import convert_and_reorder_colors
|
||||||
|
|
||||||
|
try:
|
||||||
|
import uos as os
|
||||||
|
except ImportError:
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def process_data(payload, settings, presets, controller_ip=None):
|
||||||
|
"""Read one controller message; binary v1 envelope or JSON v1, then apply fields."""
|
||||||
|
data = None
|
||||||
|
if isinstance(payload, (bytes, bytearray)):
|
||||||
|
data = parse_binary_envelope(payload)
|
||||||
|
if data is None:
|
||||||
|
try:
|
||||||
|
data = json.loads(payload)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
data = json.loads(payload)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
return
|
||||||
|
print(payload)
|
||||||
|
if data.get("v", "") != "1":
|
||||||
|
return
|
||||||
|
if "b" in data:
|
||||||
|
apply_brightness(data, settings, presets)
|
||||||
|
if "presets" in data:
|
||||||
|
apply_presets(data, settings, presets)
|
||||||
|
if "clear_presets" in data:
|
||||||
|
apply_clear_presets(data, presets)
|
||||||
|
if "select" in data:
|
||||||
|
apply_select(data, settings, presets)
|
||||||
|
if "default" in data:
|
||||||
|
apply_default(data, settings, presets)
|
||||||
|
if "manifest" in data:
|
||||||
|
apply_patterns_ota(data, presets, controller_ip=controller_ip)
|
||||||
|
if "save" in data and ("presets" in data or "default" in data):
|
||||||
|
presets.save()
|
||||||
|
if "save" in data and "clear_presets" in data:
|
||||||
|
presets.save()
|
||||||
|
if "save" in data and "b" in data:
|
||||||
|
settings.save()
|
||||||
|
|
||||||
|
|
||||||
|
def apply_brightness(data, settings, presets):
|
||||||
|
try:
|
||||||
|
presets.b = max(0, min(255, int(data["b"])))
|
||||||
|
settings["brightness"] = presets.b
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def apply_presets(data, settings, presets):
|
||||||
|
presets_map = data["presets"]
|
||||||
|
for id, preset_data in presets_map.items():
|
||||||
|
if not preset_data:
|
||||||
|
continue
|
||||||
|
color_key = "c" if "c" in preset_data else ("colors" if "colors" in preset_data else None)
|
||||||
|
if color_key is not None:
|
||||||
|
try:
|
||||||
|
preset_data[color_key] = convert_and_reorder_colors(
|
||||||
|
preset_data[color_key], settings
|
||||||
|
)
|
||||||
|
except (TypeError, ValueError, KeyError):
|
||||||
|
continue
|
||||||
|
presets.edit(id, preset_data)
|
||||||
|
print(f"Edited preset {id}: {preset_data.get('name', '')}")
|
||||||
|
|
||||||
|
|
||||||
|
def apply_select(data, settings, presets):
|
||||||
|
select_map = data["select"]
|
||||||
|
device_name = settings["name"]
|
||||||
|
select_list = select_map.get(device_name, [])
|
||||||
|
if not select_list:
|
||||||
|
return
|
||||||
|
preset_name = select_list[0]
|
||||||
|
step = select_list[1] if len(select_list) > 1 else None
|
||||||
|
presets.select(preset_name, step=step)
|
||||||
|
|
||||||
|
|
||||||
|
def apply_clear_presets(data, presets):
|
||||||
|
clear_value = data.get("clear_presets")
|
||||||
|
if isinstance(clear_value, bool):
|
||||||
|
should_clear = clear_value
|
||||||
|
elif isinstance(clear_value, int):
|
||||||
|
should_clear = bool(clear_value)
|
||||||
|
elif isinstance(clear_value, str):
|
||||||
|
should_clear = clear_value.lower() in ("true", "1", "yes", "on")
|
||||||
|
else:
|
||||||
|
should_clear = False
|
||||||
|
if not should_clear:
|
||||||
|
return
|
||||||
|
presets.delete_all()
|
||||||
|
print("Cleared all presets.")
|
||||||
|
|
||||||
|
|
||||||
|
def apply_default(data, settings, presets):
|
||||||
|
targets = data.get("targets") or []
|
||||||
|
default_name = data["default"]
|
||||||
|
if (
|
||||||
|
settings["name"] in targets
|
||||||
|
and isinstance(default_name, str)
|
||||||
|
and default_name in presets.presets
|
||||||
|
):
|
||||||
|
settings["default"] = default_name
|
||||||
|
settings.save()
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_http_url(url):
|
||||||
|
"""Parse http://host[:port]/path into (host, port, path)."""
|
||||||
|
if not isinstance(url, str):
|
||||||
|
raise ValueError("url must be a string")
|
||||||
|
if not url.startswith("http://"):
|
||||||
|
raise ValueError("only http:// URLs are supported")
|
||||||
|
remainder = url[7:]
|
||||||
|
slash_idx = remainder.find("/")
|
||||||
|
if slash_idx == -1:
|
||||||
|
host_port = remainder
|
||||||
|
path = "/"
|
||||||
|
else:
|
||||||
|
host_port = remainder[:slash_idx]
|
||||||
|
path = remainder[slash_idx:]
|
||||||
|
if ":" in host_port:
|
||||||
|
host, port_s = host_port.rsplit(":", 1)
|
||||||
|
port = int(port_s)
|
||||||
|
else:
|
||||||
|
host = host_port
|
||||||
|
port = 80
|
||||||
|
if not host:
|
||||||
|
raise ValueError("missing host")
|
||||||
|
return host, port, path
|
||||||
|
|
||||||
|
|
||||||
|
def _http_get_raw(url, timeout_s=10.0):
|
||||||
|
host, port, path = _parse_http_url(url)
|
||||||
|
req = (
|
||||||
|
"GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n" % (path, host)
|
||||||
|
).encode("utf-8")
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
try:
|
||||||
|
sock.settimeout(timeout_s)
|
||||||
|
sock.connect((host, int(port)))
|
||||||
|
sock.send(req)
|
||||||
|
data = b""
|
||||||
|
while True:
|
||||||
|
chunk = sock.recv(1024)
|
||||||
|
if not chunk:
|
||||||
|
break
|
||||||
|
data += chunk
|
||||||
|
finally:
|
||||||
|
try:
|
||||||
|
sock.close()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
sep = b"\r\n\r\n"
|
||||||
|
if sep not in data:
|
||||||
|
raise OSError("invalid HTTP response")
|
||||||
|
head, body = data.split(sep, 1)
|
||||||
|
status_line = head.split(b"\r\n", 1)[0]
|
||||||
|
if b" 200 " not in status_line:
|
||||||
|
raise OSError("HTTP status not OK: %s" % status_line.decode("utf-8"))
|
||||||
|
return body
|
||||||
|
|
||||||
|
|
||||||
|
def _http_get_json(url, timeout_s=10.0):
|
||||||
|
body = _http_get_raw(url, timeout_s=timeout_s)
|
||||||
|
return json.loads(body.decode("utf-8"))
|
||||||
|
|
||||||
|
|
||||||
|
def _http_get_text(url, timeout_s=10.0, controller_ip=None):
|
||||||
|
# Support relative URLs from controller messages.
|
||||||
|
if isinstance(url, str) and url.startswith("/"):
|
||||||
|
if not controller_ip:
|
||||||
|
raise OSError("controller IP unavailable for relative URL")
|
||||||
|
url = "http://%s%s" % (controller_ip, url)
|
||||||
|
try:
|
||||||
|
body = _http_get_raw(url, timeout_s=timeout_s)
|
||||||
|
return body.decode("utf-8")
|
||||||
|
except Exception:
|
||||||
|
# Fallback for mDNS/unresolvable host: retry against current controller IP.
|
||||||
|
if not controller_ip or not isinstance(url, str) or not url.startswith("http://"):
|
||||||
|
raise
|
||||||
|
_host, _port, path = _parse_http_url(url)
|
||||||
|
fallback = "http://%s:%d%s" % (controller_ip, _port, path)
|
||||||
|
body = _http_get_raw(fallback, timeout_s=timeout_s)
|
||||||
|
return body.decode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
def _safe_pattern_filename(name):
|
||||||
|
if not isinstance(name, str):
|
||||||
|
return False
|
||||||
|
if not name.endswith(".py"):
|
||||||
|
return False
|
||||||
|
if "/" in name or "\\" in name or ".." in name:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def apply_patterns_ota(data, presets, controller_ip=None):
|
||||||
|
manifest_payload = data.get("manifest")
|
||||||
|
if not manifest_payload:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
if isinstance(manifest_payload, dict):
|
||||||
|
manifest = manifest_payload
|
||||||
|
elif isinstance(manifest_payload, str):
|
||||||
|
manifest = _http_get_json(manifest_payload, timeout_s=20.0)
|
||||||
|
else:
|
||||||
|
print("patterns_ota: invalid manifest payload type")
|
||||||
|
return
|
||||||
|
files = manifest.get("files", [])
|
||||||
|
if not isinstance(files, list) or not files:
|
||||||
|
print("patterns_ota: no files in manifest")
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
os.mkdir("patterns")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
updated = 0
|
||||||
|
for item in files:
|
||||||
|
if not isinstance(item, dict):
|
||||||
|
continue
|
||||||
|
name = item.get("name")
|
||||||
|
url = item.get("url")
|
||||||
|
inline_code = item.get("code")
|
||||||
|
if not _safe_pattern_filename(name):
|
||||||
|
continue
|
||||||
|
if isinstance(inline_code, str):
|
||||||
|
code = inline_code
|
||||||
|
elif isinstance(url, str):
|
||||||
|
code = _http_get_text(url, timeout_s=20.0, controller_ip=controller_ip)
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
with open("patterns/" + name, "w") as f:
|
||||||
|
f.write(code)
|
||||||
|
updated += 1
|
||||||
|
if updated > 0:
|
||||||
|
presets.reload_patterns()
|
||||||
|
print("patterns_ota: updated", updated, "pattern file(s)")
|
||||||
|
else:
|
||||||
|
print("patterns_ota: no valid files downloaded")
|
||||||
|
except Exception as e:
|
||||||
|
print("patterns_ota failed:", e)
|
||||||
191
src/hello.py
Normal file
191
src/hello.py
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
"""LED hello JSON line and UDP broadcast on port 8766.
|
||||||
|
|
||||||
|
Used so led-controller can register the device (name, MAC, IP) when ``wait_reply`` is
|
||||||
|
false; the controller may then connect to the device's WebSocket. With
|
||||||
|
``wait_reply`` true, blocks for an echo and returns the controller IP (legacy discovery).
|
||||||
|
|
||||||
|
Wi-Fi must already be connected; this module does not use Settings or call connect().
|
||||||
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import socket
|
||||||
|
import ubinascii
|
||||||
|
|
||||||
|
import network
|
||||||
|
|
||||||
|
# Match led-controller/tests/udp_server.py
|
||||||
|
DISCOVERY_UDP_PORT = 8766
|
||||||
|
DEFAULT_RECV_TIMEOUT_S = 3
|
||||||
|
|
||||||
|
|
||||||
|
def pack_hello_dict(sta, device_name=""):
|
||||||
|
"""Same fields as main HTTP/ESP-NOW hello."""
|
||||||
|
mac = sta.config("mac")
|
||||||
|
return {
|
||||||
|
"v": "1",
|
||||||
|
"device_name": device_name,
|
||||||
|
"mac": ubinascii.hexlify(mac).decode().lower(),
|
||||||
|
"type": "led",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def pack_hello_bytes(sta, device_name=""):
|
||||||
|
return json.dumps(pack_hello_dict(sta, device_name)).encode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
def pack_hello_line(sta, device_name=""):
|
||||||
|
"""JSON hello + newline (HTTP/UDP discovery payloads)."""
|
||||||
|
return pack_hello_bytes(sta, device_name) + b"\n"
|
||||||
|
|
||||||
|
|
||||||
|
def ipv4_broadcast(ip, netmask):
|
||||||
|
"""Directed broadcast (e.g. 192.168.1.0/24 -> 192.168.1.255)."""
|
||||||
|
ia = [int(x) for x in ip.split(".")]
|
||||||
|
im = [int(x) for x in netmask.split(".")]
|
||||||
|
if len(ia) != 4 or len(im) != 4:
|
||||||
|
return None
|
||||||
|
# STA often reports 255.255.255.255; "broadcast" would equal the host IP — useless for LAN.
|
||||||
|
if netmask == "255.255.255.255":
|
||||||
|
return None
|
||||||
|
bcast = ".".join(str(ia[i] | (255 - im[i])) for i in range(4))
|
||||||
|
if bcast == ip:
|
||||||
|
return None
|
||||||
|
return bcast
|
||||||
|
|
||||||
|
|
||||||
|
def udp_discovery_targets(ip, mask):
|
||||||
|
"""(directed_broadcast, port) then limited broadcast."""
|
||||||
|
out = [("255.255.255.255", DISCOVERY_UDP_PORT)]
|
||||||
|
b = ipv4_broadcast(ip, mask)
|
||||||
|
if b:
|
||||||
|
out.insert(0, (b, DISCOVERY_UDP_PORT))
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def _udp_discovery_targets_single(ip, mask):
|
||||||
|
"""One destination: subnet broadcast if known, else limited broadcast."""
|
||||||
|
b = ipv4_broadcast(ip, mask)
|
||||||
|
if b:
|
||||||
|
return [(b, DISCOVERY_UDP_PORT)]
|
||||||
|
return [("255.255.255.255", DISCOVERY_UDP_PORT)]
|
||||||
|
|
||||||
|
|
||||||
|
def broadcast_hello_udp(
|
||||||
|
sta,
|
||||||
|
device_name="",
|
||||||
|
*,
|
||||||
|
wait_reply=True,
|
||||||
|
recv_timeout_s=DEFAULT_RECV_TIMEOUT_S,
|
||||||
|
wdt=None,
|
||||||
|
dual_destinations=True,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Send pack_hello_line on DISCOVERY_UDP_PORT.
|
||||||
|
STA must already be connected with a valid IPv4 (caller brings up Wi-Fi).
|
||||||
|
|
||||||
|
If dual_destinations (default), send subnet broadcast then 255.255.255.255 so
|
||||||
|
discovery works on awkward APs — the controller may receive two packets.
|
||||||
|
If dual_destinations is False, send only one (subnet broadcast or limited),
|
||||||
|
e.g. after TCP connect so the Pi does not run duplicate resync handlers.
|
||||||
|
|
||||||
|
If wait_reply, wait for first UDP echo. Returns controller IP string or None.
|
||||||
|
"""
|
||||||
|
ip, mask, _gw, _dns = sta.ifconfig()
|
||||||
|
msg = pack_hello_line(sta, device_name)
|
||||||
|
print("hello:", msg)
|
||||||
|
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
try:
|
||||||
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||||
|
except (AttributeError, OSError) as e:
|
||||||
|
print("SO_BROADCAST not set:", e)
|
||||||
|
try:
|
||||||
|
sock.bind((ip, 0))
|
||||||
|
except (AttributeError, OSError, TypeError) as e:
|
||||||
|
try:
|
||||||
|
sock.bind(("0.0.0.0", 0))
|
||||||
|
except (AttributeError, OSError) as e2:
|
||||||
|
print("bind skipped:", e, e2)
|
||||||
|
if wait_reply:
|
||||||
|
try:
|
||||||
|
sock.settimeout(recv_timeout_s)
|
||||||
|
except (AttributeError, OSError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
discovered = None
|
||||||
|
targets = (
|
||||||
|
udp_discovery_targets(ip, mask)
|
||||||
|
if dual_destinations
|
||||||
|
else _udp_discovery_targets_single(ip, mask)
|
||||||
|
)
|
||||||
|
for dest_ip, dest_port in targets:
|
||||||
|
if wdt is not None:
|
||||||
|
wdt.feed()
|
||||||
|
label = "%s:%s" % (dest_ip, dest_port)
|
||||||
|
target = (dest_ip, dest_port)
|
||||||
|
try:
|
||||||
|
sock.sendto(msg, target)
|
||||||
|
print("sent hello ->", target)
|
||||||
|
except OSError as e:
|
||||||
|
print("sendto failed:", e)
|
||||||
|
continue
|
||||||
|
if not wait_reply:
|
||||||
|
continue
|
||||||
|
if wdt is not None:
|
||||||
|
wdt.feed()
|
||||||
|
try:
|
||||||
|
data, addr = sock.recvfrom(2048)
|
||||||
|
print("reply from", addr, ":", data)
|
||||||
|
remote_ip = addr[0]
|
||||||
|
if data != msg:
|
||||||
|
print("(warning: reply payload differs from hello; still using source IP.)")
|
||||||
|
discovered = remote_ip
|
||||||
|
print("Discovered controller at", remote_ip)
|
||||||
|
break
|
||||||
|
except OSError as e:
|
||||||
|
print("recv (no reply):", e, "via", label)
|
||||||
|
if dest_ip == "255.255.255.255":
|
||||||
|
print(
|
||||||
|
"(hint: many APs drop Wi-Fi client broadcast; try wired server or AP without client isolation.)"
|
||||||
|
)
|
||||||
|
|
||||||
|
sock.close()
|
||||||
|
return discovered
|
||||||
|
|
||||||
|
|
||||||
|
def discover_controller_udp(device_name="", wdt=None):
|
||||||
|
"""
|
||||||
|
Broadcast hello; return controller IP from first UDP echo, or None.
|
||||||
|
STA must already be connected.
|
||||||
|
|
||||||
|
device_name: logical name in the JSON (caller supplies, e.g. from Settings elsewhere).
|
||||||
|
wdt: optional WDT to feed during waits.
|
||||||
|
"""
|
||||||
|
sta = network.WLAN(network.STA_IF)
|
||||||
|
if not sta.isconnected():
|
||||||
|
print("hello: STA not connected — connect Wi-Fi before discovery.")
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
ip, mask, _g, _d = sta.ifconfig()
|
||||||
|
if ip == "0.0.0.0":
|
||||||
|
print("hello: STA has no IP address.")
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
print("STA IP:", ip, "mask:", mask)
|
||||||
|
|
||||||
|
discovered = broadcast_hello_udp(
|
||||||
|
sta,
|
||||||
|
device_name,
|
||||||
|
wait_reply=True,
|
||||||
|
wdt=wdt,
|
||||||
|
)
|
||||||
|
if discovered:
|
||||||
|
print("discover done; controller =", repr(discovered))
|
||||||
|
else:
|
||||||
|
print("discover done; controller not found")
|
||||||
|
return discovered
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if not discover_controller_udp():
|
||||||
|
raise SystemExit(1)
|
||||||
125
src/http_routes.py
Normal file
125
src/http_routes.py
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
from controller_messages import process_data
|
||||||
|
from microdot.websocket import WebSocketError, with_websocket
|
||||||
|
|
||||||
|
try:
|
||||||
|
import uos as os
|
||||||
|
except ImportError:
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def _safe_pattern_filename(name):
|
||||||
|
if not isinstance(name, str):
|
||||||
|
return False
|
||||||
|
if not name.endswith(".py"):
|
||||||
|
return False
|
||||||
|
if "/" in name or "\\" in name or ".." in name:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def register_routes(app, settings, presets, runtime_state):
|
||||||
|
@app.route("/ws")
|
||||||
|
@with_websocket
|
||||||
|
async def ws_handler(request, ws):
|
||||||
|
print("WS client connected")
|
||||||
|
runtime_state.ws_connected()
|
||||||
|
controller_ip = None
|
||||||
|
try:
|
||||||
|
client_addr = getattr(request, "client_addr", None)
|
||||||
|
if isinstance(client_addr, (tuple, list)) and client_addr:
|
||||||
|
controller_ip = client_addr[0]
|
||||||
|
elif isinstance(client_addr, str):
|
||||||
|
controller_ip = client_addr
|
||||||
|
except Exception:
|
||||||
|
controller_ip = None
|
||||||
|
print("WS controller_ip:", controller_ip)
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
data = await ws.receive()
|
||||||
|
if not data:
|
||||||
|
print("WS client disconnected (closed)")
|
||||||
|
break
|
||||||
|
print("WS recv bytes:", len(data) if isinstance(data, (bytes, bytearray)) else len(str(data)))
|
||||||
|
print(data)
|
||||||
|
process_data(data, settings, presets, controller_ip=controller_ip)
|
||||||
|
except WebSocketError as e:
|
||||||
|
print("WS client disconnected:", e)
|
||||||
|
except OSError as e:
|
||||||
|
print("WS client dropped (OSError):", e)
|
||||||
|
finally:
|
||||||
|
runtime_state.ws_disconnected()
|
||||||
|
print(
|
||||||
|
"WS client disconnected: hello=",
|
||||||
|
runtime_state.hello,
|
||||||
|
"ws_client_count=",
|
||||||
|
runtime_state.ws_client_count,
|
||||||
|
)
|
||||||
|
|
||||||
|
@app.post("/patterns/upload")
|
||||||
|
async def upload_pattern(request):
|
||||||
|
"""Receive one pattern file body from led-controller and reload patterns."""
|
||||||
|
raw_name = request.args.get("name")
|
||||||
|
reload_raw = request.args.get("reload", "1")
|
||||||
|
reload_patterns = str(reload_raw).strip().lower() not in ("0", "false", "no", "off")
|
||||||
|
print("patterns/upload request:", {"name": raw_name, "reload": reload_patterns})
|
||||||
|
|
||||||
|
if not isinstance(raw_name, str) or not raw_name.strip():
|
||||||
|
return json.dumps({"error": "name is required"}), 400, {"Content-Type": "application/json"}
|
||||||
|
body = request.body
|
||||||
|
if not isinstance(body, (bytes, bytearray)) or not body:
|
||||||
|
print("patterns/upload rejected: empty body")
|
||||||
|
return json.dumps({"error": "code is required"}), 400, {"Content-Type": "application/json"}
|
||||||
|
print("patterns/upload body_bytes:", len(body))
|
||||||
|
try:
|
||||||
|
code = body.decode("utf-8")
|
||||||
|
except UnicodeError:
|
||||||
|
print("patterns/upload rejected: body not utf-8")
|
||||||
|
return json.dumps({"error": "body must be utf-8 text"}), 400, {"Content-Type": "application/json"}
|
||||||
|
if not code.strip():
|
||||||
|
return json.dumps({"error": "code is required"}), 400, {"Content-Type": "application/json"}
|
||||||
|
|
||||||
|
name = raw_name.strip()
|
||||||
|
if not name.endswith(".py"):
|
||||||
|
name += ".py"
|
||||||
|
if not _safe_pattern_filename(name) or name in ("__init__.py", "main.py"):
|
||||||
|
return json.dumps({"error": "invalid pattern filename"}), 400, {"Content-Type": "application/json"}
|
||||||
|
|
||||||
|
try:
|
||||||
|
os.mkdir("patterns")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
path = "patterns/" + name
|
||||||
|
try:
|
||||||
|
print("patterns/upload writing:", path)
|
||||||
|
with open(path, "w") as f:
|
||||||
|
f.write(code)
|
||||||
|
if reload_patterns:
|
||||||
|
print("patterns/upload reloading patterns")
|
||||||
|
presets.reload_patterns()
|
||||||
|
except OSError as e:
|
||||||
|
print("patterns/upload failed:", e)
|
||||||
|
return json.dumps({"error": str(e)}), 500, {"Content-Type": "application/json"}
|
||||||
|
print("patterns/upload success:", {"name": name, "reloaded": reload_patterns})
|
||||||
|
|
||||||
|
return json.dumps(
|
||||||
|
{
|
||||||
|
"message": "pattern uploaded",
|
||||||
|
"name": name,
|
||||||
|
"reloaded": reload_patterns,
|
||||||
|
}
|
||||||
|
), 201, {"Content-Type": "application/json"}
|
||||||
|
|
||||||
|
@app.post("/presets/upload")
|
||||||
|
async def upload_presets(request):
|
||||||
|
"""Receive v1 JSON with ``presets`` and apply/save on the driver."""
|
||||||
|
body = request.body
|
||||||
|
if not isinstance(body, (bytes, bytearray)) or not body:
|
||||||
|
return json.dumps({"error": "body is required"}), 400, {"Content-Type": "application/json"}
|
||||||
|
try:
|
||||||
|
process_data(body, settings, presets)
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)}), 400, {"Content-Type": "application/json"}
|
||||||
|
return json.dumps({"message": "presets applied"}), 200, {"Content-Type": "application/json"}
|
||||||
252
src/main.py
252
src/main.py
@@ -1,75 +1,209 @@
|
|||||||
from settings import Settings
|
from settings import Settings
|
||||||
from machine import WDT
|
import machine
|
||||||
from espnow import ESPNow
|
|
||||||
import utime
|
|
||||||
import network
|
import network
|
||||||
from presets import Presets
|
import utime
|
||||||
from utils import convert_and_reorder_colors
|
import asyncio
|
||||||
import json
|
import json
|
||||||
|
import gc
|
||||||
|
from microdot import Microdot
|
||||||
|
from microdot.websocket import WebSocketError, with_websocket
|
||||||
|
from presets import Presets
|
||||||
|
from controller_messages import process_data
|
||||||
|
from hello import broadcast_hello_udp
|
||||||
|
try:
|
||||||
|
import uos as os
|
||||||
|
except ImportError:
|
||||||
|
import os
|
||||||
|
|
||||||
|
machine.freq(160000000)
|
||||||
|
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings()
|
||||||
print(settings)
|
print(settings)
|
||||||
|
|
||||||
|
wdt = machine.WDT(timeout=10000)
|
||||||
|
wdt.feed()
|
||||||
|
|
||||||
|
gc.collect()
|
||||||
|
print("mem before presets:", {"free": gc.mem_free(), "alloc": gc.mem_alloc()})
|
||||||
|
|
||||||
presets = Presets(settings["led_pin"], settings["num_leds"])
|
presets = Presets(settings["led_pin"], settings["num_leds"])
|
||||||
presets.load()
|
presets.load(settings)
|
||||||
presets.b = settings.get("brightness", 255)
|
presets.b = settings.get("brightness", 255)
|
||||||
# Use the default preset name from settings (set via controller or defaults)
|
presets.debug = bool(settings.get("debug", False))
|
||||||
default_preset = settings.get("default")
|
gc.collect()
|
||||||
if default_preset:
|
print("mem after presets:", {"free": gc.mem_free(), "alloc": gc.mem_alloc()})
|
||||||
presets.select(default_preset)
|
|
||||||
|
default_preset = settings.get("default", "")
|
||||||
|
if default_preset and default_preset in presets.presets:
|
||||||
|
if presets.select(default_preset):
|
||||||
print(f"Selected startup preset: {default_preset}")
|
print(f"Selected startup preset: {default_preset}")
|
||||||
|
else:
|
||||||
|
print("Startup preset failed (invalid pattern?):", default_preset)
|
||||||
|
|
||||||
wdt = WDT(timeout=10000)
|
# On ESP32-C3, soft reboots can leave Wi-Fi driver state allocated.
|
||||||
wdt.feed()
|
# Reset both interfaces and collect before bringing STA up.
|
||||||
last_brightness_save = 0
|
ap_if = network.WLAN(network.AP_IF)
|
||||||
|
ap_if.active(False)
|
||||||
sta_if = network.WLAN(network.STA_IF)
|
sta_if = network.WLAN(network.STA_IF)
|
||||||
|
if sta_if.active():
|
||||||
|
sta_if.active(False)
|
||||||
|
utime.sleep_ms(100)
|
||||||
|
gc.collect()
|
||||||
sta_if.active(True)
|
sta_if.active(True)
|
||||||
sta_if.disconnect()
|
sta_if.config(pm=network.WLAN.PM_NONE)
|
||||||
sta_if.config(channel=1)
|
sta_if.connect(settings["ssid"], settings["password"])
|
||||||
e = ESPNow()
|
while not sta_if.isconnected():
|
||||||
e.active(True)
|
print("Connecting")
|
||||||
|
utime.sleep(1)
|
||||||
|
|
||||||
while True:
|
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
presets.tick()
|
|
||||||
if e.any():
|
print(sta_if.ifconfig())
|
||||||
host, msg = e.recv()
|
|
||||||
print(msg)
|
app = Microdot()
|
||||||
data = json.loads(msg)
|
|
||||||
# Only handle messages with the expected version.
|
|
||||||
if data.get("v") != "1":
|
def _safe_pattern_filename(name):
|
||||||
continue
|
if not isinstance(name, str):
|
||||||
# print(data)
|
return False
|
||||||
# Global brightness (0–255) for this device
|
if not name.endswith(".py"):
|
||||||
if "b" in data:
|
return False
|
||||||
|
if "/" in name or "\\" in name or ".." in name:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/ws")
|
||||||
|
@with_websocket
|
||||||
|
async def ws_handler(request, ws):
|
||||||
|
print("WS client connected")
|
||||||
|
controller_ip = None
|
||||||
try:
|
try:
|
||||||
presets.b = max(0, min(255, int(data["b"])))
|
client_addr = getattr(request, "client_addr", None)
|
||||||
settings["brightness"] = presets.b
|
if isinstance(client_addr, (tuple, list)) and client_addr:
|
||||||
now = utime.ticks_ms()
|
controller_ip = client_addr[0]
|
||||||
if utime.ticks_diff(now, last_brightness_save) >= 500:
|
elif isinstance(client_addr, str):
|
||||||
settings.save()
|
controller_ip = client_addr
|
||||||
last_brightness_save = now
|
except Exception:
|
||||||
except (TypeError, ValueError):
|
controller_ip = None
|
||||||
|
print("WS controller_ip:", controller_ip)
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
data = await ws.receive()
|
||||||
|
if not data:
|
||||||
|
print("WS client disconnected (closed)")
|
||||||
|
break
|
||||||
|
print("WS recv bytes:", len(data) if isinstance(data, (bytes, bytearray)) else len(str(data)))
|
||||||
|
print(data)
|
||||||
|
process_data(data, settings, presets, controller_ip=controller_ip)
|
||||||
|
except WebSocketError as e:
|
||||||
|
print("WS client disconnected:", e)
|
||||||
|
except OSError as e:
|
||||||
|
print("WS client dropped (OSError):", e)
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/patterns/upload")
|
||||||
|
async def upload_pattern(request):
|
||||||
|
"""Receive one pattern file body from led-controller and reload patterns."""
|
||||||
|
raw_name = request.args.get("name")
|
||||||
|
reload_raw = request.args.get("reload", "1")
|
||||||
|
reload_patterns = str(reload_raw).strip().lower() not in ("0", "false", "no", "off")
|
||||||
|
print("patterns/upload request:", {"name": raw_name, "reload": reload_patterns})
|
||||||
|
|
||||||
|
if not isinstance(raw_name, str) or not raw_name.strip():
|
||||||
|
return json.dumps({"error": "name is required"}), 400, {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
body = request.body
|
||||||
|
if not isinstance(body, (bytes, bytearray)) or not body:
|
||||||
|
print("patterns/upload rejected: empty body")
|
||||||
|
return json.dumps({"error": "code is required"}), 400, {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
print("patterns/upload body_bytes:", len(body))
|
||||||
|
try:
|
||||||
|
code = body.decode("utf-8")
|
||||||
|
except UnicodeError:
|
||||||
|
print("patterns/upload rejected: body not utf-8")
|
||||||
|
return json.dumps({"error": "body must be utf-8 text"}), 400, {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
if not code.strip():
|
||||||
|
return json.dumps({"error": "code is required"}), 400, {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
|
||||||
|
name = raw_name.strip()
|
||||||
|
if not name.endswith(".py"):
|
||||||
|
name += ".py"
|
||||||
|
if not _safe_pattern_filename(name) or name in ("__init__.py", "main.py"):
|
||||||
|
return json.dumps({"error": "invalid pattern filename"}), 400, {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
os.mkdir("patterns")
|
||||||
|
except OSError:
|
||||||
pass
|
pass
|
||||||
if "presets" in data:
|
|
||||||
for id, preset_data in data["presets"].items():
|
path = "patterns/" + name
|
||||||
# Convert hex color strings to RGB tuples and reorder based on device color order
|
try:
|
||||||
if "c" in preset_data:
|
print("patterns/upload writing:", path)
|
||||||
preset_data["c"] = convert_and_reorder_colors(preset_data["c"], settings)
|
with open(path, "w") as f:
|
||||||
presets.edit(id, preset_data)
|
f.write(code)
|
||||||
print(f"Edited preset {id}: {preset_data.get('name', '')}")
|
if reload_patterns:
|
||||||
if settings.get("name") in data.get("select", {}):
|
print("patterns/upload reloading patterns")
|
||||||
select_list = data["select"][settings.get("name")]
|
presets.reload_patterns()
|
||||||
# Select value is always a list: ["preset_name"] or ["preset_name", step]
|
except OSError as e:
|
||||||
if select_list:
|
print("patterns/upload failed:", e)
|
||||||
preset_name = select_list[0]
|
return json.dumps({"error": str(e)}), 500, {
|
||||||
step = select_list[1] if len(select_list) > 1 else None
|
"Content-Type": "application/json"
|
||||||
presets.select(preset_name, step=step)
|
}
|
||||||
if "default" in data:
|
print("patterns/upload success:", {"name": name, "reloaded": reload_patterns})
|
||||||
settings["default"] = data["default"]
|
|
||||||
print(f"Set startup preset to: {data['default']}")
|
return json.dumps({
|
||||||
settings.save()
|
"message": "pattern uploaded",
|
||||||
if "save" in data:
|
"name": name,
|
||||||
presets.save()
|
"reloaded": reload_patterns,
|
||||||
|
}), 201, {"Content-Type": "application/json"}
|
||||||
|
|
||||||
|
|
||||||
|
async def presets_loop():
|
||||||
|
last_mem_log = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
presets.tick()
|
||||||
|
wdt.feed()
|
||||||
|
if bool(getattr(presets, "debug", False)):
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last_mem_log) >= 5000:
|
||||||
|
gc.collect()
|
||||||
|
print("mem runtime:", {"free": gc.mem_free(), "alloc": gc.mem_alloc()})
|
||||||
|
last_mem_log = now
|
||||||
|
# tick() does not await; yield so UDP hello and HTTP/WebSocket can run.
|
||||||
|
await asyncio.sleep(0)
|
||||||
|
|
||||||
|
|
||||||
|
async def _udp_hello_after_http_ready():
|
||||||
|
"""Hello must run after the HTTP server binds, or discovery clients time out on /ws."""
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
print("UDP hello: broadcasting…")
|
||||||
|
try:
|
||||||
|
broadcast_hello_udp(
|
||||||
|
sta_if,
|
||||||
|
settings.get("name", ""),
|
||||||
|
wait_reply=False,
|
||||||
|
wdt=wdt,
|
||||||
|
dual_destinations=True,
|
||||||
|
)
|
||||||
|
except Exception as ex:
|
||||||
|
print("UDP hello broadcast failed:", ex)
|
||||||
|
|
||||||
|
|
||||||
|
async def main(port=80):
|
||||||
|
asyncio.create_task(presets_loop())
|
||||||
|
asyncio.create_task(_udp_hello_after_http_ready())
|
||||||
|
await app.start_server(host="0.0.0.0", port=port)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main(port=80))
|
||||||
|
|||||||
16
src/p2p.py
16
src/p2p.py
@@ -1,16 +0,0 @@
|
|||||||
import asyncio
|
|
||||||
import aioespnow
|
|
||||||
import json
|
|
||||||
|
|
||||||
async def p2p(settings, patterns):
|
|
||||||
e = aioespnow.AIOESPNow() # Returns AIOESPNow enhanced with async support
|
|
||||||
e.active(True)
|
|
||||||
async for mac, msg in e:
|
|
||||||
try:
|
|
||||||
data = json.loads(msg)
|
|
||||||
except:
|
|
||||||
print(f"Failed to load espnow data {msg}")
|
|
||||||
continue
|
|
||||||
|
|
||||||
if "names" not in data or settings.get("name") in data.get("names", []):
|
|
||||||
await settings.set_settings(data.get("settings", {}), patterns, data.get("save", False))
|
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
from .blink import Blink
|
"""Pattern modules are registered only via Presets._load_dynamic_patterns().
|
||||||
from .rainbow import Rainbow
|
|
||||||
from .pulse import Pulse
|
This file is ignored as a pattern (see presets.py). Keep it free of imports so
|
||||||
from .transition import Transition
|
adding a pattern does not require editing this package.
|
||||||
from .chase import Chase
|
"""
|
||||||
from .circle import Circle
|
|
||||||
|
|||||||
31
src/patterns/aurora.py
Normal file
31
src/patterns/aurora.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class Aurora:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
colors = preset.c if preset.c else [(40, 200, 140), (80, 120, 255), (160, 80, 220)]
|
||||||
|
bands = max(1, int(preset.n1) if int(preset.n1) > 0 else 3)
|
||||||
|
shimmer = max(0, min(255, int(preset.n2) if int(preset.n2) > 0 else 40))
|
||||||
|
phase = self.driver.step % 256
|
||||||
|
last = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
d = max(1, int(preset.d))
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
idx = ((i * bands) // max(1, self.driver.num_leds) + (phase // 32)) % len(colors)
|
||||||
|
c = self.driver.apply_brightness(colors[idx], preset.b)
|
||||||
|
w = (255 - abs(128 - ((i * 8 + phase) & 255)) * 2)
|
||||||
|
w = max(0, min(255, w + shimmer))
|
||||||
|
self.driver.n[i] = ((c[0]*w)//255, (c[1]*w)//255, (c[2]*w)//255)
|
||||||
|
self.driver.n.write()
|
||||||
|
phase = (phase + 1) & 255
|
||||||
|
self.driver.step = phase
|
||||||
|
last = utime.ticks_add(last, d)
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
yield
|
||||||
29
src/patterns/bar_graph.py
Normal file
29
src/patterns/bar_graph.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class BarGraph:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
colors = preset.c if preset.c else [(0, 255, 0), (255, 80, 0)]
|
||||||
|
last_update = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
delay_ms = max(1, int(preset.d))
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last_update) >= delay_ms:
|
||||||
|
level = max(0, min(100, int(preset.n1) if int(preset.n1) >= 0 else 50))
|
||||||
|
target = (self.driver.num_leds * level) // 100
|
||||||
|
lit = self.driver.apply_brightness(colors[0], preset.b)
|
||||||
|
unlit = self.driver.apply_brightness(
|
||||||
|
colors[-1],
|
||||||
|
preset.b,
|
||||||
|
)
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
self.driver.n[i] = lit if i < target else unlit
|
||||||
|
self.driver.n.write()
|
||||||
|
last_update = utime.ticks_add(last_update, delay_ms)
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
yield
|
||||||
@@ -25,9 +25,9 @@ class Blink:
|
|||||||
# Advance to next color for the next "on" phase
|
# Advance to next color for the next "on" phase
|
||||||
color_index += 1
|
color_index += 1
|
||||||
else:
|
else:
|
||||||
# "Off" phase: turn all LEDs off
|
# "Off" phase should actually be off.
|
||||||
self.driver.fill((0, 0, 0))
|
self.driver.fill((0, 0, 0))
|
||||||
state = not state
|
state = not state
|
||||||
last_update = current_time
|
last_update = utime.ticks_add(last_update, delay_ms)
|
||||||
# Yield once per tick so other logic can run
|
# Yield once per tick so other logic can run
|
||||||
yield
|
yield
|
||||||
|
|||||||
40
src/patterns/breathing_dual.py
Normal file
40
src/patterns/breathing_dual.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class BreathingDual:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
colors = preset.c if preset.c else [(255, 0, 140), (0, 120, 255)]
|
||||||
|
phase_offset = max(0, min(255, int(preset.n1)))
|
||||||
|
ease = max(1, int(preset.n2) if int(preset.n2) > 0 else 1)
|
||||||
|
phase = self.driver.step % 256
|
||||||
|
last = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
d = max(1, int(preset.d))
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
p1 = phase
|
||||||
|
p2 = (phase + phase_offset) & 255
|
||||||
|
t1 = 255 - abs(128 - p1) * 2
|
||||||
|
t2 = 255 - abs(128 - p2) * 2
|
||||||
|
if ease > 1:
|
||||||
|
t1 = (t1 * t1) // 255
|
||||||
|
t2 = (t2 * t2) // 255
|
||||||
|
c1 = self.driver.apply_brightness(colors[0], preset.b)
|
||||||
|
c2 = self.driver.apply_brightness(colors[1 % len(colors)] if len(colors) > 1 else colors[0], preset.b)
|
||||||
|
half = self.driver.num_leds // 2
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
if i < half:
|
||||||
|
self.driver.n[i] = ((c1[0]*t1)//255, (c1[1]*t1)//255, (c1[2]*t1)//255)
|
||||||
|
else:
|
||||||
|
self.driver.n[i] = ((c2[0]*t2)//255, (c2[1]*t2)//255, (c2[2]*t2)//255)
|
||||||
|
self.driver.n.write()
|
||||||
|
phase = (phase + 2) & 255
|
||||||
|
self.driver.step = phase
|
||||||
|
last = utime.ticks_add(last, d)
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
yield
|
||||||
@@ -26,6 +26,7 @@ class Chase:
|
|||||||
|
|
||||||
color0 = self.driver.apply_brightness(color0, preset.b)
|
color0 = self.driver.apply_brightness(color0, preset.b)
|
||||||
color1 = self.driver.apply_brightness(color1, preset.b)
|
color1 = self.driver.apply_brightness(color1, preset.b)
|
||||||
|
bg_color = self.driver.apply_brightness(colors[-1], preset.b)
|
||||||
|
|
||||||
n1 = max(1, int(preset.n1)) # LEDs of color 0
|
n1 = max(1, int(preset.n1)) # LEDs of color 0
|
||||||
n2 = max(1, int(preset.n2)) # LEDs of color 1
|
n2 = max(1, int(preset.n2)) # LEDs of color 1
|
||||||
@@ -53,7 +54,7 @@ class Chase:
|
|||||||
# If auto is False, run a single step and then stop
|
# If auto is False, run a single step and then stop
|
||||||
if not preset.a:
|
if not preset.a:
|
||||||
# Clear all LEDs
|
# Clear all LEDs
|
||||||
self.driver.n.fill((0, 0, 0))
|
self.driver.n.fill(bg_color)
|
||||||
|
|
||||||
# Draw repeating pattern starting at position
|
# Draw repeating pattern starting at position
|
||||||
for i in range(self.driver.num_leds):
|
for i in range(self.driver.num_leds):
|
||||||
@@ -98,7 +99,7 @@ class Chase:
|
|||||||
position += max_pos
|
position += max_pos
|
||||||
|
|
||||||
# Clear all LEDs
|
# Clear all LEDs
|
||||||
self.driver.n.fill((0, 0, 0))
|
self.driver.n.fill(bg_color)
|
||||||
|
|
||||||
# Draw repeating pattern starting at position
|
# Draw repeating pattern starting at position
|
||||||
for i in range(self.driver.num_leds):
|
for i in range(self.driver.num_leds):
|
||||||
@@ -118,7 +119,8 @@ class Chase:
|
|||||||
# Increment step
|
# Increment step
|
||||||
step_count += 1
|
step_count += 1
|
||||||
self.driver.step = step_count
|
self.driver.step = step_count
|
||||||
last_update = current_time
|
last_update = utime.ticks_add(last_update, transition_duration)
|
||||||
|
transition_duration = max(10, int(preset.d))
|
||||||
|
|
||||||
# Yield once per tick so other logic can run
|
# Yield once per tick so other logic can run
|
||||||
yield
|
yield
|
||||||
|
|||||||
@@ -31,10 +31,10 @@ class Circle:
|
|||||||
base0 = base1 = (255, 255, 255)
|
base0 = base1 = (255, 255, 255)
|
||||||
elif len(colors) == 1:
|
elif len(colors) == 1:
|
||||||
base0 = colors[0]
|
base0 = colors[0]
|
||||||
base1 = (0, 0, 0)
|
base1 = colors[-1]
|
||||||
else:
|
else:
|
||||||
base0 = colors[0]
|
base0 = colors[0]
|
||||||
base1 = colors[1]
|
base1 = colors[-1]
|
||||||
|
|
||||||
color0 = self.driver.apply_brightness(base0, preset.b)
|
color0 = self.driver.apply_brightness(base0, preset.b)
|
||||||
color1 = self.driver.apply_brightness(base1, preset.b)
|
color1 = self.driver.apply_brightness(base1, preset.b)
|
||||||
@@ -46,7 +46,7 @@ class Circle:
|
|||||||
if phase == "off":
|
if phase == "off":
|
||||||
self.driver.n.fill(color1)
|
self.driver.n.fill(color1)
|
||||||
else:
|
else:
|
||||||
self.driver.n.fill((0, 0, 0))
|
self.driver.n.fill(color1)
|
||||||
|
|
||||||
# Calculate segment length
|
# Calculate segment length
|
||||||
segment_length = (head - tail) % self.driver.num_leds
|
segment_length = (head - tail) % self.driver.num_leds
|
||||||
@@ -62,7 +62,9 @@ class Circle:
|
|||||||
# Move head continuously at n1 LEDs per second
|
# Move head continuously at n1 LEDs per second
|
||||||
if utime.ticks_diff(current_time, last_head_move) >= head_delay:
|
if utime.ticks_diff(current_time, last_head_move) >= head_delay:
|
||||||
head = (head + 1) % self.driver.num_leds
|
head = (head + 1) % self.driver.num_leds
|
||||||
last_head_move = current_time
|
last_head_move = utime.ticks_add(last_head_move, head_delay)
|
||||||
|
head_rate = max(1, int(preset.n1))
|
||||||
|
head_delay = 1000 // head_rate
|
||||||
|
|
||||||
# Tail behavior based on phase
|
# Tail behavior based on phase
|
||||||
if phase == "growing":
|
if phase == "growing":
|
||||||
@@ -73,7 +75,9 @@ class Circle:
|
|||||||
# Shrinking phase: move tail forward at n3 LEDs per second
|
# Shrinking phase: move tail forward at n3 LEDs per second
|
||||||
if utime.ticks_diff(current_time, last_tail_move) >= tail_delay:
|
if utime.ticks_diff(current_time, last_tail_move) >= tail_delay:
|
||||||
tail = (tail + 1) % self.driver.num_leds
|
tail = (tail + 1) % self.driver.num_leds
|
||||||
last_tail_move = current_time
|
last_tail_move = utime.ticks_add(last_tail_move, tail_delay)
|
||||||
|
tail_rate = max(1, int(preset.n3))
|
||||||
|
tail_delay = 1000 // tail_rate
|
||||||
|
|
||||||
# Check if we've reached min length
|
# Check if we've reached min length
|
||||||
current_length = (head - tail) % self.driver.num_leds
|
current_length = (head - tail) % self.driver.num_leds
|
||||||
|
|||||||
33
src/patterns/clock_sweep.py
Normal file
33
src/patterns/clock_sweep.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class ClockSweep:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
colors = preset.c if preset.c else [(255, 255, 255), (60, 60, 60)]
|
||||||
|
width = max(1, int(preset.n1) if int(preset.n1) > 0 else 1)
|
||||||
|
marker = max(0, int(preset.n2) if int(preset.n2) > 0 else 0)
|
||||||
|
pos = self.driver.step % max(1, self.driver.num_leds)
|
||||||
|
last = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
d = max(1, int(preset.d))
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
bg = self.driver.apply_brightness(colors[-1], preset.b)
|
||||||
|
fg = self.driver.apply_brightness(colors[0], preset.b)
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
self.driver.n[i] = bg
|
||||||
|
if marker > 0 and i % marker == 0:
|
||||||
|
self.driver.n[i] = ((bg[0]*2)//3, (bg[1]*2)//3, (bg[2]*2)//3)
|
||||||
|
for w in range(width):
|
||||||
|
self.driver.n[(pos + w) % self.driver.num_leds] = fg
|
||||||
|
self.driver.n.write()
|
||||||
|
pos = (pos + 1) % max(1, self.driver.num_leds)
|
||||||
|
self.driver.step = pos
|
||||||
|
last = utime.ticks_add(last, d)
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
yield
|
||||||
56
src/patterns/colour_cycle.py
Normal file
56
src/patterns/colour_cycle.py
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class ColourCycle:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def _render(self, colors, phase, brightness):
|
||||||
|
num_leds = self.driver.num_leds
|
||||||
|
color_count = len(colors)
|
||||||
|
if num_leds <= 0 or color_count <= 0:
|
||||||
|
return
|
||||||
|
if color_count == 1:
|
||||||
|
self.driver.fill(self.driver.apply_brightness(colors[0], brightness))
|
||||||
|
return
|
||||||
|
|
||||||
|
full_span = color_count * 256
|
||||||
|
# Match rainbow behaviour: phase is 0..255 and maps to one full-strip shift.
|
||||||
|
phase_shift = (phase * full_span) // 256
|
||||||
|
for i in range(num_leds):
|
||||||
|
# Position around the colour loop, shifted by phase.
|
||||||
|
pos = ((i * full_span) // num_leds + phase_shift) % full_span
|
||||||
|
idx = pos // 256
|
||||||
|
frac = pos & 255
|
||||||
|
|
||||||
|
c1 = colors[idx]
|
||||||
|
c2 = colors[(idx + 1) % color_count]
|
||||||
|
blended = (
|
||||||
|
c1[0] + ((c2[0] - c1[0]) * frac) // 256,
|
||||||
|
c1[1] + ((c2[1] - c1[1]) * frac) // 256,
|
||||||
|
c1[2] + ((c2[2] - c1[2]) * frac) // 256,
|
||||||
|
)
|
||||||
|
self.driver.n[i] = self.driver.apply_brightness(blended, brightness)
|
||||||
|
self.driver.n.write()
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
colors = preset.c if preset.c else [(255, 255, 255)]
|
||||||
|
phase = self.driver.step % 256
|
||||||
|
step_amount = max(1, int(preset.n1))
|
||||||
|
|
||||||
|
if not preset.a:
|
||||||
|
self._render(colors, phase, preset.b)
|
||||||
|
self.driver.step = (phase + step_amount) % 256
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
|
||||||
|
last_update = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
current_time = utime.ticks_ms()
|
||||||
|
delay_ms = max(1, int(preset.d))
|
||||||
|
if utime.ticks_diff(current_time, last_update) >= delay_ms:
|
||||||
|
self._render(colors, phase, preset.b)
|
||||||
|
phase = (phase + step_amount) % 256
|
||||||
|
self.driver.step = phase
|
||||||
|
last_update = utime.ticks_add(last_update, delay_ms)
|
||||||
|
yield
|
||||||
44
src/patterns/comet_dual.py
Normal file
44
src/patterns/comet_dual.py
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class CometDual:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
colors = preset.c if preset.c else [(255, 255, 255)]
|
||||||
|
tail = max(1, int(preset.n1) if int(preset.n1) > 0 else 6)
|
||||||
|
speed = max(1, int(preset.n2) if int(preset.n2) > 0 else 1)
|
||||||
|
gap = max(0, int(preset.n3))
|
||||||
|
p1 = 0
|
||||||
|
p2 = self.driver.num_leds - 1 - gap
|
||||||
|
last = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
d = max(1, int(preset.d))
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
bg_color = self.driver.apply_brightness(colors[-1], preset.b)
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
self.driver.n[i] = bg_color
|
||||||
|
c1 = self.driver.apply_brightness(colors[0 % len(colors)], preset.b)
|
||||||
|
c2 = self.driver.apply_brightness(colors[1 % len(colors)] if len(colors) > 1 else colors[0], preset.b)
|
||||||
|
for t in range(tail):
|
||||||
|
i1 = p1 - t
|
||||||
|
if 0 <= i1 < self.driver.num_leds:
|
||||||
|
s = (255 * (tail - t)) // max(1, tail)
|
||||||
|
self.driver.n[i1] = ((c1[0]*s)//255, (c1[1]*s)//255, (c1[2]*s)//255)
|
||||||
|
i2 = p2 + t
|
||||||
|
if 0 <= i2 < self.driver.num_leds:
|
||||||
|
s = (255 * (tail - t)) // max(1, tail)
|
||||||
|
self.driver.n[i2] = ((c2[0]*s)//255, (c2[1]*s)//255, (c2[2]*s)//255)
|
||||||
|
self.driver.n.write()
|
||||||
|
p1 += speed
|
||||||
|
p2 -= speed
|
||||||
|
if p1 - tail > self.driver.num_leds and p2 + tail < 0:
|
||||||
|
p1 = 0
|
||||||
|
p2 = self.driver.num_leds - 1 - gap
|
||||||
|
last = utime.ticks_add(last, d)
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
yield
|
||||||
35
src/patterns/fireflies.py
Normal file
35
src/patterns/fireflies.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import random
|
||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class Fireflies:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
colors = preset.c if preset.c else [(255, 210, 80), (120, 255, 120)]
|
||||||
|
count = max(1, int(preset.n1) if int(preset.n1) > 0 else 6)
|
||||||
|
speed = max(1, int(preset.n2) if int(preset.n2) > 0 else 8)
|
||||||
|
bugs = [[random.randint(0, max(0, self.driver.num_leds - 1)), random.randint(0, 255)] for _ in range(count)]
|
||||||
|
last = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
d = max(1, int(preset.d))
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
bg_color = self.driver.apply_brightness(colors[-1], preset.b)
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
self.driver.n[i] = bg_color
|
||||||
|
for b in bugs:
|
||||||
|
idx, ph = b
|
||||||
|
tri = 255 - abs(128 - ph) * 2
|
||||||
|
c = self.driver.apply_brightness(colors[idx % len(colors)], preset.b)
|
||||||
|
self.driver.n[idx] = ((c[0]*tri)//255, (c[1]*tri)//255, (c[2]*tri)//255)
|
||||||
|
b[1] = (ph + speed) & 255
|
||||||
|
if random.randint(0, 31) == 0:
|
||||||
|
b[0] = random.randint(0, max(0, self.driver.num_leds - 1))
|
||||||
|
self.driver.n.write()
|
||||||
|
last = utime.ticks_add(last, d)
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
yield
|
||||||
210
src/patterns/flame.py
Normal file
210
src/patterns/flame.py
Normal file
@@ -0,0 +1,210 @@
|
|||||||
|
import random
|
||||||
|
import utime
|
||||||
|
|
||||||
|
# Default warm palette: ember → orange → yellow → pale hot (RGB)
|
||||||
|
_DEFAULT_PALETTE = (
|
||||||
|
(90, 8, 8),
|
||||||
|
(200, 40, 12),
|
||||||
|
(255, 120, 30),
|
||||||
|
(255, 220, 140),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _clamp(x, lo, hi):
|
||||||
|
if x < lo:
|
||||||
|
return lo
|
||||||
|
if x > hi:
|
||||||
|
return hi
|
||||||
|
return x
|
||||||
|
|
||||||
|
|
||||||
|
def _lerp_chan(a, b, t):
|
||||||
|
return a + ((b - a) * t >> 8)
|
||||||
|
|
||||||
|
|
||||||
|
def _lerp_rgb(c0, c1, t):
|
||||||
|
return (
|
||||||
|
_lerp_chan(c0[0], c1[0], t),
|
||||||
|
_lerp_chan(c0[1], c1[1], t),
|
||||||
|
_lerp_chan(c0[2], c1[2], t),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _palette_sample(palette, pos256):
|
||||||
|
n = len(palette)
|
||||||
|
if n == 0:
|
||||||
|
return (255, 160, 60)
|
||||||
|
if n == 1:
|
||||||
|
return palette[0]
|
||||||
|
span = (n - 1) * pos256
|
||||||
|
seg = span >> 8
|
||||||
|
if seg >= n - 1:
|
||||||
|
return palette[n - 1]
|
||||||
|
frac = span & 0xFF
|
||||||
|
return _lerp_rgb(palette[seg], palette[seg + 1], frac)
|
||||||
|
|
||||||
|
|
||||||
|
def _triangle_255(elapsed_ms, period_ms):
|
||||||
|
period_ms = max(period_ms, 400)
|
||||||
|
p = elapsed_ms % period_ms
|
||||||
|
half = period_ms >> 1
|
||||||
|
if half <= 0:
|
||||||
|
return 128
|
||||||
|
if p < half:
|
||||||
|
return (p * 255) // half
|
||||||
|
return ((period_ms - p) * 255) // (period_ms - half)
|
||||||
|
|
||||||
|
|
||||||
|
class Flame:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def _build_palette(self, preset):
|
||||||
|
colors = preset.c
|
||||||
|
if not colors:
|
||||||
|
return list(_DEFAULT_PALETTE)
|
||||||
|
out = []
|
||||||
|
for c in colors:
|
||||||
|
if isinstance(c, (list, tuple)) and len(c) == 3:
|
||||||
|
out.append(
|
||||||
|
(
|
||||||
|
_clamp(int(c[0]), 0, 255),
|
||||||
|
_clamp(int(c[1]), 0, 255),
|
||||||
|
_clamp(int(c[2]), 0, 255),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return out if out else list(_DEFAULT_PALETTE)
|
||||||
|
|
||||||
|
def _draw_frame(self, preset, palette, ticks_now, breath_el_ms, rise, cluster_jit, breath_ms, lo, hi, spark_state):
|
||||||
|
"""spark_state: (active: bool, start_ticks, duration_ms). ticks_now for sparks; breath_el_ms for slow wave."""
|
||||||
|
num = self.driver.num_leds
|
||||||
|
denom = num - 1 if num > 1 else 1
|
||||||
|
|
||||||
|
breathe = _triangle_255(breath_el_ms, breath_ms)
|
||||||
|
base_level = lo + (((hi - lo) * breathe) >> 8)
|
||||||
|
micro = 232 + random.randint(0, 35)
|
||||||
|
level = (base_level * micro) >> 8
|
||||||
|
level = _clamp(level, lo, hi)
|
||||||
|
|
||||||
|
spark_boost = 0
|
||||||
|
spark_white = (0, 0, 0)
|
||||||
|
active, s0, dur = spark_state
|
||||||
|
if active and dur > 0:
|
||||||
|
el = utime.ticks_diff(ticks_now, s0)
|
||||||
|
if el < 0:
|
||||||
|
el = 0
|
||||||
|
if el >= dur:
|
||||||
|
spark_boost = 0
|
||||||
|
else:
|
||||||
|
env = 255 - ((el * 255) // dur)
|
||||||
|
spark_boost = (env * 90) >> 8
|
||||||
|
spark_white = ((env * 55) >> 8, (env * 50) >> 8, (env * 40) >> 8)
|
||||||
|
|
||||||
|
for i in range(num):
|
||||||
|
h = (i * 256) // denom
|
||||||
|
flow = (h + rise + ((i // max(1, num >> 3)) * 17)) & 255
|
||||||
|
pos = (flow + cluster_jit[(i >> 2) & 7]) & 255
|
||||||
|
rgb = _palette_sample(palette, pos)
|
||||||
|
if spark_boost:
|
||||||
|
rgb = (
|
||||||
|
_clamp(rgb[0] + spark_white[0] + (spark_boost * 3 >> 2), 0, 255),
|
||||||
|
_clamp(rgb[1] + spark_white[1] + (spark_boost >> 1), 0, 255),
|
||||||
|
_clamp(rgb[2] + spark_white[2] + (spark_boost >> 2), 0, 255),
|
||||||
|
)
|
||||||
|
self.driver.n[i] = self.driver.apply_brightness(rgb, level)
|
||||||
|
|
||||||
|
self.driver.n.write()
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
"""Salt-lamp / hearth-style flame: warm gradient, breathing, jitter, drift, rare sparks."""
|
||||||
|
palette = self._build_palette(preset)
|
||||||
|
lo = max(0, min(255, int(preset.n1)))
|
||||||
|
hi = max(0, min(255, int(preset.b)))
|
||||||
|
if lo > hi:
|
||||||
|
lo, hi = hi, lo
|
||||||
|
|
||||||
|
bp = int(preset.n2)
|
||||||
|
breath_ms = max(800, bp if bp > 0 else 2500)
|
||||||
|
|
||||||
|
gap_lo = int(preset.n3)
|
||||||
|
gap_hi = int(preset.n4)
|
||||||
|
# n3 < 0 disables sparks; n3=n4=0 uses ~10–30 s gaps (hearth pops).
|
||||||
|
if gap_lo < 0:
|
||||||
|
sparks_on = False
|
||||||
|
else:
|
||||||
|
sparks_on = True
|
||||||
|
if gap_lo == 0 and gap_hi == 0:
|
||||||
|
gap_lo, gap_hi = 10000, 30000
|
||||||
|
else:
|
||||||
|
gap_lo = max(gap_lo, 500)
|
||||||
|
if gap_hi < gap_lo:
|
||||||
|
gap_hi = gap_lo
|
||||||
|
|
||||||
|
delay_ms = max(16, int(preset.d))
|
||||||
|
rise = random.randint(0, 255)
|
||||||
|
cluster_jit = [random.randint(-18, 18) for _ in range(8)]
|
||||||
|
last_draw = utime.ticks_ms()
|
||||||
|
breath_origin = last_draw
|
||||||
|
last_cluster = last_draw
|
||||||
|
spark_active = False
|
||||||
|
spark_start = 0
|
||||||
|
spark_dur = 0
|
||||||
|
next_spark = utime.ticks_add(last_draw, random.randint(gap_lo, gap_hi)) if sparks_on else 0
|
||||||
|
|
||||||
|
if not preset.a:
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
self._draw_frame(
|
||||||
|
preset,
|
||||||
|
palette,
|
||||||
|
now,
|
||||||
|
utime.ticks_diff(now, breath_origin),
|
||||||
|
rise,
|
||||||
|
cluster_jit,
|
||||||
|
breath_ms,
|
||||||
|
lo,
|
||||||
|
hi,
|
||||||
|
(False, 0, 0),
|
||||||
|
)
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
|
||||||
|
while True:
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last_draw) < delay_ms:
|
||||||
|
yield
|
||||||
|
continue
|
||||||
|
last_draw = utime.ticks_add(last_draw, delay_ms)
|
||||||
|
|
||||||
|
rise = (rise + random.randint(-10, 12)) & 255
|
||||||
|
|
||||||
|
if utime.ticks_diff(now, last_cluster) >= (delay_ms * 4):
|
||||||
|
last_cluster = now
|
||||||
|
cluster_jit = [random.randint(-18, 18) for _ in range(8)]
|
||||||
|
|
||||||
|
spark_state = (spark_active, spark_start, spark_dur)
|
||||||
|
if sparks_on:
|
||||||
|
if spark_active:
|
||||||
|
if utime.ticks_diff(now, spark_start) >= spark_dur:
|
||||||
|
spark_active = False
|
||||||
|
next_spark = utime.ticks_add(
|
||||||
|
now,
|
||||||
|
random.randint(gap_lo, gap_hi),
|
||||||
|
)
|
||||||
|
elif utime.ticks_diff(now, next_spark) >= 0:
|
||||||
|
spark_active = True
|
||||||
|
spark_start = now
|
||||||
|
spark_dur = random.randint(180, 360)
|
||||||
|
|
||||||
|
self._draw_frame(
|
||||||
|
preset,
|
||||||
|
palette,
|
||||||
|
now,
|
||||||
|
utime.ticks_diff(now, breath_origin),
|
||||||
|
rise,
|
||||||
|
cluster_jit,
|
||||||
|
breath_ms,
|
||||||
|
lo,
|
||||||
|
hi,
|
||||||
|
(spark_active, spark_start, spark_dur),
|
||||||
|
)
|
||||||
|
yield
|
||||||
40
src/patterns/flicker.py
Normal file
40
src/patterns/flicker.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import random
|
||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class Flicker:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
"""Random brightness between n1 (min) and b (max); delay d ms between updates."""
|
||||||
|
colors = preset.c if preset.c else [(255, 255, 255)]
|
||||||
|
color_index = 0
|
||||||
|
last_update = utime.ticks_ms()
|
||||||
|
|
||||||
|
def brightness_bounds():
|
||||||
|
lo = max(0, min(255, int(preset.n1)))
|
||||||
|
hi = max(0, min(255, int(preset.b)))
|
||||||
|
if lo > hi:
|
||||||
|
lo, hi = hi, lo
|
||||||
|
return lo, hi
|
||||||
|
|
||||||
|
if not preset.a:
|
||||||
|
lo, hi = brightness_bounds()
|
||||||
|
level = random.randint(lo, hi)
|
||||||
|
base = colors[color_index % len(colors)]
|
||||||
|
self.driver.fill(self.driver.apply_brightness(base, level))
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
|
||||||
|
while True:
|
||||||
|
current_time = utime.ticks_ms()
|
||||||
|
delay_ms = max(1, int(preset.d))
|
||||||
|
lo, hi = brightness_bounds()
|
||||||
|
if utime.ticks_diff(current_time, last_update) >= delay_ms:
|
||||||
|
level = random.randint(lo, hi)
|
||||||
|
base = colors[color_index % len(colors)]
|
||||||
|
self.driver.fill(self.driver.apply_brightness(base, level))
|
||||||
|
color_index += 1
|
||||||
|
last_update = utime.ticks_add(last_update, delay_ms)
|
||||||
|
yield
|
||||||
57
src/patterns/gradient_scroll.py
Normal file
57
src/patterns/gradient_scroll.py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class GradientScroll:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def _render(self, colors, phase, brightness):
|
||||||
|
num_leds = self.driver.num_leds
|
||||||
|
color_count = len(colors)
|
||||||
|
if num_leds <= 0 or color_count <= 0:
|
||||||
|
return
|
||||||
|
if color_count == 1:
|
||||||
|
self.driver.fill(self.driver.apply_brightness(colors[0], brightness))
|
||||||
|
return
|
||||||
|
|
||||||
|
full_span = color_count * 256
|
||||||
|
phase_shift = (phase * full_span) // 256
|
||||||
|
for i in range(num_leds):
|
||||||
|
pos = ((i * full_span) // num_leds + phase_shift) % full_span
|
||||||
|
idx = pos // 256
|
||||||
|
frac = pos & 255
|
||||||
|
|
||||||
|
c1 = colors[idx]
|
||||||
|
c2 = colors[(idx + 1) % color_count]
|
||||||
|
blended = (
|
||||||
|
c1[0] + ((c2[0] - c1[0]) * frac) // 256,
|
||||||
|
c1[1] + ((c2[1] - c1[1]) * frac) // 256,
|
||||||
|
c1[2] + ((c2[2] - c1[2]) * frac) // 256,
|
||||||
|
)
|
||||||
|
self.driver.n[i] = self.driver.apply_brightness(blended, brightness)
|
||||||
|
self.driver.n.write()
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
"""Scrolling blended gradient.
|
||||||
|
|
||||||
|
n1: phase step amount (default 1)
|
||||||
|
"""
|
||||||
|
colors = preset.c if preset.c else [(255, 0, 0), (0, 0, 255)]
|
||||||
|
phase = self.driver.step % 256
|
||||||
|
step_amount = max(1, int(preset.n1) if int(preset.n1) > 0 else 1)
|
||||||
|
last_update = utime.ticks_ms()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
delay_ms = max(1, int(preset.d))
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last_update) >= delay_ms:
|
||||||
|
self._render(colors, phase, preset.b)
|
||||||
|
phase = (phase + step_amount) % 256
|
||||||
|
self.driver.step = phase
|
||||||
|
last_update = utime.ticks_add(last_update, delay_ms)
|
||||||
|
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
|
||||||
|
yield
|
||||||
36
src/patterns/heartbeat.py
Normal file
36
src/patterns/heartbeat.py
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class Heartbeat:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
colors = preset.c if preset.c else [(255, 0, 40)]
|
||||||
|
phase = 0
|
||||||
|
phase_start = utime.ticks_ms()
|
||||||
|
did_manual_pulse = False
|
||||||
|
while True:
|
||||||
|
p1 = max(20, int(preset.n1) if int(preset.n1) > 0 else 120)
|
||||||
|
p2 = max(20, int(preset.n2) if int(preset.n2) > 0 else 80)
|
||||||
|
pause = max(20, int(preset.n3) if int(preset.n3) > 0 else 500)
|
||||||
|
beat_gap = max(20, int(preset.d))
|
||||||
|
colors = preset.c if preset.c else [(255, 0, 40)]
|
||||||
|
lit_color = self.driver.apply_brightness(colors[0], preset.b)
|
||||||
|
bg_color = self.driver.apply_brightness(colors[-1], preset.b)
|
||||||
|
phase_durations = (p1, beat_gap, p2, pause)
|
||||||
|
phase_colors = (lit_color, bg_color, lit_color, bg_color)
|
||||||
|
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(now, phase_start) >= phase_durations[phase]:
|
||||||
|
phase_start = utime.ticks_add(phase_start, phase_durations[phase])
|
||||||
|
phase = (phase + 1) % 4
|
||||||
|
|
||||||
|
self.driver.fill(phase_colors[phase])
|
||||||
|
yield
|
||||||
|
if not preset.a:
|
||||||
|
if did_manual_pulse or phase == 0:
|
||||||
|
self.driver.fill(bg_color)
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
did_manual_pulse = True
|
||||||
31
src/patterns/marquee.py
Normal file
31
src/patterns/marquee.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class Marquee:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
colors = preset.c if preset.c else [(255, 255, 255)]
|
||||||
|
on_len = max(1, int(preset.n1) if int(preset.n1) > 0 else 3)
|
||||||
|
off_len = max(1, int(preset.n2) if int(preset.n2) > 0 else 2)
|
||||||
|
step = max(1, int(preset.n3) if int(preset.n3) > 0 else 1)
|
||||||
|
phase = self.driver.step % (on_len + off_len)
|
||||||
|
last = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
d = max(1, int(preset.d))
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
c = self.driver.apply_brightness(colors[0], preset.b)
|
||||||
|
bg_color = self.driver.apply_brightness(colors[-1], preset.b)
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
m = (i + phase) % (on_len + off_len)
|
||||||
|
self.driver.n[i] = c if m < on_len else bg_color
|
||||||
|
self.driver.n.write()
|
||||||
|
phase = (phase + step) % (on_len + off_len)
|
||||||
|
self.driver.step = phase
|
||||||
|
last = utime.ticks_add(last, d)
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
yield
|
||||||
62
src/patterns/meteor_rain.py
Normal file
62
src/patterns/meteor_rain.py
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class MeteorRain:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def _fade(self, color, fade_amount):
|
||||||
|
return (
|
||||||
|
(color[0] * fade_amount) // 255,
|
||||||
|
(color[1] * fade_amount) // 255,
|
||||||
|
(color[2] * fade_amount) // 255,
|
||||||
|
)
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
"""Single meteor with a fading tail.
|
||||||
|
|
||||||
|
n1: tail length (default 8)
|
||||||
|
n2: speed in LEDs per frame (default 1)
|
||||||
|
n3: fade amount per frame, 1..255 (default 192)
|
||||||
|
"""
|
||||||
|
colors = preset.c if preset.c else [(255, 255, 255)]
|
||||||
|
color_index = 0
|
||||||
|
head = 0
|
||||||
|
direction = 1
|
||||||
|
last_update = utime.ticks_ms()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
delay_ms = max(1, int(preset.d))
|
||||||
|
tail_len = max(1, int(preset.n1) if int(preset.n1) > 0 else 8)
|
||||||
|
speed = max(1, int(preset.n2) if int(preset.n2) > 0 else 1)
|
||||||
|
fade_amount = int(preset.n3) if int(preset.n3) > 0 else 192
|
||||||
|
fade_amount = max(1, min(255, fade_amount))
|
||||||
|
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last_update) >= delay_ms:
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
self.driver.n[i] = self._fade(self.driver.n[i], fade_amount)
|
||||||
|
|
||||||
|
base = colors[color_index % len(colors)]
|
||||||
|
lit = self.driver.apply_brightness(base, preset.b)
|
||||||
|
if 0 <= head < self.driver.num_leds:
|
||||||
|
self.driver.n[head] = lit
|
||||||
|
self.driver.n.write()
|
||||||
|
|
||||||
|
head += direction * speed
|
||||||
|
if head >= self.driver.num_leds + tail_len:
|
||||||
|
head = self.driver.num_leds - 1
|
||||||
|
direction = -1
|
||||||
|
color_index += 1
|
||||||
|
elif head < -tail_len:
|
||||||
|
head = 0
|
||||||
|
direction = 1
|
||||||
|
color_index += 1
|
||||||
|
|
||||||
|
last_update = utime.ticks_add(last_update, delay_ms)
|
||||||
|
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
|
||||||
|
yield
|
||||||
31
src/patterns/orbit.py
Normal file
31
src/patterns/orbit.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class Orbit:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
colors = preset.c if preset.c else [(255, 255, 255), (0, 180, 255), (255, 0, 120)]
|
||||||
|
orbits = max(1, int(preset.n1) if int(preset.n1) > 0 else 3)
|
||||||
|
speed = max(1, int(preset.n2) if int(preset.n2) > 0 else 1)
|
||||||
|
phase = self.driver.step % 256
|
||||||
|
last = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
d = max(1, int(preset.d))
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
bg_color = self.driver.apply_brightness(colors[-1], preset.b)
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
self.driver.n[i] = bg_color
|
||||||
|
for k in range(orbits):
|
||||||
|
idx = ((phase * (k + 1)) // 8 + (k * self.driver.num_leds // max(1, orbits))) % max(1, self.driver.num_leds)
|
||||||
|
self.driver.n[idx] = self.driver.apply_brightness(colors[k % len(colors)], preset.b)
|
||||||
|
self.driver.n.write()
|
||||||
|
phase = (phase + speed) & 255
|
||||||
|
self.driver.step = phase
|
||||||
|
last = utime.ticks_add(last, d)
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
yield
|
||||||
81
src/patterns/palette_morph.py
Normal file
81
src/patterns/palette_morph.py
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class PaletteMorph:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def _blend(self, c1, c2, t):
|
||||||
|
return (
|
||||||
|
c1[0] + ((c2[0] - c1[0]) * t) // 255,
|
||||||
|
c1[1] + ((c2[1] - c1[1]) * t) // 255,
|
||||||
|
c1[2] + ((c2[2] - c1[2]) * t) // 255,
|
||||||
|
)
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
"""Living color field (non-scrolling palette warp).
|
||||||
|
|
||||||
|
Different from `colour_cycle`: this does not scroll a fixed gradient.
|
||||||
|
Instead, each LED breathes/warps through the palette with local phase
|
||||||
|
offsets so the strip looks alive.
|
||||||
|
|
||||||
|
n1: morph duration (ms)
|
||||||
|
n2: warp rate
|
||||||
|
n3: spatial turbulence amount
|
||||||
|
"""
|
||||||
|
colors = preset.c if preset.c else [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
|
||||||
|
if len(colors) < 2:
|
||||||
|
while True:
|
||||||
|
self.driver.fill(self.driver.apply_brightness(colors[0], preset.b))
|
||||||
|
yield
|
||||||
|
morph = max(50, int(preset.n1) if int(preset.n1) > 0 else 1200)
|
||||||
|
warp_rate = max(1, int(preset.n2) if int(preset.n2) > 0 else 3)
|
||||||
|
turbulence = max(1, int(preset.n3) if int(preset.n3) > 0 else 24)
|
||||||
|
base_idx = 0
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
phase = self.driver.step % 256
|
||||||
|
last_update = start
|
||||||
|
|
||||||
|
while True:
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
delay_ms = max(1, int(preset.d))
|
||||||
|
if utime.ticks_diff(now, last_update) < delay_ms:
|
||||||
|
yield
|
||||||
|
continue
|
||||||
|
last_update = utime.ticks_add(last_update, delay_ms)
|
||||||
|
age = utime.ticks_diff(now, start)
|
||||||
|
if age < morph:
|
||||||
|
t = (age * 255) // morph
|
||||||
|
else:
|
||||||
|
t = 255
|
||||||
|
|
||||||
|
# Global morph anchor between neighboring palette colors.
|
||||||
|
a = colors[base_idx % len(colors)]
|
||||||
|
b = colors[(base_idx + 1) % len(colors)]
|
||||||
|
anchor = self._blend(a, b, t)
|
||||||
|
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
# Non-linear local warp per LED to create "living" motion.
|
||||||
|
pos = (i * 256) // max(1, self.driver.num_leds)
|
||||||
|
wobble = ((pos * turbulence) // 32 + phase + (t // 2)) & 255
|
||||||
|
breath = 255 - abs(128 - wobble) * 2
|
||||||
|
local = (pos + (breath // 3) + (t // 4)) % 256
|
||||||
|
idx = (base_idx + ((local * len(colors)) // 256)) % len(colors)
|
||||||
|
frac = (local * len(colors)) & 255
|
||||||
|
c1 = colors[idx]
|
||||||
|
c2 = colors[(idx + 1) % len(colors)]
|
||||||
|
grad = self._blend(c1, c2, frac)
|
||||||
|
# Blend with anchor to keep coherent palette morphing.
|
||||||
|
out = self._blend(grad, anchor, 80)
|
||||||
|
self.driver.n[i] = self.driver.apply_brightness(out, preset.b)
|
||||||
|
self.driver.n.write()
|
||||||
|
|
||||||
|
if age >= morph:
|
||||||
|
base_idx = (base_idx + 1) % len(colors)
|
||||||
|
start = now
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
phase = (phase + warp_rate) & 255
|
||||||
|
self.driver.step = phase
|
||||||
|
yield
|
||||||
39
src/patterns/plasma.py
Normal file
39
src/patterns/plasma.py
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class Plasma:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def _wheel(self, pos):
|
||||||
|
if pos < 85:
|
||||||
|
return (pos * 3, 255 - pos * 3, 0)
|
||||||
|
if pos < 170:
|
||||||
|
pos -= 85
|
||||||
|
return (255 - pos * 3, 0, pos * 3)
|
||||||
|
pos -= 170
|
||||||
|
return (0, pos * 3, 255 - pos * 3)
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
scale = max(1, int(preset.n1) if int(preset.n1) > 0 else 6)
|
||||||
|
speed = max(1, int(preset.n2) if int(preset.n2) > 0 else 2)
|
||||||
|
contrast = max(1, int(preset.n3) if int(preset.n3) > 0 else 2)
|
||||||
|
t = self.driver.step % 256
|
||||||
|
last = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
d = max(1, int(preset.d))
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
v = ((i * scale + t) & 255)
|
||||||
|
v2 = (((i * scale // max(1, contrast)) - (t * 2)) & 255)
|
||||||
|
c = self._wheel((v + v2) & 255)
|
||||||
|
self.driver.n[i] = self.driver.apply_brightness(c, preset.b)
|
||||||
|
self.driver.n.write()
|
||||||
|
t = (t + speed) % 256
|
||||||
|
self.driver.step = t
|
||||||
|
last = utime.ticks_add(last, d)
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
yield
|
||||||
@@ -18,6 +18,7 @@ class Pulse:
|
|||||||
|
|
||||||
# State machine based pulse using a single generator loop
|
# State machine based pulse using a single generator loop
|
||||||
while True:
|
while True:
|
||||||
|
bg_color = self.driver.apply_brightness(colors[-1], preset.b)
|
||||||
# Read current timing parameters from preset
|
# Read current timing parameters from preset
|
||||||
attack_ms = max(0, int(preset.n1)) # Attack time in ms
|
attack_ms = max(0, int(preset.n1)) # Attack time in ms
|
||||||
hold_ms = max(0, int(preset.n2)) # Hold time in ms
|
hold_ms = max(0, int(preset.n2)) # Hold time in ms
|
||||||
@@ -49,7 +50,7 @@ class Pulse:
|
|||||||
self.driver.fill(self.driver.apply_brightness(color, preset.b))
|
self.driver.fill(self.driver.apply_brightness(color, preset.b))
|
||||||
elif elapsed < total_ms:
|
elif elapsed < total_ms:
|
||||||
# Delay phase: LEDs off between pulses
|
# Delay phase: LEDs off between pulses
|
||||||
self.driver.fill((0, 0, 0))
|
self.driver.fill(bg_color)
|
||||||
else:
|
else:
|
||||||
# End of cycle, move to next color and restart timing
|
# End of cycle, move to next color and restart timing
|
||||||
color_index += 1
|
color_index += 1
|
||||||
|
|||||||
136
src/patterns/radiate.py
Normal file
136
src/patterns/radiate.py
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
import utime
|
||||||
|
|
||||||
|
_RADIATE_DBG_INTERVAL_MS = 1000
|
||||||
|
|
||||||
|
|
||||||
|
class Radiate:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
"""Radiate from nodes every n1 LEDs, retriggering every delay (d).
|
||||||
|
|
||||||
|
- n1: node spacing in LEDs
|
||||||
|
- n2: outbound travel time in ms
|
||||||
|
- n3: return travel time in ms
|
||||||
|
- d: retrigger interval in ms
|
||||||
|
"""
|
||||||
|
colors = preset.c if preset.c else [(255, 255, 255)]
|
||||||
|
base_on = colors[0]
|
||||||
|
base_off = colors[-1]
|
||||||
|
|
||||||
|
spacing = max(1, int(preset.n1))
|
||||||
|
outward_ms = max(1, int(preset.n2))
|
||||||
|
return_ms = max(1, int(preset.n3))
|
||||||
|
max_dist = spacing // 2
|
||||||
|
|
||||||
|
lit_color = self.driver.apply_brightness(base_on, preset.b)
|
||||||
|
off_color = self.driver.apply_brightness(base_off, preset.b)
|
||||||
|
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
last_trigger = now
|
||||||
|
active_pulses = [now]
|
||||||
|
last_dbg = now
|
||||||
|
dbg_banner = False
|
||||||
|
|
||||||
|
if not preset.a:
|
||||||
|
# Single-step render uses only the first instant pulse.
|
||||||
|
active_pulses = [utime.ticks_ms()]
|
||||||
|
|
||||||
|
while True:
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
delay_ms = max(1, int(preset.d))
|
||||||
|
spacing = max(1, int(preset.n1))
|
||||||
|
outward_ms = max(1, int(preset.n2))
|
||||||
|
return_ms = max(1, int(preset.n3))
|
||||||
|
max_dist = spacing // 2
|
||||||
|
lit_color = self.driver.apply_brightness(base_on, preset.b)
|
||||||
|
off_color = self.driver.apply_brightness(base_off, preset.b)
|
||||||
|
|
||||||
|
if preset.a and utime.ticks_diff(now, last_trigger) >= delay_ms:
|
||||||
|
# Keep one pulse train at a time; replacing instead of appending
|
||||||
|
# prevents overlap from keeping color[0] continuously visible.
|
||||||
|
active_pulses = [now]
|
||||||
|
last_trigger = utime.ticks_add(last_trigger, delay_ms)
|
||||||
|
if bool(getattr(self.driver, "debug", False)):
|
||||||
|
print(
|
||||||
|
"[radiate] trigger spacing=%d out=%d in=%d delay=%d"
|
||||||
|
% (spacing, outward_ms, return_ms, delay_ms)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Drop pulses once their out-and-back lifetime ends.
|
||||||
|
pulse_lifetime = outward_ms + return_ms
|
||||||
|
kept = []
|
||||||
|
for start in active_pulses:
|
||||||
|
age = utime.ticks_diff(now, start)
|
||||||
|
if age < pulse_lifetime:
|
||||||
|
kept.append(start)
|
||||||
|
active_pulses = kept
|
||||||
|
debug_front = -1
|
||||||
|
lit_count = 0
|
||||||
|
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
# Nearest node distance for a repeating node grid every `spacing` LEDs.
|
||||||
|
offset = i % spacing
|
||||||
|
dist = min(offset, spacing - offset)
|
||||||
|
|
||||||
|
lit = False
|
||||||
|
for start in active_pulses:
|
||||||
|
age = utime.ticks_diff(now, start)
|
||||||
|
# Do not render on the exact trigger tick; this avoids
|
||||||
|
# node LEDs appearing "stuck on" between cycles.
|
||||||
|
if age <= 0:
|
||||||
|
continue
|
||||||
|
if age <= outward_ms:
|
||||||
|
# Integer-ceiling progression so peak can be reached even
|
||||||
|
# when tick timing skips the exact outward_ms boundary.
|
||||||
|
front = (age * max_dist + outward_ms - 1) // outward_ms
|
||||||
|
elif age <= outward_ms + return_ms:
|
||||||
|
back_age = age - outward_ms
|
||||||
|
remaining = return_ms - back_age
|
||||||
|
front = (remaining * max_dist + return_ms - 1) // return_ms
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if dist <= front:
|
||||||
|
lit = True
|
||||||
|
if front > debug_front:
|
||||||
|
debug_front = front
|
||||||
|
break
|
||||||
|
|
||||||
|
self.driver.n[i] = lit_color if lit else off_color
|
||||||
|
if lit:
|
||||||
|
lit_count += 1
|
||||||
|
|
||||||
|
self.driver.n.write()
|
||||||
|
|
||||||
|
if bool(getattr(self.driver, "debug", False)):
|
||||||
|
if not dbg_banner:
|
||||||
|
dbg_banner = True
|
||||||
|
print(
|
||||||
|
"[radiate] debug on: spacing=%s out=%s in=%s d=%s num=%d"
|
||||||
|
% (
|
||||||
|
preset.n1,
|
||||||
|
preset.n2,
|
||||||
|
preset.n3,
|
||||||
|
preset.d,
|
||||||
|
self.driver.num_leds,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if utime.ticks_diff(now, last_dbg) >= _RADIATE_DBG_INTERVAL_MS:
|
||||||
|
pulse_age = -1
|
||||||
|
if active_pulses:
|
||||||
|
pulse_age = utime.ticks_diff(now, active_pulses[0])
|
||||||
|
print(
|
||||||
|
"[radiate] age=%d front=%d max=%d active=%d lit=%d"
|
||||||
|
% (pulse_age, debug_front, max_dist, len(active_pulses), lit_count)
|
||||||
|
)
|
||||||
|
if lit_count == 0:
|
||||||
|
print("[radiate] fully off")
|
||||||
|
last_dbg = now
|
||||||
|
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
|
||||||
|
yield
|
||||||
41
src/patterns/rain_drops.py
Normal file
41
src/patterns/rain_drops.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import random
|
||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class RainDrops:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
colors = preset.c if preset.c else [(120, 180, 255)]
|
||||||
|
rate = max(1, int(preset.n1) if int(preset.n1) > 0 else 32)
|
||||||
|
width = max(1, int(preset.n2) if int(preset.n2) > 0 else 3)
|
||||||
|
drops = []
|
||||||
|
last = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
d = max(1, int(preset.d))
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
bg_color = self.driver.apply_brightness(colors[-1], preset.b)
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
self.driver.n[i] = bg_color
|
||||||
|
if random.randint(0, 255) < rate:
|
||||||
|
drops.append([random.randint(0, max(0, self.driver.num_leds - 1)), 0])
|
||||||
|
nd = []
|
||||||
|
for pos, age in drops:
|
||||||
|
for off in range(-width, width + 1):
|
||||||
|
idx = pos + off
|
||||||
|
if 0 <= idx < self.driver.num_leds:
|
||||||
|
s = 255 - min(255, abs(off) * 255 // max(1, width + 1) + age * 40)
|
||||||
|
base = self.driver.apply_brightness(colors[age % len(colors)], preset.b)
|
||||||
|
self.driver.n[idx] = ((base[0]*s)//255, (base[1]*s)//255, (base[2]*s)//255)
|
||||||
|
age += 1
|
||||||
|
if age < 8:
|
||||||
|
nd.append([pos, age])
|
||||||
|
drops = nd
|
||||||
|
self.driver.n.write()
|
||||||
|
last = utime.ticks_add(last, d)
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
yield
|
||||||
@@ -46,6 +46,6 @@ class Rainbow:
|
|||||||
self.driver.n.write()
|
self.driver.n.write()
|
||||||
step = (step + step_amount) % 256
|
step = (step + step_amount) % 256
|
||||||
self.driver.step = step
|
self.driver.step = step
|
||||||
last_update = current_time
|
last_update = utime.ticks_add(last_update, sleep_ms)
|
||||||
# Yield once per tick so other logic can run
|
# Yield once per tick so other logic can run
|
||||||
yield
|
yield
|
||||||
|
|||||||
67
src/patterns/scanner.py
Normal file
67
src/patterns/scanner.py
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class Scanner:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
"""Classic scanner eye with soft falloff.
|
||||||
|
|
||||||
|
n1: eye width (default 4)
|
||||||
|
n2: end pause in frames (default 0)
|
||||||
|
"""
|
||||||
|
colors = preset.c if preset.c else [(255, 0, 0)]
|
||||||
|
color_index = 0
|
||||||
|
center = 0
|
||||||
|
direction = 1
|
||||||
|
pause_frames = 0
|
||||||
|
last_update = utime.ticks_ms()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
delay_ms = max(1, int(preset.d))
|
||||||
|
width = max(1, int(preset.n1) if int(preset.n1) > 0 else 4)
|
||||||
|
end_pause = max(0, int(preset.n2))
|
||||||
|
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last_update) >= delay_ms:
|
||||||
|
base = colors[color_index % len(colors)]
|
||||||
|
base = self.driver.apply_brightness(base, preset.b)
|
||||||
|
bg_color = self.driver.apply_brightness(colors[-1], preset.b)
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
dist = i - center
|
||||||
|
if dist < 0:
|
||||||
|
dist = -dist
|
||||||
|
if dist > width:
|
||||||
|
self.driver.n[i] = bg_color
|
||||||
|
else:
|
||||||
|
scale = ((width - dist) * 255) // max(1, width)
|
||||||
|
self.driver.n[i] = (
|
||||||
|
(base[0] * scale) // 255,
|
||||||
|
(base[1] * scale) // 255,
|
||||||
|
(base[2] * scale) // 255,
|
||||||
|
)
|
||||||
|
self.driver.n.write()
|
||||||
|
|
||||||
|
if pause_frames > 0:
|
||||||
|
pause_frames -= 1
|
||||||
|
else:
|
||||||
|
center += direction
|
||||||
|
if center >= self.driver.num_leds - 1:
|
||||||
|
center = self.driver.num_leds - 1
|
||||||
|
direction = -1
|
||||||
|
pause_frames = end_pause
|
||||||
|
color_index += 1
|
||||||
|
elif center <= 0:
|
||||||
|
center = 0
|
||||||
|
direction = 1
|
||||||
|
pause_frames = end_pause
|
||||||
|
color_index += 1
|
||||||
|
|
||||||
|
last_update = utime.ticks_add(last_update, delay_ms)
|
||||||
|
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
|
||||||
|
yield
|
||||||
45
src/patterns/segment_chase.py
Normal file
45
src/patterns/segment_chase.py
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class SegmentChase:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
"""Independent moving segments (distinct from classic two-color chase).
|
||||||
|
|
||||||
|
n1: segment size (LEDs per segment)
|
||||||
|
n2: step size (phase increment each frame)
|
||||||
|
n3: per-segment phase offset
|
||||||
|
n4: gap spacing inside segment (0 = solid segment)
|
||||||
|
"""
|
||||||
|
colors = preset.c if preset.c else [(255, 0, 0), (0, 0, 255)]
|
||||||
|
seg = max(1, int(preset.n1) if int(preset.n1) > 0 else 4)
|
||||||
|
phase_step = max(1, int(preset.n2) if int(preset.n2) > 0 else 1)
|
||||||
|
seg_offset = max(0, int(preset.n3))
|
||||||
|
gap = max(0, int(preset.n4))
|
||||||
|
phase = self.driver.step % 256
|
||||||
|
last = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
d = max(1, int(preset.d))
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
bg_color = self.driver.apply_brightness(colors[-1], preset.b)
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
seg_idx = i // seg
|
||||||
|
in_seg = i % seg
|
||||||
|
local_phase = (phase + seg_idx * seg_offset) % seg
|
||||||
|
lit_idx = (in_seg + local_phase) % seg
|
||||||
|
if gap > 0 and lit_idx >= max(1, seg - gap):
|
||||||
|
self.driver.n[i] = bg_color
|
||||||
|
else:
|
||||||
|
color_idx = seg_idx % len(colors)
|
||||||
|
self.driver.n[i] = self.driver.apply_brightness(colors[color_idx], preset.b)
|
||||||
|
self.driver.n.write()
|
||||||
|
phase = (phase + phase_step) % seg
|
||||||
|
self.driver.step = phase
|
||||||
|
last = utime.ticks_add(last, d)
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
yield
|
||||||
37
src/patterns/snowfall.py
Normal file
37
src/patterns/snowfall.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import random
|
||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class Snowfall:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
colors = preset.c if preset.c else [(255, 255, 255), (180, 220, 255)]
|
||||||
|
density = max(1, int(preset.n1) if int(preset.n1) > 0 else 20)
|
||||||
|
speed = max(1, int(preset.n2) if int(preset.n2) > 0 else 1)
|
||||||
|
flakes = []
|
||||||
|
last = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
d = max(1, int(preset.d))
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
bg_color = self.driver.apply_brightness(colors[-1], preset.b)
|
||||||
|
if random.randint(0, 255) < density:
|
||||||
|
flakes.append([self.driver.num_leds - 1, random.randint(0, len(colors)-1)])
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
self.driver.n[i] = bg_color
|
||||||
|
nf = []
|
||||||
|
for pos, ci in flakes:
|
||||||
|
if 0 <= pos < self.driver.num_leds:
|
||||||
|
self.driver.n[pos] = self.driver.apply_brightness(colors[ci], preset.b)
|
||||||
|
pos -= speed
|
||||||
|
if pos >= -1:
|
||||||
|
nf.append([pos, ci])
|
||||||
|
flakes = nf
|
||||||
|
self.driver.n.write()
|
||||||
|
last = utime.ticks_add(last, d)
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
yield
|
||||||
31
src/patterns/sparkle_trail.py
Normal file
31
src/patterns/sparkle_trail.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import random
|
||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class SparkleTrail:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
colors = preset.c if preset.c else [(120, 120, 255)]
|
||||||
|
density = max(1, int(preset.n1) if int(preset.n1) > 0 else 24)
|
||||||
|
decay = max(1, min(255, int(preset.n2) if int(preset.n2) > 0 else 210))
|
||||||
|
last = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
d = max(1, int(preset.d))
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
r,g,b = self.driver.n[i]
|
||||||
|
self.driver.n[i] = ((r*decay)//255, (g*decay)//255, (b*decay)//255)
|
||||||
|
sparks = max(1, self.driver.num_leds * density // 255)
|
||||||
|
for _ in range(sparks):
|
||||||
|
idx = random.randint(0, max(0, self.driver.num_leds - 1))
|
||||||
|
c = self.driver.apply_brightness(colors[random.randint(0, len(colors)-1)], preset.b)
|
||||||
|
self.driver.n[idx] = c
|
||||||
|
self.driver.n.write()
|
||||||
|
last = utime.ticks_add(last, d)
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
yield
|
||||||
45
src/patterns/strobe_burst.py
Normal file
45
src/patterns/strobe_burst.py
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class StrobeBurst:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
colors = preset.c if preset.c else [(255, 255, 255)]
|
||||||
|
state = "flash_on"
|
||||||
|
flash_idx = 0
|
||||||
|
state_start = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
count = max(1, int(preset.n1) if int(preset.n1) > 0 else 3)
|
||||||
|
gap = max(1, int(preset.n2) if int(preset.n2) > 0 else 60)
|
||||||
|
cooldown = max(1, int(preset.n3) if int(preset.n3) > 0 else 400)
|
||||||
|
on_ms = max(1, int(preset.d) // 2)
|
||||||
|
c = self.driver.apply_brightness(colors[0], preset.b)
|
||||||
|
bg_color = self.driver.apply_brightness(colors[-1], preset.b)
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
|
||||||
|
if state == "flash_on":
|
||||||
|
self.driver.fill(c)
|
||||||
|
if utime.ticks_diff(now, state_start) >= on_ms:
|
||||||
|
state = "flash_off"
|
||||||
|
state_start = utime.ticks_add(state_start, on_ms)
|
||||||
|
elif state == "flash_off":
|
||||||
|
self.driver.fill(bg_color)
|
||||||
|
if utime.ticks_diff(now, state_start) >= gap:
|
||||||
|
flash_idx += 1
|
||||||
|
if flash_idx >= count:
|
||||||
|
if not preset.a:
|
||||||
|
return
|
||||||
|
state = "cooldown"
|
||||||
|
flash_idx = 0
|
||||||
|
state_start = utime.ticks_add(state_start, gap)
|
||||||
|
else:
|
||||||
|
state = "flash_on"
|
||||||
|
state_start = utime.ticks_add(state_start, gap)
|
||||||
|
else:
|
||||||
|
self.driver.fill(bg_color)
|
||||||
|
if utime.ticks_diff(now, state_start) >= cooldown:
|
||||||
|
state = "flash_on"
|
||||||
|
state_start = utime.ticks_add(state_start, cooldown)
|
||||||
|
yield
|
||||||
228
src/patterns/twinkle.py
Normal file
228
src/patterns/twinkle.py
Normal file
@@ -0,0 +1,228 @@
|
|||||||
|
import random
|
||||||
|
import utime
|
||||||
|
|
||||||
|
# Default cool palette (icy blues, violet, mint) when preset has no colours.
|
||||||
|
# When `driver.debug` is True, print stats every N twinkle ticks (serial can be slow).
|
||||||
|
_TWINKLE_DBG_INTERVAL = 40
|
||||||
|
|
||||||
|
_DEFAULT_COOL = (
|
||||||
|
(120, 200, 255),
|
||||||
|
(80, 140, 255),
|
||||||
|
(180, 120, 255),
|
||||||
|
(100, 220, 240),
|
||||||
|
(160, 200, 255),
|
||||||
|
(90, 180, 220),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Twinkle:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def _palette(self, preset):
|
||||||
|
colors = preset.c
|
||||||
|
if not colors:
|
||||||
|
return list(_DEFAULT_COOL)
|
||||||
|
out = []
|
||||||
|
for c in colors:
|
||||||
|
if isinstance(c, (list, tuple)) and len(c) == 3:
|
||||||
|
out.append(
|
||||||
|
(
|
||||||
|
max(0, min(255, int(c[0]))),
|
||||||
|
max(0, min(255, int(c[1]))),
|
||||||
|
max(0, min(255, int(c[2]))),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return out if out else list(_DEFAULT_COOL)
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
"""Twinkle: n1 activity, n2 density; n3/n4 min/max length of adjacent on/off runs."""
|
||||||
|
palette = self._palette(preset)
|
||||||
|
num = self.driver.num_leds
|
||||||
|
bg_color = self.driver.apply_brightness(palette[-1], preset.b)
|
||||||
|
if num <= 0:
|
||||||
|
while True:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
|
||||||
|
def activity_rate():
|
||||||
|
r = int(preset.n1)
|
||||||
|
if r <= 0:
|
||||||
|
r = 48
|
||||||
|
return max(1, min(255, r))
|
||||||
|
|
||||||
|
def density255():
|
||||||
|
"""Higher → more LEDs lit on average when a twinkle step fires (0 = default mid)."""
|
||||||
|
d = int(preset.n2)
|
||||||
|
if d <= 0:
|
||||||
|
d = 128
|
||||||
|
return max(0, min(255, d))
|
||||||
|
|
||||||
|
def cluster_len_bounds():
|
||||||
|
"""n3 = min adjacent LEDs per twinkle, n4 = max (both 0 → 1..4)."""
|
||||||
|
lo = int(preset.n3)
|
||||||
|
hi = int(preset.n4)
|
||||||
|
if lo <= 0 and hi <= 0:
|
||||||
|
lo, hi = 1, min(4, num)
|
||||||
|
else:
|
||||||
|
if lo <= 0:
|
||||||
|
lo = 1
|
||||||
|
if hi <= 0:
|
||||||
|
hi = lo
|
||||||
|
if hi < lo:
|
||||||
|
lo, hi = hi, lo
|
||||||
|
lo = max(1, min(lo, num))
|
||||||
|
hi = max(lo, min(hi, num))
|
||||||
|
return lo, hi
|
||||||
|
|
||||||
|
def random_cluster_len():
|
||||||
|
lo, hi = cluster_len_bounds()
|
||||||
|
# When min and max match, every lit/dim run is exactly that many LEDs (still capped by strip length).
|
||||||
|
if lo == hi:
|
||||||
|
return lo
|
||||||
|
return random.randint(lo, hi)
|
||||||
|
|
||||||
|
def cluster_base_index(start, k):
|
||||||
|
"""Shift run left so a length-k segment fits; keeps full k when num >= k."""
|
||||||
|
k = min(max(0, int(k)), num)
|
||||||
|
if k <= 0:
|
||||||
|
return 0
|
||||||
|
return max(0, min(int(start), num - k))
|
||||||
|
|
||||||
|
dens = density255()
|
||||||
|
on = [random.randint(0, 255) < dens for _ in range(num)]
|
||||||
|
colour_i = [random.randint(0, len(palette) - 1) for _ in range(num)]
|
||||||
|
last_update = utime.ticks_ms()
|
||||||
|
dbg_tick = 0
|
||||||
|
dbg_banner = False
|
||||||
|
|
||||||
|
def on_run_min_max(bits):
|
||||||
|
"""Smallest and largest contiguous run of True in bits (0,0 if all off)."""
|
||||||
|
best_min = num + 1
|
||||||
|
best_max = 0
|
||||||
|
cur = 0
|
||||||
|
for j in range(num):
|
||||||
|
if bits[j]:
|
||||||
|
cur += 1
|
||||||
|
else:
|
||||||
|
if cur:
|
||||||
|
if cur < best_min:
|
||||||
|
best_min = cur
|
||||||
|
if cur > best_max:
|
||||||
|
best_max = cur
|
||||||
|
cur = 0
|
||||||
|
if cur:
|
||||||
|
if cur < best_min:
|
||||||
|
best_min = cur
|
||||||
|
if cur > best_max:
|
||||||
|
best_max = cur
|
||||||
|
if best_min == num + 1:
|
||||||
|
return 0, 0
|
||||||
|
return best_min, best_max
|
||||||
|
|
||||||
|
if not preset.a:
|
||||||
|
for i in range(num):
|
||||||
|
if on[i]:
|
||||||
|
base = palette[colour_i[i] % len(palette)]
|
||||||
|
self.driver.n[i] = self.driver.apply_brightness(base, preset.b)
|
||||||
|
else:
|
||||||
|
self.driver.n[i] = bg_color
|
||||||
|
self.driver.n.write()
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
|
||||||
|
while True:
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
delay_ms = max(1, int(preset.d))
|
||||||
|
if utime.ticks_diff(now, last_update) >= delay_ms:
|
||||||
|
rate = activity_rate()
|
||||||
|
dens = density255()
|
||||||
|
dbg = bool(getattr(self.driver, "debug", False))
|
||||||
|
dbg_tick += 1
|
||||||
|
# Snapshot for decisions; apply all darks then all lights so
|
||||||
|
# overlaps in the same tick favour lit runs (lights win).
|
||||||
|
prev_on = on[:]
|
||||||
|
prev_ci = colour_i[:]
|
||||||
|
next_on = list(prev_on)
|
||||||
|
next_ci = list(prev_ci)
|
||||||
|
dbg_ops = {"L": 0, "D": 0}
|
||||||
|
|
||||||
|
light_i = []
|
||||||
|
dark_i = []
|
||||||
|
for i in range(num):
|
||||||
|
if random.randint(0, 255) < rate:
|
||||||
|
r = random.randint(0, 255)
|
||||||
|
if not prev_on[i]:
|
||||||
|
if r < dens:
|
||||||
|
light_i.append(i)
|
||||||
|
else:
|
||||||
|
if r < (255 - dens):
|
||||||
|
dark_i.append(i)
|
||||||
|
|
||||||
|
def light_adjacent(start):
|
||||||
|
dbg_ops["L"] += 1
|
||||||
|
k = random_cluster_len()
|
||||||
|
b = cluster_base_index(start, k)
|
||||||
|
for dj in range(k):
|
||||||
|
idx = b + dj
|
||||||
|
next_on[idx] = True
|
||||||
|
next_ci[idx] = random.randint(0, len(palette) - 1)
|
||||||
|
|
||||||
|
def dark_adjacent(start):
|
||||||
|
dbg_ops["D"] += 1
|
||||||
|
k = random_cluster_len()
|
||||||
|
b = cluster_base_index(start, k)
|
||||||
|
for dj in range(k):
|
||||||
|
idx = b + dj
|
||||||
|
next_on[idx] = False
|
||||||
|
|
||||||
|
for i in dark_i:
|
||||||
|
dark_adjacent(i)
|
||||||
|
for i in light_i:
|
||||||
|
light_adjacent(i)
|
||||||
|
|
||||||
|
for i in range(num):
|
||||||
|
if next_on[i]:
|
||||||
|
base = palette[next_ci[i] % len(palette)]
|
||||||
|
self.driver.n[i] = self.driver.apply_brightness(base, preset.b)
|
||||||
|
else:
|
||||||
|
self.driver.n[i] = bg_color
|
||||||
|
self.driver.n.write()
|
||||||
|
on = next_on
|
||||||
|
colour_i = next_ci
|
||||||
|
last_update = utime.ticks_add(last_update, delay_ms)
|
||||||
|
|
||||||
|
if dbg:
|
||||||
|
lo, hi = cluster_len_bounds()
|
||||||
|
if not dbg_banner:
|
||||||
|
dbg_banner = True
|
||||||
|
print(
|
||||||
|
"[twinkle] debug on: n1=%s n2=%s n3=%s n4=%s d=%s -> lo=%d hi=%d num=%d"
|
||||||
|
% (
|
||||||
|
preset.n1,
|
||||||
|
preset.n2,
|
||||||
|
preset.n3,
|
||||||
|
preset.n4,
|
||||||
|
preset.d,
|
||||||
|
lo,
|
||||||
|
hi,
|
||||||
|
num,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
rmin, rmax = on_run_min_max(on)
|
||||||
|
bad = lo > 0 and rmin > 0 and rmin < lo and num >= lo
|
||||||
|
if bad or (dbg_tick % _TWINKLE_DBG_INTERVAL == 0):
|
||||||
|
print(
|
||||||
|
"[twinkle] tick=%d rate=%d dens=%d L=%d D=%d on_runs min=%d max=%d%s"
|
||||||
|
% (
|
||||||
|
dbg_tick,
|
||||||
|
rate,
|
||||||
|
dens,
|
||||||
|
dbg_ops["L"],
|
||||||
|
dbg_ops["D"],
|
||||||
|
rmin,
|
||||||
|
rmax,
|
||||||
|
" **run<lo**" if bad else "",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
yield
|
||||||
32
src/patterns/wave.py
Normal file
32
src/patterns/wave.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import utime
|
||||||
|
|
||||||
|
|
||||||
|
class Wave:
|
||||||
|
def __init__(self, driver):
|
||||||
|
self.driver = driver
|
||||||
|
|
||||||
|
def run(self, preset):
|
||||||
|
colors = preset.c if preset.c else [(0, 180, 255)]
|
||||||
|
wavelength = max(2, int(preset.n1) if int(preset.n1) > 0 else 12)
|
||||||
|
amp = max(0, min(255, int(preset.n2) if int(preset.n2) > 0 else 180))
|
||||||
|
drift = max(1, int(preset.n3) if int(preset.n3) > 0 else 1)
|
||||||
|
phase = self.driver.step % 256
|
||||||
|
last = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
d = max(1, int(preset.d))
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
base = self.driver.apply_brightness(colors[0], preset.b)
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
x = (i * 256 // wavelength + phase) & 255
|
||||||
|
tri = 255 - abs(128 - x) * 2
|
||||||
|
s = (tri * amp) // 255
|
||||||
|
self.driver.n[i] = ((base[0]*s)//255, (base[1]*s)//255, (base[2]*s)//255)
|
||||||
|
self.driver.n.write()
|
||||||
|
phase = (phase + drift) % 256
|
||||||
|
self.driver.step = phase
|
||||||
|
last = utime.ticks_add(last, d)
|
||||||
|
if not preset.a:
|
||||||
|
yield
|
||||||
|
return
|
||||||
|
yield
|
||||||
@@ -19,7 +19,44 @@ class Preset:
|
|||||||
def edit(self, data=None):
|
def edit(self, data=None):
|
||||||
if not data:
|
if not data:
|
||||||
return False
|
return False
|
||||||
|
aliases = {
|
||||||
|
"pattern": "p",
|
||||||
|
"colors": "c",
|
||||||
|
"delay": "d",
|
||||||
|
"brightness": "b",
|
||||||
|
"auto": "a",
|
||||||
|
}
|
||||||
|
int_fields = {"d", "b", "n1", "n2", "n3", "n4", "n5", "n6"}
|
||||||
|
allowed_fields = {"p", "c", "d", "b", "a", "n1", "n2", "n3", "n4", "n5", "n6"}
|
||||||
for key, value in data.items():
|
for key, value in data.items():
|
||||||
|
key = aliases.get(key, key)
|
||||||
|
if key not in allowed_fields:
|
||||||
|
continue
|
||||||
|
if key in int_fields:
|
||||||
|
try:
|
||||||
|
parsed = int(value)
|
||||||
|
if key == "b":
|
||||||
|
parsed = max(0, min(255, parsed))
|
||||||
|
elif key in ("d", "n1", "n2", "n3", "n4", "n5", "n6"):
|
||||||
|
parsed = max(0, parsed)
|
||||||
|
setattr(self, key, parsed)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
continue
|
||||||
|
elif key == "a":
|
||||||
|
if isinstance(value, bool):
|
||||||
|
self.a = value
|
||||||
|
elif isinstance(value, int):
|
||||||
|
self.a = bool(value)
|
||||||
|
elif isinstance(value, str):
|
||||||
|
lowered = value.lower()
|
||||||
|
if lowered in ("true", "1", "yes", "on"):
|
||||||
|
self.a = True
|
||||||
|
elif lowered in ("false", "0", "no", "off"):
|
||||||
|
self.a = False
|
||||||
|
elif key == "c":
|
||||||
|
if isinstance(value, (list, tuple)):
|
||||||
|
self.c = value
|
||||||
|
else:
|
||||||
setattr(self, key, value)
|
setattr(self, key, value)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
{"14": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 102, 0]], "b": 255, "n2": 1000, "n1": 2000, "p": "pulse", "n3": 2000, "d": 800}, "15": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 0, 0], [0, 255, 0]], "b": 255, "n2": 0, "n1": 0, "p": "blink", "n3": 0, "d": 500}, "5": {"n5": 0, "n4": 1, "a": true, "n6": 0, "c": [[255, 0, 0], [0, 0, 255]], "b": 255, "n2": 5, "n1": 5, "p": "chase", "n3": 1, "d": 200}, "4": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 0, 0], [0, 255, 0], [0, 0, 255]], "b": 255, "n2": 0, "n1": 0, "p": "transition", "n3": 0, "d": 500}, "7": {"n5": 0, "n4": 5, "a": true, "n6": 0, "c": [[255, 165, 0], [128, 0, 128]], "b": 255, "n2": 10, "n1": 2, "p": "circle", "n3": 2, "d": 200}, "11": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 0, 0]], "b": 255, "n2": 0, "n1": 0, "p": "on", "n3": 0, "d": 100}, "12": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[0, 0, 255]], "b": 255, "n2": 0, "n1": 0, "p": "on", "n3": 0, "d": 100}, "6": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[0, 255, 0]], "b": 255, "n2": 500, "n1": 1000, "p": "pulse", "n3": 1000, "d": 500}, "3": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 255, 255]], "b": 255, "n2": 0, "n1": 2, "p": "rainbow", "n3": 0, "d": 100}, "2": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 255, 255]], "b": 0, "n2": 0, "n1": 0, "p": "off", "n3": 0, "d": 100}, "1": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 255, 255]], "b": 255, "n2": 0, "n1": 0, "p": "on", "n3": 0, "d": 100}, "10": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[230, 242, 255]], "b": 200, "n2": 0, "n1": 0, "p": "on", "n3": 0, "d": 100}, "13": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 255, 255]], "b": 255, "n2": 0, "n1": 1, "p": "rainbow", "n3": 0, "d": 150}, "9": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 245, 230]], "b": 200, "n2": 0, "n1": 0, "p": "on", "n3": 0, "d": 100}, "8": {"n5": 0, "n4": 0, "a": true, "n6": 0, "c": [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0]], "b": 255, "n2": 0, "n1": 0, "p": "blink", "n3": 0, "d": 1000}}
|
|
||||||
@@ -1,8 +1,15 @@
|
|||||||
from machine import Pin
|
from machine import Pin
|
||||||
from neopixel import NeoPixel
|
from neopixel import NeoPixel
|
||||||
from preset import Preset
|
from preset import Preset
|
||||||
from patterns import Blink, Rainbow, Pulse, Transition, Chase, Circle
|
from utils import convert_and_reorder_colors
|
||||||
import json
|
import json
|
||||||
|
import sys
|
||||||
|
try:
|
||||||
|
import uos as os
|
||||||
|
except ImportError:
|
||||||
|
import os
|
||||||
|
|
||||||
|
MAX_PRESETS = 32
|
||||||
|
|
||||||
|
|
||||||
class Presets:
|
class Presets:
|
||||||
@@ -17,17 +24,53 @@ class Presets:
|
|||||||
self.presets = {}
|
self.presets = {}
|
||||||
self.selected = None
|
self.selected = None
|
||||||
|
|
||||||
# Register all pattern methods
|
self.reload_patterns()
|
||||||
|
|
||||||
|
def reload_patterns(self):
|
||||||
|
# Register built-in methods first, then discovered pattern classes
|
||||||
self.patterns = {
|
self.patterns = {
|
||||||
"off": self.off,
|
"off": self.off,
|
||||||
"on": self.on,
|
"on": self.on,
|
||||||
"blink": Blink(self).run,
|
|
||||||
"rainbow": Rainbow(self).run,
|
|
||||||
"pulse": Pulse(self).run,
|
|
||||||
"transition": Transition(self).run,
|
|
||||||
"chase": Chase(self).run,
|
|
||||||
"circle": Circle(self).run,
|
|
||||||
}
|
}
|
||||||
|
self.patterns.update(self._load_dynamic_patterns())
|
||||||
|
|
||||||
|
def _load_dynamic_patterns(self):
|
||||||
|
loaded = {}
|
||||||
|
try:
|
||||||
|
files = os.listdir("patterns")
|
||||||
|
except OSError:
|
||||||
|
return loaded
|
||||||
|
|
||||||
|
for filename in files:
|
||||||
|
if not filename.endswith(".py") or filename in ("__init__.py", "main.py"):
|
||||||
|
continue
|
||||||
|
module_basename = filename[:-3]
|
||||||
|
module_name = "patterns." + module_basename
|
||||||
|
try:
|
||||||
|
if module_name in sys.modules:
|
||||||
|
del sys.modules[module_name]
|
||||||
|
module = __import__(module_name, None, None, ["*"])
|
||||||
|
except Exception as e:
|
||||||
|
print("Pattern import failed:", module_name, e)
|
||||||
|
continue
|
||||||
|
|
||||||
|
pattern_class = None
|
||||||
|
for attr_name in dir(module):
|
||||||
|
attr = getattr(module, attr_name)
|
||||||
|
# Pick the first class in the module that exposes run()
|
||||||
|
if isinstance(attr, type) and hasattr(attr, "run"):
|
||||||
|
pattern_class = attr
|
||||||
|
break
|
||||||
|
|
||||||
|
if pattern_class is None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
try:
|
||||||
|
loaded[module_basename] = pattern_class(self).run
|
||||||
|
except Exception as e:
|
||||||
|
print("Pattern init failed:", module_name, e)
|
||||||
|
|
||||||
|
return loaded
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
"""Save the presets to a file."""
|
"""Save the presets to a file."""
|
||||||
@@ -35,8 +78,13 @@ class Presets:
|
|||||||
json.dump({name: preset.to_dict() for name, preset in self.presets.items()}, f)
|
json.dump({name: preset.to_dict() for name, preset in self.presets.items()}, f)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def load(self):
|
def load(self, settings=None):
|
||||||
"""Load presets from a file."""
|
"""Load presets from a file.
|
||||||
|
|
||||||
|
`settings` is used to convert hex strings in `c` to RGB tuples and apply
|
||||||
|
the device's colour order (same as ESPNow receive). If omitted, RGB order
|
||||||
|
is assumed.
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
with open("presets.json", "r") as f:
|
with open("presets.json", "r") as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
@@ -46,10 +94,17 @@ class Presets:
|
|||||||
self.save()
|
self.save()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
order = settings if settings is not None else "rgb"
|
||||||
self.presets = {}
|
self.presets = {}
|
||||||
for name, preset_data in data.items():
|
for name, preset_data in data.items():
|
||||||
if "c" in preset_data:
|
if len(self.presets) >= MAX_PRESETS:
|
||||||
preset_data["c"] = [tuple(color) for color in preset_data["c"]]
|
print("Preset limit reached on load:", MAX_PRESETS)
|
||||||
|
break
|
||||||
|
color_key = "c" if "c" in preset_data else ("colors" if "colors" in preset_data else None)
|
||||||
|
if color_key is not None:
|
||||||
|
preset_data[color_key] = convert_and_reorder_colors(
|
||||||
|
preset_data[color_key], order
|
||||||
|
)
|
||||||
self.presets[name] = Preset(preset_data)
|
self.presets[name] = Preset(preset_data)
|
||||||
if self.presets:
|
if self.presets:
|
||||||
print("Loaded presets:")
|
print("Loaded presets:")
|
||||||
@@ -63,6 +118,9 @@ class Presets:
|
|||||||
# Update existing preset
|
# Update existing preset
|
||||||
self.presets[name].edit(data)
|
self.presets[name].edit(data)
|
||||||
else:
|
else:
|
||||||
|
if len(self.presets) >= MAX_PRESETS and name not in ("on", "off"):
|
||||||
|
print("Preset limit reached:", MAX_PRESETS)
|
||||||
|
return False
|
||||||
# Create new preset
|
# Create new preset
|
||||||
self.presets[name] = Preset(data)
|
self.presets[name] = Preset(data)
|
||||||
return True
|
return True
|
||||||
@@ -73,6 +131,12 @@ class Presets:
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def delete_all(self):
|
||||||
|
self.presets = {}
|
||||||
|
self.generator = None
|
||||||
|
self.selected = None
|
||||||
|
return True
|
||||||
|
|
||||||
def tick(self):
|
def tick(self):
|
||||||
if self.generator is None:
|
if self.generator is None:
|
||||||
return
|
return
|
||||||
@@ -80,6 +144,9 @@ class Presets:
|
|||||||
next(self.generator)
|
next(self.generator)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
self.generator = None
|
self.generator = None
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error in tick: {e}")
|
||||||
|
self.generator = None
|
||||||
|
|
||||||
def select(self, preset_name, step=None):
|
def select(self, preset_name, step=None):
|
||||||
# Auto-create simple built-in presets for common names on first use
|
# Auto-create simple built-in presets for common names on first use
|
||||||
@@ -100,6 +167,9 @@ class Presets:
|
|||||||
self.generator = self.patterns[preset.p](preset)
|
self.generator = self.patterns[preset.p](preset)
|
||||||
self.selected = preset_name # Store the preset name, not the object
|
self.selected = preset_name # Store the preset name, not the object
|
||||||
return True
|
return True
|
||||||
|
print("select failed: pattern not found for preset", preset_name, "pattern=", preset.p)
|
||||||
|
return False
|
||||||
|
print("select failed: preset not found", preset_name)
|
||||||
# If preset doesn't exist or pattern not found, indicate failure
|
# If preset doesn't exist or pattern not found, indicate failure
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|||||||
12
src/runtime_state.py
Normal file
12
src/runtime_state.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
class RuntimeState:
|
||||||
|
def __init__(self):
|
||||||
|
self.hello = True
|
||||||
|
self.ws_client_count = 0
|
||||||
|
|
||||||
|
def ws_connected(self):
|
||||||
|
self.ws_client_count += 1
|
||||||
|
self.hello = False
|
||||||
|
|
||||||
|
def ws_disconnected(self):
|
||||||
|
self.ws_client_count = max(0, self.ws_client_count - 1)
|
||||||
|
self.hello = self.ws_client_count == 0
|
||||||
@@ -12,15 +12,27 @@ class Settings(dict):
|
|||||||
self.color_order = self.get_color_order(self["color_order"])
|
self.color_order = self.get_color_order(self["color_order"])
|
||||||
|
|
||||||
def set_defaults(self):
|
def set_defaults(self):
|
||||||
|
|
||||||
self["led_pin"] = 10
|
self["led_pin"] = 10
|
||||||
self["num_leds"] = 119
|
self["num_leds"] = 119
|
||||||
|
|
||||||
self["color_order"] = "rgb"
|
self["color_order"] = "rgb"
|
||||||
self["name"] = "a"
|
|
||||||
|
sta = network.WLAN(network.STA_IF)
|
||||||
|
sta.active(True)
|
||||||
|
#use led-mac for name
|
||||||
|
mac = sta.config("mac")
|
||||||
|
mac = ubinascii.hexlify(mac).decode().lower()
|
||||||
|
self["name"] = "led-" + mac
|
||||||
|
|
||||||
self["debug"] = False
|
self["debug"] = False
|
||||||
self["default"] = "on"
|
self["default"] = "on"
|
||||||
self["brightness"] = 32
|
self["brightness"] = 32
|
||||||
|
self["transport_type"] = "espnow"
|
||||||
|
self["wifi_channel"] = 1
|
||||||
|
# ESP-NOW transport (requires espnow firmware; uses wifi_channel).
|
||||||
|
self["ssid"] = ""
|
||||||
|
self["password"] = ""
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
try:
|
try:
|
||||||
@@ -42,7 +54,6 @@ class Settings(dict):
|
|||||||
self.set_defaults()
|
self.set_defaults()
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
|
|
||||||
def get_color_order(self, color_order):
|
def get_color_order(self, color_order):
|
||||||
"""Convert color order string to tuple of hex string indices."""
|
"""Convert color order string to tuple of hex string indices."""
|
||||||
color_orders = {
|
color_orders = {
|
||||||
|
|||||||
53
src/startup.py
Normal file
53
src/startup.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import gc
|
||||||
|
import machine
|
||||||
|
import network
|
||||||
|
import utime
|
||||||
|
|
||||||
|
from presets import Presets
|
||||||
|
from settings import Settings
|
||||||
|
|
||||||
|
|
||||||
|
def initialize_runtime():
|
||||||
|
machine.freq(160000000)
|
||||||
|
|
||||||
|
settings = Settings()
|
||||||
|
print(settings)
|
||||||
|
|
||||||
|
wdt = machine.WDT(timeout=10000)
|
||||||
|
wdt.feed()
|
||||||
|
|
||||||
|
gc.collect()
|
||||||
|
print("mem before presets:", {"free": gc.mem_free(), "alloc": gc.mem_alloc()})
|
||||||
|
|
||||||
|
presets = Presets(settings["led_pin"], settings["num_leds"])
|
||||||
|
presets.load(settings)
|
||||||
|
presets.b = settings.get("brightness", 255)
|
||||||
|
presets.debug = bool(settings.get("debug", False))
|
||||||
|
gc.collect()
|
||||||
|
print("mem after presets:", {"free": gc.mem_free(), "alloc": gc.mem_alloc()})
|
||||||
|
|
||||||
|
default_preset = settings.get("default", "")
|
||||||
|
if default_preset and default_preset in presets.presets:
|
||||||
|
if presets.select(default_preset):
|
||||||
|
print("Selected startup preset:", default_preset)
|
||||||
|
else:
|
||||||
|
print("Startup preset failed (invalid pattern?):", default_preset)
|
||||||
|
|
||||||
|
# On ESP32-C3, soft reboots can leave Wi-Fi driver state allocated.
|
||||||
|
# Reset both interfaces and collect before bringing STA up.
|
||||||
|
ap_if = network.WLAN(network.AP_IF)
|
||||||
|
ap_if.active(False)
|
||||||
|
sta_if = network.WLAN(network.STA_IF)
|
||||||
|
if sta_if.active():
|
||||||
|
sta_if.active(False)
|
||||||
|
utime.sleep_ms(100)
|
||||||
|
gc.collect()
|
||||||
|
sta_if.active(True)
|
||||||
|
sta_if.config(pm=network.WLAN.PM_NONE)
|
||||||
|
sta_if.connect(settings["ssid"], settings["password"])
|
||||||
|
while not sta_if.isconnected():
|
||||||
|
utime.sleep(1)
|
||||||
|
wdt.feed()
|
||||||
|
|
||||||
|
print(sta_if.ifconfig())
|
||||||
|
return settings, presets, wdt, sta_if
|
||||||
23
src/utils.py
23
src/utils.py
@@ -33,21 +33,26 @@ def convert_and_reorder_colors(colors, settings_or_color_order):
|
|||||||
|
|
||||||
converted_colors = []
|
converted_colors = []
|
||||||
for color in colors:
|
for color in colors:
|
||||||
|
try:
|
||||||
# Convert "#RRGGBB" to (R, G, B)
|
# Convert "#RRGGBB" to (R, G, B)
|
||||||
if isinstance(color, str) and color.startswith("#"):
|
if isinstance(color, str) and color.startswith("#") and len(color) == 7:
|
||||||
r = int(color[1:3], 16)
|
r = int(color[1:3], 16)
|
||||||
g = int(color[3:5], 16)
|
g = int(color[3:5], 16)
|
||||||
b = int(color[5:7], 16)
|
b = int(color[5:7], 16)
|
||||||
rgb = (r, g, b)
|
rgb = (r, g, b)
|
||||||
|
elif isinstance(color, (list, tuple)) and len(color) == 3:
|
||||||
|
# Already a tuple/list, just coerce and clamp.
|
||||||
|
rgb = tuple(max(0, min(255, int(x))) for x in color)
|
||||||
|
else:
|
||||||
|
# Unknown format: ignore safely.
|
||||||
|
continue
|
||||||
|
|
||||||
# Reorder based on device color order
|
# Reorder based on device color order
|
||||||
reordered = (rgb[channel_order[0]], rgb[channel_order[1]], rgb[channel_order[2]])
|
reordered = (rgb[channel_order[0]], rgb[channel_order[1]], rgb[channel_order[2]])
|
||||||
converted_colors.append(reordered)
|
converted_colors.append(reordered)
|
||||||
elif isinstance(color, (list, tuple)) and len(color) == 3:
|
except (TypeError, ValueError, IndexError):
|
||||||
# Already a tuple/list, just reorder
|
# Skip malformed color entries to avoid crashing pattern loops.
|
||||||
rgb = tuple(color)
|
continue
|
||||||
reordered = (rgb[channel_order[0]], rgb[channel_order[1]], rgb[channel_order[2]])
|
if not converted_colors:
|
||||||
converted_colors.append(reordered)
|
converted_colors.append((255, 255, 255))
|
||||||
else:
|
|
||||||
# Keep as-is if not recognized format
|
|
||||||
converted_colors.append(color)
|
|
||||||
return converted_colors
|
return converted_colors
|
||||||
|
|||||||
292
tests/all.py
Normal file
292
tests/all.py
Normal file
@@ -0,0 +1,292 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Self-contained led-driver test runner for MicroPython/mpremote."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
from utils import convert_and_reorder_colors
|
||||||
|
|
||||||
|
|
||||||
|
class _TestContext:
|
||||||
|
def __init__(self):
|
||||||
|
self.settings = Settings()
|
||||||
|
self.settings["name"] = self.settings.get("name", "test_device")
|
||||||
|
self.presets = Presets(self.settings["led_pin"], self.settings["num_leds"])
|
||||||
|
self.presets.b = self.settings.get("brightness", 255)
|
||||||
|
self.wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
def tick_for_ms(self, duration_ms, sleep_ms=5):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < duration_ms:
|
||||||
|
self.wdt.feed()
|
||||||
|
run_tick(self.presets)
|
||||||
|
utime.sleep_ms(sleep_ms)
|
||||||
|
|
||||||
|
|
||||||
|
def _process_message(ctx, payload):
|
||||||
|
"""Small test helper that mirrors the main message handling logic."""
|
||||||
|
try:
|
||||||
|
if isinstance(payload, (bytes, bytearray)):
|
||||||
|
data = json.loads(payload)
|
||||||
|
elif isinstance(payload, str):
|
||||||
|
data = json.loads(payload)
|
||||||
|
else:
|
||||||
|
data = payload
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return "invalid_json"
|
||||||
|
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return "invalid_shape"
|
||||||
|
if data.get("v") != "1":
|
||||||
|
return "wrong_version"
|
||||||
|
|
||||||
|
if "b" in data:
|
||||||
|
try:
|
||||||
|
ctx.presets.b = max(0, min(255, int(data["b"])))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
if isinstance(data.get("presets"), dict):
|
||||||
|
for name, preset_data in data["presets"].items():
|
||||||
|
if not isinstance(preset_data, dict):
|
||||||
|
continue
|
||||||
|
color_key = "c" if "c" in preset_data else ("colors" if "colors" in preset_data else None)
|
||||||
|
if color_key is not None:
|
||||||
|
try:
|
||||||
|
preset_data[color_key] = convert_and_reorder_colors(
|
||||||
|
preset_data[color_key], ctx.settings
|
||||||
|
)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
continue
|
||||||
|
ctx.presets.edit(name, preset_data)
|
||||||
|
|
||||||
|
if isinstance(data.get("select"), dict) and ctx.settings.get("name") in data["select"]:
|
||||||
|
select_list = data["select"][ctx.settings.get("name")]
|
||||||
|
if isinstance(select_list, list) and select_list:
|
||||||
|
preset_name = select_list[0]
|
||||||
|
step = select_list[1] if len(select_list) > 1 else None
|
||||||
|
if isinstance(preset_name, str):
|
||||||
|
ctx.presets.select(preset_name, step=step)
|
||||||
|
|
||||||
|
if "default" in data:
|
||||||
|
default_name = data["default"]
|
||||||
|
this_device_name = ctx.settings.get("name")
|
||||||
|
this_device_name_norm = (
|
||||||
|
this_device_name.strip().lower()
|
||||||
|
if isinstance(this_device_name, str)
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
should_apply_default = True
|
||||||
|
if "targets" in data:
|
||||||
|
should_apply_default = False
|
||||||
|
targets = data.get("targets")
|
||||||
|
if isinstance(targets, list) and this_device_name_norm:
|
||||||
|
normalized_targets = [
|
||||||
|
target.strip().lower()
|
||||||
|
for target in targets
|
||||||
|
if isinstance(target, str) and target.strip()
|
||||||
|
]
|
||||||
|
should_apply_default = this_device_name_norm in normalized_targets
|
||||||
|
if (
|
||||||
|
should_apply_default
|
||||||
|
and
|
||||||
|
isinstance(default_name, str)
|
||||||
|
and default_name
|
||||||
|
and default_name in ctx.presets.presets
|
||||||
|
):
|
||||||
|
ctx.settings["default"] = default_name
|
||||||
|
|
||||||
|
if "save" in data:
|
||||||
|
ctx.presets.save()
|
||||||
|
|
||||||
|
return "ok"
|
||||||
|
|
||||||
|
|
||||||
|
def test_invalid_messages_do_not_crash():
|
||||||
|
ctx = _TestContext()
|
||||||
|
cases = [
|
||||||
|
b"{not-json",
|
||||||
|
"[]",
|
||||||
|
json.dumps({"v": "2"}),
|
||||||
|
json.dumps({"v": "1", "presets": ["bad"]}),
|
||||||
|
json.dumps({"v": "1", "select": {"test_device": "not-list"}}),
|
||||||
|
json.dumps({"v": "1", "presets": {"x": {"c": ["#GG0000"]}}}),
|
||||||
|
]
|
||||||
|
for payload in cases:
|
||||||
|
_process_message(ctx, payload)
|
||||||
|
ctx.wdt.feed()
|
||||||
|
|
||||||
|
|
||||||
|
def test_preset_edit_sanitization():
|
||||||
|
ctx = _TestContext()
|
||||||
|
ctx.presets.edit(
|
||||||
|
"sanitize",
|
||||||
|
{
|
||||||
|
"pattern": "blink",
|
||||||
|
"delay": "120",
|
||||||
|
"brightness": "999",
|
||||||
|
"auto": "false",
|
||||||
|
"n1": "-5",
|
||||||
|
"n2": "7",
|
||||||
|
"unknown_field": "ignored",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
p = ctx.presets.presets["sanitize"]
|
||||||
|
assert p.p == "blink"
|
||||||
|
assert p.d == 120
|
||||||
|
assert p.b == 255
|
||||||
|
assert p.a is False
|
||||||
|
assert p.n1 == 0
|
||||||
|
assert p.n2 == 7
|
||||||
|
assert not hasattr(p, "unknown_field")
|
||||||
|
|
||||||
|
|
||||||
|
def test_colour_conversion_and_transition():
|
||||||
|
ctx = _TestContext()
|
||||||
|
msg = {
|
||||||
|
"v": "1",
|
||||||
|
"presets": {
|
||||||
|
"fade": {
|
||||||
|
"p": "transition",
|
||||||
|
"c": ["#ff0000", "#00ff00"],
|
||||||
|
"d": 80,
|
||||||
|
"a": True,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"select": {ctx.settings["name"]: ["fade"]},
|
||||||
|
}
|
||||||
|
result = _process_message(ctx, msg)
|
||||||
|
assert result == "ok"
|
||||||
|
assert ctx.presets.selected == "fade"
|
||||||
|
# Smoke-run the generator to ensure math runs without type errors.
|
||||||
|
ctx.tick_for_ms(250)
|
||||||
|
|
||||||
|
|
||||||
|
def test_pattern_smoke():
|
||||||
|
ctx = _TestContext()
|
||||||
|
cases = {
|
||||||
|
"t_on": {"p": "on", "c": [(16, 8, 4)]},
|
||||||
|
"t_off": {"p": "off"},
|
||||||
|
"t_blink": {"p": "blink", "c": [(255, 0, 0)], "d": 20},
|
||||||
|
"t_rainbow": {"p": "rainbow", "d": 5, "n1": 2},
|
||||||
|
"t_pulse": {"p": "pulse", "c": [(255, 0, 0)], "n1": 20, "n2": 10, "n3": 20, "d": 10},
|
||||||
|
"t_transition": {"p": "transition", "c": [(255, 0, 0), (0, 0, 255)], "d": 30},
|
||||||
|
"t_chase": {"p": "chase", "c": [(255, 0, 0), (0, 0, 255)], "n1": 3, "n2": 2, "n3": 1, "n4": 1, "d": 20},
|
||||||
|
"t_circle": {"p": "circle", "c": [(255, 255, 0), (0, 0, 8)], "n1": 5, "n2": 10, "n3": 5, "n4": 2},
|
||||||
|
}
|
||||||
|
for name, data in cases.items():
|
||||||
|
ctx.presets.edit(name, data)
|
||||||
|
assert ctx.presets.select(name), "select failed: %s" % name
|
||||||
|
ctx.tick_for_ms(120)
|
||||||
|
|
||||||
|
|
||||||
|
def test_patterns_do_not_use_blocking_sleep():
|
||||||
|
pattern_dir = "patterns"
|
||||||
|
offenders = []
|
||||||
|
try:
|
||||||
|
files = os.listdir(pattern_dir)
|
||||||
|
except OSError:
|
||||||
|
raise AssertionError("patterns directory is missing")
|
||||||
|
|
||||||
|
for filename in files:
|
||||||
|
if not filename.endswith(".py") or filename in ("__init__.py", "main.py"):
|
||||||
|
continue
|
||||||
|
path = pattern_dir + "/" + filename
|
||||||
|
try:
|
||||||
|
with open(path, "r") as f:
|
||||||
|
src = f.read()
|
||||||
|
except OSError:
|
||||||
|
offenders.append(filename + " (unreadable)")
|
||||||
|
continue
|
||||||
|
|
||||||
|
if (
|
||||||
|
"utime.sleep(" in src
|
||||||
|
or "utime.sleep_ms(" in src
|
||||||
|
or "time.sleep(" in src
|
||||||
|
or "time.sleep_ms(" in src
|
||||||
|
):
|
||||||
|
offenders.append(filename)
|
||||||
|
|
||||||
|
assert not offenders, "blocking sleep found in patterns: %s" % ", ".join(offenders)
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_requires_existing_preset():
|
||||||
|
ctx = _TestContext()
|
||||||
|
_process_message(ctx, {"v": "1", "default": "missing"})
|
||||||
|
assert ctx.settings.get("default") != "missing"
|
||||||
|
|
||||||
|
ctx.presets.edit("exists", {"p": "on"})
|
||||||
|
_process_message(ctx, {"v": "1", "default": "exists"})
|
||||||
|
assert ctx.settings.get("default") == "exists"
|
||||||
|
|
||||||
|
def test_default_targets_gate_by_device_name():
|
||||||
|
ctx = _TestContext()
|
||||||
|
ctx.settings["name"] = "a"
|
||||||
|
ctx.presets.edit("targeted", {"p": "on"})
|
||||||
|
ctx.settings["default"] = "baseline"
|
||||||
|
|
||||||
|
_process_message(
|
||||||
|
ctx,
|
||||||
|
{"v": "1", "default": "targeted", "targets": ["11"]},
|
||||||
|
)
|
||||||
|
assert ctx.settings.get("default") == "baseline"
|
||||||
|
|
||||||
|
_process_message(
|
||||||
|
ctx,
|
||||||
|
{"v": "1", "default": "targeted", "targets": [" A "]},
|
||||||
|
)
|
||||||
|
assert ctx.settings.get("default") == "targeted"
|
||||||
|
|
||||||
|
|
||||||
|
def test_save_and_load_roundtrip():
|
||||||
|
ctx = _TestContext()
|
||||||
|
ctx.presets.edit(
|
||||||
|
"persist",
|
||||||
|
{"p": "blink", "c": [(1, 2, 3), (4, 5, 6)], "d": 77, "b": 123, "a": False},
|
||||||
|
)
|
||||||
|
assert ctx.presets.save()
|
||||||
|
|
||||||
|
reloaded = Presets(ctx.settings["led_pin"], ctx.settings["num_leds"])
|
||||||
|
assert reloaded.load(ctx.settings)
|
||||||
|
p = reloaded.presets.get("persist")
|
||||||
|
assert p is not None
|
||||||
|
assert p.p == "blink"
|
||||||
|
assert p.d == 77
|
||||||
|
assert p.b == 123
|
||||||
|
assert p.a is False
|
||||||
|
assert p.c == [(1, 2, 3), (4, 5, 6)]
|
||||||
|
|
||||||
|
try:
|
||||||
|
os.remove("presets.json")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def run_all():
|
||||||
|
tests = [
|
||||||
|
test_invalid_messages_do_not_crash,
|
||||||
|
test_preset_edit_sanitization,
|
||||||
|
test_colour_conversion_and_transition,
|
||||||
|
test_pattern_smoke,
|
||||||
|
test_patterns_do_not_use_blocking_sleep,
|
||||||
|
test_default_requires_existing_preset,
|
||||||
|
test_default_targets_gate_by_device_name,
|
||||||
|
test_save_and_load_roundtrip,
|
||||||
|
]
|
||||||
|
print("=" * 56)
|
||||||
|
print("led-driver self-contained tests")
|
||||||
|
print("=" * 56)
|
||||||
|
for test_func in tests:
|
||||||
|
print("Running %s ..." % test_func.__name__)
|
||||||
|
test_func()
|
||||||
|
print(" PASS")
|
||||||
|
print("-" * 56)
|
||||||
|
print("All tests passed")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
run_all()
|
||||||
40
tests/patterns/aurora.py
Normal file
40
tests/patterns/aurora.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
p.edit("test_aurora", {
|
||||||
|
"p": "aurora",
|
||||||
|
"b": 200,
|
||||||
|
"d": 60,
|
||||||
|
"c": [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
|
||||||
|
"n1": 4,
|
||||||
|
"n2": 2,
|
||||||
|
"n3": 120,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("test_aurora")
|
||||||
|
run_for(p, wdt, 3000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
import utime
|
import utime
|
||||||
from machine import WDT
|
from machine import WDT
|
||||||
from settings import Settings
|
from settings import Settings
|
||||||
from presets import Presets
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
def run_for(p, wdt, duration_ms):
|
def run_for(p, wdt, duration_ms):
|
||||||
@@ -10,7 +10,7 @@ def run_for(p, wdt, duration_ms):
|
|||||||
start = utime.ticks_ms()
|
start = utime.ticks_ms()
|
||||||
while utime.ticks_diff(utime.ticks_ms(), start) < duration_ms:
|
while utime.ticks_diff(utime.ticks_ms(), start) < duration_ms:
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
p.tick()
|
run_tick(p)
|
||||||
utime.sleep_ms(10)
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
@@ -52,7 +52,7 @@ def main():
|
|||||||
p.select("rainbow_manual")
|
p.select("rainbow_manual")
|
||||||
print("Calling tick() 5 times (should advance 5 steps)...")
|
print("Calling tick() 5 times (should advance 5 steps)...")
|
||||||
for i in range(5):
|
for i in range(5):
|
||||||
p.tick()
|
run_tick(p)
|
||||||
utime.sleep_ms(100) # Small delay to see changes
|
utime.sleep_ms(100) # Small delay to see changes
|
||||||
print(f" Tick {i+1}: generator={'active' if p.generator is not None else 'stopped'}")
|
print(f" Tick {i+1}: generator={'active' if p.generator is not None else 'stopped'}")
|
||||||
|
|
||||||
@@ -96,7 +96,7 @@ def main():
|
|||||||
tick_count = 0
|
tick_count = 0
|
||||||
max_ticks = 200 # Safety limit
|
max_ticks = 200 # Safety limit
|
||||||
while p.generator is not None and tick_count < max_ticks:
|
while p.generator is not None and tick_count < max_ticks:
|
||||||
p.tick()
|
run_tick(p)
|
||||||
tick_count += 1
|
tick_count += 1
|
||||||
utime.sleep_ms(10)
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
@@ -133,7 +133,7 @@ def main():
|
|||||||
tick_count = 0
|
tick_count = 0
|
||||||
max_ticks = 200
|
max_ticks = 200
|
||||||
while p.generator is not None and tick_count < max_ticks:
|
while p.generator is not None and tick_count < max_ticks:
|
||||||
p.tick()
|
run_tick(p)
|
||||||
tick_count += 1
|
tick_count += 1
|
||||||
utime.sleep_ms(10)
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
@@ -162,7 +162,7 @@ def main():
|
|||||||
|
|
||||||
print("Calling tick() 3 times in manual mode...")
|
print("Calling tick() 3 times in manual mode...")
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
p.tick()
|
run_tick(p)
|
||||||
utime.sleep_ms(100)
|
utime.sleep_ms(100)
|
||||||
print(f" Tick {i+1}: generator={'active' if p.generator is not None else 'stopped'}")
|
print(f" Tick {i+1}: generator={'active' if p.generator is not None else 'stopped'}")
|
||||||
|
|
||||||
@@ -178,7 +178,7 @@ def main():
|
|||||||
print("\nCleaning up...")
|
print("\nCleaning up...")
|
||||||
p.edit("cleanup_off", {"p": "off"})
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
p.select("cleanup_off")
|
p.select("cleanup_off")
|
||||||
p.tick()
|
run_tick(p)
|
||||||
utime.sleep_ms(100)
|
utime.sleep_ms(100)
|
||||||
|
|
||||||
print("\n" + "=" * 50)
|
print("\n" + "=" * 50)
|
||||||
40
tests/patterns/bar_graph.py
Normal file
40
tests/patterns/bar_graph.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
p.edit("test_bar_graph", {
|
||||||
|
"p": "bar_graph",
|
||||||
|
"b": 200,
|
||||||
|
"d": 60,
|
||||||
|
"c": [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
|
||||||
|
"n1": 4,
|
||||||
|
"n2": 2,
|
||||||
|
"n3": 120,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("test_bar_graph")
|
||||||
|
run_for(p, wdt, 3000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
import utime
|
import utime
|
||||||
from machine import WDT
|
from machine import WDT
|
||||||
from settings import Settings
|
from settings import Settings
|
||||||
from presets import Presets
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -25,7 +25,7 @@ def main():
|
|||||||
start = utime.ticks_ms()
|
start = utime.ticks_ms()
|
||||||
while utime.ticks_diff(utime.ticks_ms(), start) < 1500:
|
while utime.ticks_diff(utime.ticks_ms(), start) < 1500:
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
p.tick()
|
run_tick(p)
|
||||||
utime.sleep_ms(10)
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
40
tests/patterns/breathing_dual.py
Normal file
40
tests/patterns/breathing_dual.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
p.edit("test_breathing_dual", {
|
||||||
|
"p": "breathing_dual",
|
||||||
|
"b": 200,
|
||||||
|
"d": 60,
|
||||||
|
"c": [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
|
||||||
|
"n1": 4,
|
||||||
|
"n2": 2,
|
||||||
|
"n3": 120,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("test_breathing_dual")
|
||||||
|
run_for(p, wdt, 3000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
import utime
|
import utime
|
||||||
from machine import WDT
|
from machine import WDT
|
||||||
from settings import Settings
|
from settings import Settings
|
||||||
from presets import Presets
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
def run_for(p, wdt, ms):
|
def run_for(p, wdt, ms):
|
||||||
@@ -10,7 +10,7 @@ def run_for(p, wdt, ms):
|
|||||||
start = utime.ticks_ms()
|
start = utime.ticks_ms()
|
||||||
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
p.tick()
|
run_tick(p)
|
||||||
utime.sleep_ms(10)
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
@@ -123,7 +123,7 @@ def main():
|
|||||||
print(" Advancing pattern with 10 beats (select + tick)...")
|
print(" Advancing pattern with 10 beats (select + tick)...")
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
p.select("chase_manual") # Simulate beat - restarts generator
|
p.select("chase_manual") # Simulate beat - restarts generator
|
||||||
p.tick() # Advance one step
|
run_tick(p) # Advance one step
|
||||||
utime.sleep_ms(500) # Pause to see the pattern
|
utime.sleep_ms(500) # Pause to see the pattern
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
print(f" Beat {i+1}: step={p.step}")
|
print(f" Beat {i+1}: step={p.step}")
|
||||||
@@ -141,7 +141,7 @@ def main():
|
|||||||
p.step = 0
|
p.step = 0
|
||||||
initial_step = p.step
|
initial_step = p.step
|
||||||
p.select("chase_manual2")
|
p.select("chase_manual2")
|
||||||
p.tick()
|
run_tick(p)
|
||||||
final_step = p.step
|
final_step = p.step
|
||||||
print(f" Step updated from {initial_step} to {final_step} (expected: 1)")
|
print(f" Step updated from {initial_step} to {final_step} (expected: 1)")
|
||||||
if final_step == 1:
|
if final_step == 1:
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
import utime
|
import utime
|
||||||
from machine import WDT
|
from machine import WDT
|
||||||
from settings import Settings
|
from settings import Settings
|
||||||
from presets import Presets
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
def run_for(p, wdt, ms):
|
def run_for(p, wdt, ms):
|
||||||
@@ -10,7 +10,7 @@ def run_for(p, wdt, ms):
|
|||||||
start = utime.ticks_ms()
|
start = utime.ticks_ms()
|
||||||
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
p.tick()
|
run_tick(p)
|
||||||
utime.sleep_ms(10)
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
40
tests/patterns/clock_sweep.py
Normal file
40
tests/patterns/clock_sweep.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
p.edit("test_clock_sweep", {
|
||||||
|
"p": "clock_sweep",
|
||||||
|
"b": 200,
|
||||||
|
"d": 60,
|
||||||
|
"c": [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
|
||||||
|
"n1": 4,
|
||||||
|
"n2": 2,
|
||||||
|
"n3": 120,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("test_clock_sweep")
|
||||||
|
run_for(p, wdt, 3000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
40
tests/patterns/comet_dual.py
Normal file
40
tests/patterns/comet_dual.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
p.edit("test_comet_dual", {
|
||||||
|
"p": "comet_dual",
|
||||||
|
"b": 200,
|
||||||
|
"d": 60,
|
||||||
|
"c": [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
|
||||||
|
"n1": 4,
|
||||||
|
"n2": 2,
|
||||||
|
"n3": 120,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("test_comet_dual")
|
||||||
|
run_for(p, wdt, 3000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
40
tests/patterns/fireflies.py
Normal file
40
tests/patterns/fireflies.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
p.edit("test_fireflies", {
|
||||||
|
"p": "fireflies",
|
||||||
|
"b": 200,
|
||||||
|
"d": 60,
|
||||||
|
"c": [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
|
||||||
|
"n1": 4,
|
||||||
|
"n2": 2,
|
||||||
|
"n3": 120,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("test_fireflies")
|
||||||
|
run_for(p, wdt, 3000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
39
tests/patterns/gradient_scroll.py
Normal file
39
tests/patterns/gradient_scroll.py
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
print("Test gradient_scroll")
|
||||||
|
p.edit("gradient_test", {
|
||||||
|
"p": "gradient_scroll",
|
||||||
|
"b": 220,
|
||||||
|
"d": 60,
|
||||||
|
"c": [(255, 0, 0), (0, 255, 0), (0, 0, 255)],
|
||||||
|
"n1": 2,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("gradient_test")
|
||||||
|
run_for(p, wdt, 4000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
40
tests/patterns/heartbeat.py
Normal file
40
tests/patterns/heartbeat.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
p.edit("test_heartbeat", {
|
||||||
|
"p": "heartbeat",
|
||||||
|
"b": 200,
|
||||||
|
"d": 60,
|
||||||
|
"c": [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
|
||||||
|
"n1": 4,
|
||||||
|
"n2": 2,
|
||||||
|
"n3": 120,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("test_heartbeat")
|
||||||
|
run_for(p, wdt, 3000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
40
tests/patterns/marquee.py
Normal file
40
tests/patterns/marquee.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
p.edit("test_marquee", {
|
||||||
|
"p": "marquee",
|
||||||
|
"b": 200,
|
||||||
|
"d": 60,
|
||||||
|
"c": [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
|
||||||
|
"n1": 4,
|
||||||
|
"n2": 2,
|
||||||
|
"n3": 120,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("test_marquee")
|
||||||
|
run_for(p, wdt, 3000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
41
tests/patterns/meteor_rain.py
Normal file
41
tests/patterns/meteor_rain.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
print("Test meteor_rain")
|
||||||
|
p.edit("meteor_test", {
|
||||||
|
"p": "meteor_rain",
|
||||||
|
"b": 200,
|
||||||
|
"d": 40,
|
||||||
|
"c": [(255, 80, 0), (0, 120, 255)],
|
||||||
|
"n1": 10,
|
||||||
|
"n2": 1,
|
||||||
|
"n3": 200,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("meteor_test")
|
||||||
|
run_for(p, wdt, 4000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
import utime
|
import utime
|
||||||
from machine import WDT
|
from machine import WDT
|
||||||
from settings import Settings
|
from settings import Settings
|
||||||
from presets import Presets
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -20,7 +20,7 @@ def main():
|
|||||||
start = utime.ticks_ms()
|
start = utime.ticks_ms()
|
||||||
while utime.ticks_diff(utime.ticks_ms(), start) < 200:
|
while utime.ticks_diff(utime.ticks_ms(), start) < 200:
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
p.tick()
|
run_tick(p)
|
||||||
utime.sleep_ms(10)
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
import utime
|
import utime
|
||||||
from machine import WDT
|
from machine import WDT
|
||||||
from settings import Settings
|
from settings import Settings
|
||||||
from presets import Presets
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -29,7 +29,7 @@ def main():
|
|||||||
start = utime.ticks_ms()
|
start = utime.ticks_ms()
|
||||||
while utime.ticks_diff(utime.ticks_ms(), start) < 800:
|
while utime.ticks_diff(utime.ticks_ms(), start) < 800:
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
p.tick()
|
run_tick(p)
|
||||||
utime.sleep_ms(10)
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
# OFF phase
|
# OFF phase
|
||||||
@@ -37,7 +37,7 @@ def main():
|
|||||||
start = utime.ticks_ms()
|
start = utime.ticks_ms()
|
||||||
while utime.ticks_diff(utime.ticks_ms(), start) < 100:
|
while utime.ticks_diff(utime.ticks_ms(), start) < 100:
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
p.tick()
|
run_tick(p)
|
||||||
utime.sleep_ms(10)
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
40
tests/patterns/orbit.py
Normal file
40
tests/patterns/orbit.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
p.edit("test_orbit", {
|
||||||
|
"p": "orbit",
|
||||||
|
"b": 200,
|
||||||
|
"d": 60,
|
||||||
|
"c": [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
|
||||||
|
"n1": 4,
|
||||||
|
"n2": 2,
|
||||||
|
"n3": 120,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("test_orbit")
|
||||||
|
run_for(p, wdt, 3000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
40
tests/patterns/palette_morph.py
Normal file
40
tests/patterns/palette_morph.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
p.edit("test_palette_morph", {
|
||||||
|
"p": "palette_morph",
|
||||||
|
"b": 200,
|
||||||
|
"d": 60,
|
||||||
|
"c": [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
|
||||||
|
"n1": 4,
|
||||||
|
"n2": 2,
|
||||||
|
"n3": 120,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("test_palette_morph")
|
||||||
|
run_for(p, wdt, 3000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
40
tests/patterns/plasma.py
Normal file
40
tests/patterns/plasma.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
p.edit("test_plasma", {
|
||||||
|
"p": "plasma",
|
||||||
|
"b": 200,
|
||||||
|
"d": 60,
|
||||||
|
"c": [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
|
||||||
|
"n1": 4,
|
||||||
|
"n2": 2,
|
||||||
|
"n3": 120,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("test_plasma")
|
||||||
|
run_for(p, wdt, 3000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
import utime
|
import utime
|
||||||
from machine import WDT
|
from machine import WDT
|
||||||
from settings import Settings
|
from settings import Settings
|
||||||
from presets import Presets
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
def run_for(p, wdt, ms):
|
def run_for(p, wdt, ms):
|
||||||
@@ -10,7 +10,7 @@ def run_for(p, wdt, ms):
|
|||||||
start = utime.ticks_ms()
|
start = utime.ticks_ms()
|
||||||
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
p.tick()
|
run_tick(p)
|
||||||
utime.sleep_ms(10)
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
40
tests/patterns/rain_drops.py
Normal file
40
tests/patterns/rain_drops.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
p.edit("test_rain_drops", {
|
||||||
|
"p": "rain_drops",
|
||||||
|
"b": 200,
|
||||||
|
"d": 60,
|
||||||
|
"c": [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
|
||||||
|
"n1": 4,
|
||||||
|
"n2": 2,
|
||||||
|
"n3": 120,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("test_rain_drops")
|
||||||
|
run_for(p, wdt, 3000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
import utime
|
import utime
|
||||||
from machine import WDT
|
from machine import WDT
|
||||||
from settings import Settings
|
from settings import Settings
|
||||||
from presets import Presets
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
def run_for(p, wdt, ms):
|
def run_for(p, wdt, ms):
|
||||||
@@ -10,7 +10,7 @@ def run_for(p, wdt, ms):
|
|||||||
start = utime.ticks_ms()
|
start = utime.ticks_ms()
|
||||||
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
p.tick()
|
run_tick(p)
|
||||||
utime.sleep_ms(10)
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
@@ -81,7 +81,7 @@ def main():
|
|||||||
for i in range(10):
|
for i in range(10):
|
||||||
p.select("rainbow5")
|
p.select("rainbow5")
|
||||||
# One tick advances the generator one frame when auto=False
|
# One tick advances the generator one frame when auto=False
|
||||||
p.tick()
|
run_tick(p)
|
||||||
utime.sleep_ms(100)
|
utime.sleep_ms(100)
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
|
|
||||||
@@ -94,7 +94,7 @@ def main():
|
|||||||
})
|
})
|
||||||
initial_step = p.step
|
initial_step = p.step
|
||||||
p.select("rainbow6")
|
p.select("rainbow6")
|
||||||
p.tick()
|
run_tick(p)
|
||||||
final_step = p.step
|
final_step = p.step
|
||||||
print(f"Step updated from {initial_step} to {final_step} (expected increment: 1)")
|
print(f"Step updated from {initial_step} to {final_step} (expected increment: 1)")
|
||||||
|
|
||||||
@@ -130,7 +130,7 @@ def main():
|
|||||||
p.step = 0
|
p.step = 0
|
||||||
initial_step = p.step
|
initial_step = p.step
|
||||||
p.select("rainbow9")
|
p.select("rainbow9")
|
||||||
p.tick()
|
run_tick(p)
|
||||||
final_step = p.step
|
final_step = p.step
|
||||||
expected_step = (initial_step + 5) % 256
|
expected_step = (initial_step + 5) % 256
|
||||||
print(f"Step updated from {initial_step} to {final_step} (expected: {expected_step})")
|
print(f"Step updated from {initial_step} to {final_step} (expected: {expected_step})")
|
||||||
40
tests/patterns/scanner.py
Normal file
40
tests/patterns/scanner.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
print("Test scanner")
|
||||||
|
p.edit("scanner_test", {
|
||||||
|
"p": "scanner",
|
||||||
|
"b": 255,
|
||||||
|
"d": 30,
|
||||||
|
"c": [(255, 0, 0)],
|
||||||
|
"n1": 4,
|
||||||
|
"n2": 2,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("scanner_test")
|
||||||
|
run_for(p, wdt, 4000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
40
tests/patterns/segment_chase.py
Normal file
40
tests/patterns/segment_chase.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
p.edit("test_segment_chase", {
|
||||||
|
"p": "segment_chase",
|
||||||
|
"b": 200,
|
||||||
|
"d": 60,
|
||||||
|
"c": [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
|
||||||
|
"n1": 4,
|
||||||
|
"n2": 2,
|
||||||
|
"n3": 120,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("test_segment_chase")
|
||||||
|
run_for(p, wdt, 3000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
40
tests/patterns/snowfall.py
Normal file
40
tests/patterns/snowfall.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
p.edit("test_snowfall", {
|
||||||
|
"p": "snowfall",
|
||||||
|
"b": 200,
|
||||||
|
"d": 60,
|
||||||
|
"c": [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
|
||||||
|
"n1": 4,
|
||||||
|
"n2": 2,
|
||||||
|
"n3": 120,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("test_snowfall")
|
||||||
|
run_for(p, wdt, 3000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
40
tests/patterns/sparkle_trail.py
Normal file
40
tests/patterns/sparkle_trail.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
p.edit("test_sparkle_trail", {
|
||||||
|
"p": "sparkle_trail",
|
||||||
|
"b": 200,
|
||||||
|
"d": 60,
|
||||||
|
"c": [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
|
||||||
|
"n1": 4,
|
||||||
|
"n2": 2,
|
||||||
|
"n3": 120,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("test_sparkle_trail")
|
||||||
|
run_for(p, wdt, 3000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
40
tests/patterns/strobe_burst.py
Normal file
40
tests/patterns/strobe_burst.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
p.edit("test_strobe_burst", {
|
||||||
|
"p": "strobe_burst",
|
||||||
|
"b": 200,
|
||||||
|
"d": 60,
|
||||||
|
"c": [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
|
||||||
|
"n1": 4,
|
||||||
|
"n2": 2,
|
||||||
|
"n3": 120,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("test_strobe_burst")
|
||||||
|
run_for(p, wdt, 3000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
import utime
|
import utime
|
||||||
from machine import WDT
|
from machine import WDT
|
||||||
from settings import Settings
|
from settings import Settings
|
||||||
from presets import Presets
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
def run_for(p, wdt, ms):
|
def run_for(p, wdt, ms):
|
||||||
@@ -10,7 +10,7 @@ def run_for(p, wdt, ms):
|
|||||||
start = utime.ticks_ms()
|
start = utime.ticks_ms()
|
||||||
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
p.tick()
|
run_tick(p)
|
||||||
utime.sleep_ms(10)
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
40
tests/patterns/wave.py
Normal file
40
tests/patterns/wave.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets, run_tick
|
||||||
|
|
||||||
|
|
||||||
|
def run_for(p, wdt, ms):
|
||||||
|
start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
|
||||||
|
wdt.feed()
|
||||||
|
run_tick(p)
|
||||||
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
p.edit("test_wave", {
|
||||||
|
"p": "wave",
|
||||||
|
"b": 200,
|
||||||
|
"d": 60,
|
||||||
|
"c": [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
|
||||||
|
"n1": 4,
|
||||||
|
"n2": 2,
|
||||||
|
"n3": 120,
|
||||||
|
"a": True,
|
||||||
|
})
|
||||||
|
p.select("test_wave")
|
||||||
|
run_for(p, wdt, 3000)
|
||||||
|
|
||||||
|
p.edit("cleanup_off", {"p": "off"})
|
||||||
|
p.select("cleanup_off")
|
||||||
|
run_for(p, wdt, 100)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
25
tests/peers.py
Normal file
25
tests/peers.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
from espnow import ESPNow
|
||||||
|
import network
|
||||||
|
|
||||||
|
sta = network.WLAN(network.STA_IF)
|
||||||
|
sta.active(True)
|
||||||
|
|
||||||
|
espnow = ESPNow()
|
||||||
|
espnow.active(True)
|
||||||
|
|
||||||
|
# add_peer() expects a 6-byte MAC (bytes/bytearray), not integers.
|
||||||
|
# Unicast placeholders (not broadcast/multicast) so get_peers() lists them.
|
||||||
|
# PEERS = aa:aa:aa:aa:aa:START … aa:aa:aa:aa:aa:END (inclusive last octet).
|
||||||
|
_PREFIX = b"\xaa\xaa\xaa\xaa\xaa"
|
||||||
|
_START_LAST_OCTET = 1
|
||||||
|
_END_LAST_OCTET = 40
|
||||||
|
PEERS = tuple(_PREFIX + bytes((i,)) for i in range(_START_LAST_OCTET, _END_LAST_OCTET + 1))
|
||||||
|
for peer in PEERS:
|
||||||
|
espnow.add_peer(peer)
|
||||||
|
|
||||||
|
print("peers:", PEERS)
|
||||||
|
|
||||||
|
for peer in PEERS:
|
||||||
|
espnow.send(peer, b"Hello, world!")
|
||||||
|
|
||||||
|
print(espnow.get_peers())
|
||||||
41
tests/test_ap_pm0.py
Normal file
41
tests/test_ap_pm0.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""MicroPython AP example with power management disabled (pm=0).
|
||||||
|
|
||||||
|
Run on device:
|
||||||
|
mpremote connect /dev/ttyACM0 run tests/test_ap_pm0.py
|
||||||
|
"""
|
||||||
|
|
||||||
|
import network
|
||||||
|
import time
|
||||||
|
|
||||||
|
AP_SSID = "led-ap"
|
||||||
|
AP_PASSWORD = "ledpass123"
|
||||||
|
AP_CHANNEL = 6
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
ap = network.WLAN(network.AP_IF)
|
||||||
|
ap.active(True)
|
||||||
|
|
||||||
|
# Explicitly disable Wi-Fi power save for AP mode.
|
||||||
|
try:
|
||||||
|
ap.config(pm=0)
|
||||||
|
except (AttributeError, ValueError, TypeError):
|
||||||
|
try:
|
||||||
|
ap.config(pm=network.WLAN.PM_NONE)
|
||||||
|
except (AttributeError, ValueError, TypeError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
ap.config(essid=AP_SSID, password=AP_PASSWORD, channel=AP_CHANNEL, authmode=3)
|
||||||
|
|
||||||
|
print("[ap-pm0] AP active:", ap.active())
|
||||||
|
print("[ap-pm0] SSID:", AP_SSID)
|
||||||
|
print("[ap-pm0] IFCONFIG:", ap.ifconfig())
|
||||||
|
print("[ap-pm0] Waiting for clients. Ctrl+C to stop.")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -4,7 +4,7 @@ import json
|
|||||||
import os
|
import os
|
||||||
import utime
|
import utime
|
||||||
from settings import Settings
|
from settings import Settings
|
||||||
from presets import Presets
|
from presets import Presets, run_tick
|
||||||
from utils import convert_and_reorder_colors
|
from utils import convert_and_reorder_colors
|
||||||
|
|
||||||
|
|
||||||
@@ -54,7 +54,7 @@ def run_main_loop_iterations(espnow, patterns, settings, wdt, max_iterations=10)
|
|||||||
|
|
||||||
while iterations < max_iterations:
|
while iterations < max_iterations:
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
patterns.tick()
|
run_tick(patterns)
|
||||||
|
|
||||||
if espnow.any():
|
if espnow.any():
|
||||||
host, msg = espnow.recv()
|
host, msg = espnow.recv()
|
||||||
@@ -363,7 +363,7 @@ def test_switch_presets():
|
|||||||
start = utime.ticks_ms()
|
start = utime.ticks_ms()
|
||||||
while utime.ticks_diff(utime.ticks_ms(), start) < 2000:
|
while utime.ticks_diff(utime.ticks_ms(), start) < 2000:
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
patterns.tick()
|
run_tick(patterns)
|
||||||
utime.sleep_ms(10)
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
# Switch to second preset and run for 2 seconds
|
# Switch to second preset and run for 2 seconds
|
||||||
@@ -381,7 +381,7 @@ def test_switch_presets():
|
|||||||
start = utime.ticks_ms()
|
start = utime.ticks_ms()
|
||||||
while utime.ticks_diff(utime.ticks_ms(), start) < 2000:
|
while utime.ticks_diff(utime.ticks_ms(), start) < 2000:
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
patterns.tick()
|
run_tick(patterns)
|
||||||
utime.sleep_ms(10)
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
# Switch to third preset and run for 2 seconds
|
# Switch to third preset and run for 2 seconds
|
||||||
@@ -399,7 +399,7 @@ def test_switch_presets():
|
|||||||
start = utime.ticks_ms()
|
start = utime.ticks_ms()
|
||||||
while utime.ticks_diff(utime.ticks_ms(), start) < 2000:
|
while utime.ticks_diff(utime.ticks_ms(), start) < 2000:
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
patterns.tick()
|
run_tick(patterns)
|
||||||
utime.sleep_ms(10)
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
# Switch back to first preset and run for 2 seconds
|
# Switch back to first preset and run for 2 seconds
|
||||||
@@ -417,7 +417,7 @@ def test_switch_presets():
|
|||||||
start = utime.ticks_ms()
|
start = utime.ticks_ms()
|
||||||
while utime.ticks_diff(utime.ticks_ms(), start) < 2000:
|
while utime.ticks_diff(utime.ticks_ms(), start) < 2000:
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
patterns.tick()
|
run_tick(patterns)
|
||||||
utime.sleep_ms(10)
|
utime.sleep_ms(10)
|
||||||
|
|
||||||
print(" ✓ Preset switching works correctly")
|
print(" ✓ Preset switching works correctly")
|
||||||
@@ -577,7 +577,7 @@ def test_select_with_step():
|
|||||||
mock_espnow.send_message(b"\xbb\xbb\xbb\xbb\xbb\xbb", msg2)
|
mock_espnow.send_message(b"\xbb\xbb\xbb\xbb\xbb\xbb", msg2)
|
||||||
run_main_loop_iterations(mock_espnow, patterns, settings, wdt, max_iterations=2)
|
run_main_loop_iterations(mock_espnow, patterns, settings, wdt, max_iterations=2)
|
||||||
# Ensure tick() is called after select() to advance the step
|
# Ensure tick() is called after select() to advance the step
|
||||||
patterns.tick()
|
run_tick(patterns)
|
||||||
|
|
||||||
assert patterns.selected == "step_preset", "Should select step_preset"
|
assert patterns.selected == "step_preset", "Should select step_preset"
|
||||||
# Step is set to 10, then tick() advances it, so it should be 11
|
# Step is set to 10, then tick() advances it, so it should be 11
|
||||||
@@ -596,7 +596,7 @@ def test_select_with_step():
|
|||||||
initial_step = patterns.step # Should be 11
|
initial_step = patterns.step # Should be 11
|
||||||
run_main_loop_iterations(mock_espnow, patterns, settings, wdt, max_iterations=2)
|
run_main_loop_iterations(mock_espnow, patterns, settings, wdt, max_iterations=2)
|
||||||
# Ensure tick() is called after select() to advance the step
|
# Ensure tick() is called after select() to advance the step
|
||||||
patterns.tick()
|
run_tick(patterns)
|
||||||
# Since it's the same preset, step should not be reset, but tick() will advance it
|
# Since it's the same preset, step should not be reset, but tick() will advance it
|
||||||
# So step should be initial_step + 1 (one tick call)
|
# So step should be initial_step + 1 (one tick call)
|
||||||
assert patterns.step == initial_step + 1, f"Step should advance from {initial_step} to {initial_step + 1} (not reset), got {patterns.step}"
|
assert patterns.step == initial_step + 1, f"Step should advance from {initial_step} to {initial_step + 1} (not reset), got {patterns.step}"
|
||||||
@@ -614,7 +614,7 @@ def test_select_with_step():
|
|||||||
mock_espnow.send_message(b"\xdd\xdd\xdd\xdd\xdd\xdd", msg4)
|
mock_espnow.send_message(b"\xdd\xdd\xdd\xdd\xdd\xdd", msg4)
|
||||||
run_main_loop_iterations(mock_espnow, patterns, settings, wdt, max_iterations=2)
|
run_main_loop_iterations(mock_espnow, patterns, settings, wdt, max_iterations=2)
|
||||||
# Ensure tick() is called after select() to advance the step
|
# Ensure tick() is called after select() to advance the step
|
||||||
patterns.tick()
|
run_tick(patterns)
|
||||||
|
|
||||||
assert patterns.selected == "other_preset", "Should select other_preset"
|
assert patterns.selected == "other_preset", "Should select other_preset"
|
||||||
# Step is set to 5, then tick() advances it, so it should be 6
|
# Step is set to 5, then tick() advances it, so it should be 6
|
||||||
@@ -644,7 +644,7 @@ def test_preset_save_load():
|
|||||||
assert patterns.save(), "Save should return True"
|
assert patterns.save(), "Save should return True"
|
||||||
|
|
||||||
reloaded = Presets(settings["led_pin"], settings["num_leds"])
|
reloaded = Presets(settings["led_pin"], settings["num_leds"])
|
||||||
assert reloaded.load(), "Load should return True"
|
assert reloaded.load(settings), "Load should return True"
|
||||||
|
|
||||||
preset = reloaded.presets.get("saved_preset")
|
preset = reloaded.presets.get("saved_preset")
|
||||||
assert preset is not None, "Preset should be loaded"
|
assert preset is not None, "Preset should be loaded"
|
||||||
239
tests/test_mdns.py
Normal file
239
tests/test_mdns.py
Normal file
@@ -0,0 +1,239 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""mDNS smoke test — runs on the MicroPython device (ESP32).
|
||||||
|
|
||||||
|
Loads Wi-Fi credentials from /settings.json via Settings (same as main firmware).
|
||||||
|
Sets the network hostname before connect so the chip can advertise as <hostname>.local
|
||||||
|
on builds where mDNS is enabled (see network.hostname() in MicroPython docs).
|
||||||
|
|
||||||
|
By default the script stays connected until you stop it (reset or mpremote Ctrl+C) so you
|
||||||
|
can ping the mDNS name from another machine (e.g. name "a" -> leda.local; hyphens are omitted
|
||||||
|
in the hostname because ESP32 mDNS often breaks on '-').
|
||||||
|
|
||||||
|
After flashing, do a full hardware reset once so the first DHCP sees the new hostname.
|
||||||
|
|
||||||
|
Deploy src to the device (including utils.py with mdns_hostname), then from the host:
|
||||||
|
|
||||||
|
mpremote connect PORT run tests/test_mdns.py
|
||||||
|
|
||||||
|
Copy ``utils.py`` from ``src/`` onto the device if imports fail.
|
||||||
|
|
||||||
|
Or with cwd led-driver:
|
||||||
|
|
||||||
|
mpremote connect /dev/ttyUSB0 run tests/test_mdns.py
|
||||||
|
"""
|
||||||
|
|
||||||
|
import time
|
||||||
|
import network
|
||||||
|
import socket
|
||||||
|
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
|
||||||
|
from settings import Settings
|
||||||
|
from utils import mdns_hostname
|
||||||
|
|
||||||
|
CONNECT_TIMEOUT_S = 45
|
||||||
|
# ESP32 MicroPython WDT timeout is capped (typically 10000 ms). Longer blocking work
|
||||||
|
# (PHY calibration) runs with no WDT; WDT is only used in HOLD_S.
|
||||||
|
WDT_TIMEOUT_MS = 10000
|
||||||
|
# socket.getaddrinfo("<self>.local", …) often hangs a long time or indefinitely on ESP32; off by default.
|
||||||
|
SELF_LOCAL_GETADDRINFO = False
|
||||||
|
# After checks: 0 = exit immediately; >0 = stay up that many seconds; -1 = until reset/Ctrl+C (for remote ping).
|
||||||
|
HOLD_S = -1
|
||||||
|
# Set False to silence [mdns-test] timing lines (phase labels + elapsed ms since test start).
|
||||||
|
DEBUG = True
|
||||||
|
|
||||||
|
|
||||||
|
def _dbg(t0, msg):
|
||||||
|
if not DEBUG:
|
||||||
|
return
|
||||||
|
ms = utime.ticks_diff(utime.ticks_ms(), t0)
|
||||||
|
print("[mdns-test +%dms] %s" % (ms, msg))
|
||||||
|
|
||||||
|
|
||||||
|
def _set_hostname(h, sta):
|
||||||
|
"""Apply hostname on this STA object before active(True) / connect (DHCP + mDNS)."""
|
||||||
|
try:
|
||||||
|
network.hostname(h)
|
||||||
|
how = "network.hostname"
|
||||||
|
except (AttributeError, ValueError, OSError) as e:
|
||||||
|
how = None
|
||||||
|
last = e
|
||||||
|
try:
|
||||||
|
sta.config(hostname=h)
|
||||||
|
how = how or "WLAN.config(hostname=)"
|
||||||
|
except (AttributeError, ValueError, OSError) as e:
|
||||||
|
if how is None:
|
||||||
|
last = e
|
||||||
|
if how:
|
||||||
|
return how
|
||||||
|
print("Warning: could not set hostname (%s); mDNS name may be default." % last)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _sta_ip(sta):
|
||||||
|
try:
|
||||||
|
pair = sta.ipconfig("addr4")
|
||||||
|
if isinstance(pair, tuple) and pair:
|
||||||
|
return pair[0].split("/")[0] if isinstance(pair[0], str) else str(pair[0])
|
||||||
|
except (AttributeError, OSError, TypeError, ValueError):
|
||||||
|
pass
|
||||||
|
return sta.ifconfig()[0]
|
||||||
|
|
||||||
|
|
||||||
|
def _wait_wifi(sta, timeout_s, wdt, t0):
|
||||||
|
"""Wait for connection. If wdt is set, feed each iteration (keep gap < WDT_TIMEOUT_MS)."""
|
||||||
|
deadline = utime.ticks_add(utime.ticks_ms(), int(timeout_s * 1000))
|
||||||
|
n = 0
|
||||||
|
while not sta.isconnected():
|
||||||
|
if utime.ticks_diff(deadline, utime.ticks_ms()) <= 0:
|
||||||
|
_dbg(t0, "WiFi wait TIMEOUT after %d iterations, status=%s" % (n, sta.status()))
|
||||||
|
return False
|
||||||
|
st = sta.status()
|
||||||
|
n += 1
|
||||||
|
if DEBUG:
|
||||||
|
_dbg(t0, "WiFi wait iter #%d status=%s" % (n, st))
|
||||||
|
else:
|
||||||
|
print("WiFi status:", st, "(waiting)")
|
||||||
|
if wdt is not None:
|
||||||
|
wdt.feed()
|
||||||
|
time.sleep(1)
|
||||||
|
if wdt is not None:
|
||||||
|
wdt.feed()
|
||||||
|
_dbg(t0, "WiFi connected after %d wait iterations" % n)
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def _try_resolve_local(hostname, t0):
|
||||||
|
"""Best-effort: resolve our own *.local via getaddrinfo (often blocks a very long time on ESP32)."""
|
||||||
|
fqdn = hostname + ".local"
|
||||||
|
_dbg(t0, "getaddrinfo(%r) starting (may block a long time)" % fqdn)
|
||||||
|
t_gai = utime.ticks_ms()
|
||||||
|
try:
|
||||||
|
ai = socket.getaddrinfo(fqdn, 80)
|
||||||
|
dt = utime.ticks_diff(utime.ticks_ms(), t_gai)
|
||||||
|
print("getaddrinfo(%r) -> %s" % (fqdn, ai))
|
||||||
|
_dbg(t0, "getaddrinfo OK (call took %dms)" % dt)
|
||||||
|
return True
|
||||||
|
except OSError as e:
|
||||||
|
dt = utime.ticks_diff(utime.ticks_ms(), t_gai)
|
||||||
|
print("getaddrinfo(%r) failed: %s" % (fqdn, e))
|
||||||
|
_dbg(t0, "getaddrinfo OSError after %dms" % dt)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
t0 = utime.ticks_ms()
|
||||||
|
_dbg(t0, "start")
|
||||||
|
|
||||||
|
settings = Settings()
|
||||||
|
_dbg(t0, "Settings() loaded")
|
||||||
|
ssid = settings.get("ssid") or ""
|
||||||
|
password = settings.get("password") or ""
|
||||||
|
if not ssid:
|
||||||
|
print("mDNS test skipped: ssid empty in settings.json (configure Wi-Fi to run this test).")
|
||||||
|
raise SystemExit(0)
|
||||||
|
|
||||||
|
hostname = mdns_hostname(settings)
|
||||||
|
_dbg(t0, "mdns_hostname -> %r" % hostname)
|
||||||
|
sta = network.WLAN(network.STA_IF)
|
||||||
|
how = _set_hostname(hostname, sta)
|
||||||
|
if how:
|
||||||
|
print("Hostname set via %s: %r" % (how, hostname))
|
||||||
|
_dbg(t0, "set_hostname done (%s)" % (how or "failed"))
|
||||||
|
|
||||||
|
_dbg(t0, "before sta.active(True) (often slow: RF calibration)")
|
||||||
|
print("WiFi active(True) (can take a while for calibration)...")
|
||||||
|
sta.active(True)
|
||||||
|
_dbg(t0, "after sta.active(True)")
|
||||||
|
try:
|
||||||
|
sta.config(pm=network.WLAN.PM_NONE)
|
||||||
|
_dbg(t0, "sta.config(pm=PM_NONE) OK")
|
||||||
|
except (AttributeError, ValueError, TypeError) as e:
|
||||||
|
_dbg(t0, "sta.config(pm=PM_NONE) skipped: %s" % e)
|
||||||
|
|
||||||
|
print("Connecting to SSID %r ..." % ssid)
|
||||||
|
_dbg(t0, "before sta.connect()")
|
||||||
|
sta.connect(ssid, password)
|
||||||
|
_dbg(t0, "after sta.connect() (returned; association may still be in progress)")
|
||||||
|
# No WDT during calibration/wait/getaddrinfo — they can block longer than WDT_TIMEOUT_MS.
|
||||||
|
if not _wait_wifi(sta, CONNECT_TIMEOUT_S, None, t0):
|
||||||
|
print("Timeout: not connected. status=", sta.status())
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
ip = _sta_ip(sta)
|
||||||
|
print("WiFi OK, IP:", ip)
|
||||||
|
try:
|
||||||
|
stack_host = network.hostname()
|
||||||
|
except (AttributeError, ValueError, TypeError, OSError):
|
||||||
|
stack_host = None
|
||||||
|
if stack_host:
|
||||||
|
print(
|
||||||
|
"mDNS: use what the stack reports — ping %s.local (avahi-resolve -n %s.local)"
|
||||||
|
% (stack_host, stack_host)
|
||||||
|
)
|
||||||
|
if str(stack_host) != str(hostname):
|
||||||
|
print(
|
||||||
|
"(We asked for %r but stack reports %r — ping the stack name; cold boot may help.)"
|
||||||
|
% (hostname, stack_host)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
print("From another machine: ping %s.local" % hostname)
|
||||||
|
print("(or: avahi-resolve -n %s.local)" % hostname)
|
||||||
|
|
||||||
|
if SELF_LOCAL_GETADDRINFO:
|
||||||
|
_try_resolve_local(hostname, t0)
|
||||||
|
else:
|
||||||
|
_dbg(
|
||||||
|
t0,
|
||||||
|
"skip getaddrinfo(%s.local): SELF_LOCAL_GETADDRINFO=False (on-device self-.local lookup often hangs)"
|
||||||
|
% hostname,
|
||||||
|
)
|
||||||
|
print(
|
||||||
|
"Skipped on-device getaddrinfo(*.local); verify mDNS from a PC (ping above). "
|
||||||
|
"Set SELF_LOCAL_GETADDRINFO = True to attempt (may hang)."
|
||||||
|
)
|
||||||
|
|
||||||
|
if HOLD_S != 0:
|
||||||
|
forever = HOLD_S < 0
|
||||||
|
_dbg(
|
||||||
|
t0,
|
||||||
|
"starting WDT(%dms) + hold %s"
|
||||||
|
% (WDT_TIMEOUT_MS, "forever" if forever else ("%ds" % HOLD_S)),
|
||||||
|
)
|
||||||
|
wdt = WDT(timeout=WDT_TIMEOUT_MS)
|
||||||
|
wdt.feed()
|
||||||
|
if forever:
|
||||||
|
ping_target = stack_host or hostname
|
||||||
|
print(
|
||||||
|
"Staying online until you stop (reset device or mpremote Ctrl+C). "
|
||||||
|
"From another host: ping %s.local" % ping_target
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
print("Keeping connection up for %d s (Ctrl+C or reset to stop) ..." % HOLD_S)
|
||||||
|
end = None if forever else utime.ticks_add(utime.ticks_ms(), HOLD_S * 1000)
|
||||||
|
hold_i = 0
|
||||||
|
while True:
|
||||||
|
wdt.feed()
|
||||||
|
time.sleep(2)
|
||||||
|
hold_i += 1
|
||||||
|
if not sta.isconnected():
|
||||||
|
print("lost WiFi connection")
|
||||||
|
break
|
||||||
|
if forever:
|
||||||
|
if DEBUG and hold_i % 15 == 0:
|
||||||
|
_dbg(t0, "hold alive #%d IP %s" % (hold_i, _sta_ip(sta)))
|
||||||
|
else:
|
||||||
|
_dbg(t0, "hold tick #%d" % hold_i)
|
||||||
|
print("still connected, IP", _sta_ip(sta))
|
||||||
|
if utime.ticks_diff(end, utime.ticks_ms()) <= 0:
|
||||||
|
break
|
||||||
|
|
||||||
|
_dbg(t0, "hold loop finished")
|
||||||
|
|
||||||
|
_dbg(t0, "Done.")
|
||||||
|
print("Done.")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
102
tests/test_wifi.py
Normal file
102
tests/test_wifi.py
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Wi-Fi connection smoke test for MicroPython on ESP32.
|
||||||
|
|
||||||
|
Runs on-device via mpremote and uses /settings.json credentials.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
mpremote connect /dev/ttyACM0 run tests/test_wifi.py
|
||||||
|
"""
|
||||||
|
|
||||||
|
import time
|
||||||
|
import utime
|
||||||
|
import network
|
||||||
|
from machine import WDT
|
||||||
|
|
||||||
|
from settings import Settings
|
||||||
|
|
||||||
|
CONNECT_TIMEOUT_S = 30
|
||||||
|
RETRY_DELAY_S = 2
|
||||||
|
WDT_TIMEOUT_MS = 10000
|
||||||
|
|
||||||
|
|
||||||
|
def _wifi_status_label(code):
|
||||||
|
names = {
|
||||||
|
getattr(network, "STAT_IDLE", 0): "idle",
|
||||||
|
getattr(network, "STAT_CONNECTING", 1): "connecting",
|
||||||
|
getattr(network, "STAT_WRONG_PASSWORD", -3): "wrong_password",
|
||||||
|
getattr(network, "STAT_NO_AP_FOUND", -2): "no_ap_found",
|
||||||
|
getattr(network, "STAT_CONNECT_FAIL", -1): "connect_fail",
|
||||||
|
getattr(network, "STAT_GOT_IP", 3): "got_ip",
|
||||||
|
}
|
||||||
|
return names.get(code, str(code))
|
||||||
|
|
||||||
|
|
||||||
|
def connect_wifi_with_wdt(sta, ssid, password, wdt):
|
||||||
|
attempt = 0
|
||||||
|
while not sta.isconnected():
|
||||||
|
attempt += 1
|
||||||
|
print("[wifi-test] attempt", attempt, "ssid=", repr(ssid))
|
||||||
|
try:
|
||||||
|
sta.disconnect()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
sta.connect(ssid, password)
|
||||||
|
|
||||||
|
start = utime.time()
|
||||||
|
last_status = None
|
||||||
|
while not sta.isconnected():
|
||||||
|
status = sta.status()
|
||||||
|
if status != last_status:
|
||||||
|
print("[wifi-test] status:", status, _wifi_status_label(status))
|
||||||
|
last_status = status
|
||||||
|
if status in (
|
||||||
|
getattr(network, "STAT_WRONG_PASSWORD", -3),
|
||||||
|
getattr(network, "STAT_NO_AP_FOUND", -2),
|
||||||
|
getattr(network, "STAT_CONNECT_FAIL", -1),
|
||||||
|
):
|
||||||
|
break
|
||||||
|
if utime.time() - start >= CONNECT_TIMEOUT_S:
|
||||||
|
print("[wifi-test] timeout after", CONNECT_TIMEOUT_S, "seconds")
|
||||||
|
break
|
||||||
|
time.sleep(1)
|
||||||
|
wdt.feed()
|
||||||
|
|
||||||
|
if sta.isconnected():
|
||||||
|
return True
|
||||||
|
|
||||||
|
print("[wifi-test] retry in", RETRY_DELAY_S, "seconds")
|
||||||
|
for _ in range(RETRY_DELAY_S):
|
||||||
|
time.sleep(1)
|
||||||
|
wdt.feed()
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
settings = Settings()
|
||||||
|
ssid = settings.get("ssid") or ""
|
||||||
|
password = settings.get("password") or ""
|
||||||
|
|
||||||
|
if not ssid:
|
||||||
|
print("[wifi-test] skipped: settings.ssid is empty")
|
||||||
|
raise SystemExit(0)
|
||||||
|
|
||||||
|
wdt = WDT(timeout=WDT_TIMEOUT_MS)
|
||||||
|
wdt.feed()
|
||||||
|
|
||||||
|
sta = network.WLAN(network.STA_IF)
|
||||||
|
sta.active(True)
|
||||||
|
try:
|
||||||
|
sta.config(pm=network.WLAN.PM_NONE)
|
||||||
|
except (AttributeError, ValueError, TypeError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
ok = connect_wifi_with_wdt(sta, ssid, password, wdt)
|
||||||
|
if not ok or not sta.isconnected():
|
||||||
|
print("[wifi-test] FAILED: not connected")
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
print("[wifi-test] OK:", sta.ifconfig())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
71
tests/udp_client.py
Normal file
71
tests/udp_client.py
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""UDP discovery test — runs on MicroPython (ESP32).
|
||||||
|
|
||||||
|
Brings up Wi-Fi from settings (test harness only), then **`hello.discover_controller_udp(device_name, wdt)`**.
|
||||||
|
`hello` does not use Settings or connect Wi‑Fi.
|
||||||
|
|
||||||
|
In firmware, **`main.py`** discovers the controller IP in RAM for HTTP; it is not written to settings.
|
||||||
|
|
||||||
|
Deploy `src` (including `hello.py`), then from host with cwd `led-driver`:
|
||||||
|
|
||||||
|
mpremote connect PORT run tests/udp_client.py
|
||||||
|
"""
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
|
import network
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
|
||||||
|
from hello import discover_controller_udp
|
||||||
|
from settings import Settings
|
||||||
|
|
||||||
|
CONNECT_WAIT_S = 45
|
||||||
|
WDT_MS = 10000
|
||||||
|
|
||||||
|
|
||||||
|
def _wait_wifi(sta, timeout_s, wdt):
|
||||||
|
deadline = utime.ticks_add(utime.ticks_ms(), int(timeout_s * 1000))
|
||||||
|
while not sta.isconnected():
|
||||||
|
wdt.feed()
|
||||||
|
if utime.ticks_diff(deadline, utime.ticks_ms()) <= 0:
|
||||||
|
return False
|
||||||
|
print("WiFi status:", sta.status())
|
||||||
|
wdt.feed()
|
||||||
|
time.sleep(1)
|
||||||
|
wdt.feed()
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
settings = Settings()
|
||||||
|
ssid = settings.get("ssid") or ""
|
||||||
|
password = settings.get("password") or ""
|
||||||
|
|
||||||
|
if not ssid:
|
||||||
|
print("udp_client: set ssid/password in settings.json (test harness Wi-Fi).")
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
sta = network.WLAN(network.STA_IF)
|
||||||
|
sta.active(True)
|
||||||
|
try:
|
||||||
|
sta.config(pm=network.WLAN.PM_NONE)
|
||||||
|
except (AttributeError, ValueError, TypeError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
wdt = WDT(timeout=WDT_MS)
|
||||||
|
wdt.feed()
|
||||||
|
print("udp_client: connecting to", repr(ssid))
|
||||||
|
sta.connect(ssid, password)
|
||||||
|
wdt.feed()
|
||||||
|
if not _wait_wifi(sta, CONNECT_WAIT_S, wdt):
|
||||||
|
print("WiFi timeout, status=", sta.status())
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
ip = discover_controller_udp(settings.get("name", ""), wdt=wdt)
|
||||||
|
if not ip:
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user