Compare commits
No commits in common. "cf2eac04bfd70475159aae90a6cff2a2b3b93479" and "091755ca08a44d8b478ea4c444ba5134b6eb7272" have entirely different histories.
cf2eac04bf
...
091755ca08
|
|
@ -160,6 +160,3 @@ cython_debug/
|
|||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
|
||||
settings.json
|
||||
extensions.json
|
||||
*.code-workspace
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
{
|
||||
"info": "This file is just used to identify a project folder."
|
||||
}
|
||||
28
config.json
28
config.json
|
|
@ -1,28 +0,0 @@
|
|||
{
|
||||
"switches": [
|
||||
{
|
||||
"id": 1283,
|
||||
"pin": 12,
|
||||
"angle_minus": 65,
|
||||
"angle_plus": 125
|
||||
},
|
||||
{
|
||||
"id": 1272,
|
||||
"pin": 13,
|
||||
"angle_minus": 65,
|
||||
"angle_plus": 125
|
||||
},
|
||||
{
|
||||
"id": 1444,
|
||||
"pin": 2,
|
||||
"angle_minus": 125,
|
||||
"angle_plus": 65
|
||||
},
|
||||
{
|
||||
"id": 1274,
|
||||
"pin": 3,
|
||||
"angle_minus": 125,
|
||||
"angle_plus": 65
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
from machine import Pin
|
||||
import neopixel
|
||||
import time
|
||||
import random
|
||||
|
||||
# Настройки: 8 LED, подключено к GP6
|
||||
num_leds = 8
|
||||
pin = 6 # GP6
|
||||
|
||||
np = neopixel.NeoPixel(Pin(pin), num_leds)
|
||||
|
||||
red = (255, 0, 0) # Красный (R, G, B)
|
||||
green = (0, 255, 0) # Зеленый
|
||||
blue = (0, 0, 255) # Синий
|
||||
orange = (255, 165, 0) # Оранжевый
|
||||
moon_white = (224, 224, 192) # Лунный белый
|
||||
off = (0, 0, 0) # Выключено
|
||||
|
||||
while True:
|
||||
np.fill(red) # Все LED красные
|
||||
colors = [red, green, blue, orange, moon_white]
|
||||
for i in range(num_leds):
|
||||
np[i] = random.choice(colors)
|
||||
np.write() # Обновить
|
||||
time.sleep(0.5) # Пауза 0.5 сек
|
||||
|
||||
np.fill(off) # Все выключить
|
||||
np.write()
|
||||
time.sleep(0.5)
|
||||
82
main.py
82
main.py
|
|
@ -1,82 +0,0 @@
|
|||
import sys
|
||||
from switch import Switch
|
||||
import select
|
||||
from machine import Pin
|
||||
|
||||
|
||||
SWITCHES = dict()
|
||||
LED = Pin("LED", Pin.OUT) # "LED" — специальное имя для встроенного индикатора
|
||||
|
||||
|
||||
|
||||
def load_switches():
|
||||
with open("config.json", "r") as file:
|
||||
import json
|
||||
config = json.load(file)
|
||||
for sw_cfg in config["switches"]:
|
||||
sw = Switch(
|
||||
id=sw_cfg["id"],
|
||||
pin=sw_cfg["pin"],
|
||||
angle_minus=sw_cfg["angle_minus"],
|
||||
angle_plus=sw_cfg["angle_plus"]
|
||||
)
|
||||
SWITCHES[sw.id] = sw
|
||||
# print(f"Loaded {len(SWITCHES)} switch{'es' if len(SWITCHES) > 1 else ''}.")
|
||||
|
||||
|
||||
def resolve_command(command: str):
|
||||
parts = command.split()
|
||||
if len(parts) == 4 and parts[0] == "SWITCH" and parts[2] == "TURN":
|
||||
try:
|
||||
sw_id = int(parts[1])
|
||||
except ValueError:
|
||||
return "ERROR Invalid ID"
|
||||
|
||||
direction = parts[3]
|
||||
if direction not in ("+", "-"):
|
||||
return "ERROR Invalid direction"
|
||||
|
||||
if sw_id not in SWITCHES:
|
||||
return f"ERROR Switch {sw_id} not found"
|
||||
|
||||
# Выполняем действие
|
||||
if direction == "+":
|
||||
SWITCHES[sw_id].set_plus()
|
||||
else:
|
||||
SWITCHES[sw_id].set_minus()
|
||||
|
||||
# РОВНО ОДНА СТРОКА — подтверждение успеха
|
||||
return f"EVENT SWITCH {sw_id} {direction}"
|
||||
elif parts[0] == "GET" and parts[1] == "ALL":
|
||||
evts = []
|
||||
for id, sw in SWITCHES.items():
|
||||
evts.append(f"EVENT SWITCH {id} {sw.pos}")
|
||||
return "\n".join(evts)
|
||||
|
||||
|
||||
def work():
|
||||
poll = select.poll()
|
||||
poll.register(sys.stdin, select.POLLIN)
|
||||
|
||||
while True:
|
||||
events = poll.poll()
|
||||
for fd, event in events:
|
||||
if event & select.POLLIN:
|
||||
try:
|
||||
line = sys.stdin.readline().strip()
|
||||
if not line:
|
||||
continue
|
||||
|
||||
parts = line.split()
|
||||
result = resolve_command(line)
|
||||
if result:
|
||||
print(result)
|
||||
|
||||
except Exception as e:
|
||||
# Любая неожиданная ошибка — тоже одна строка
|
||||
print(f"ERROR {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
LED.on()
|
||||
load_switches()
|
||||
work()
|
||||
16
servo.py
16
servo.py
|
|
@ -1,16 +0,0 @@
|
|||
from machine import Pin, PWM
|
||||
|
||||
class Servo:
|
||||
def __init__(self, pin):
|
||||
self.pwm = PWM(Pin(pin))
|
||||
self.pwm.freq(50)
|
||||
self.min_duty = 1638 # ~0.5 ms
|
||||
self.max_duty = 8192 # ~2.5 ms
|
||||
|
||||
def angle(self, deg):
|
||||
deg = max(0, min(180, deg))
|
||||
duty = int(self.min_duty + (deg / 180) * (self.max_duty - self.min_duty))
|
||||
self.pwm.duty_u16(duty)
|
||||
|
||||
def off(self):
|
||||
self.pwm.duty_u16(0)
|
||||
20
switch.py
20
switch.py
|
|
@ -1,20 +0,0 @@
|
|||
from servo import Servo
|
||||
|
||||
|
||||
class Switch:
|
||||
def __init__(self, id, pin, angle_minus, angle_plus):
|
||||
self.id = id
|
||||
self.pin = pin
|
||||
self.angle_minus = angle_minus
|
||||
self.angle_plus = angle_plus
|
||||
self.servo = Servo(pin)
|
||||
self.pos = "-"
|
||||
self.set_minus()
|
||||
|
||||
def set_minus(self):
|
||||
self.servo.angle(self.angle_minus)
|
||||
self.pos = "-"
|
||||
|
||||
def set_plus(self):
|
||||
self.servo.angle(self.angle_plus)
|
||||
self.pos = "+"
|
||||
Loading…
Reference in New Issue