Compare commits

...

2 Commits

Author SHA1 Message Date
Artem Kashaev cf2eac04bf Merge branch 'main' of git.k1nq.tech:k1nq/micro_mpc_kmk 2025-12-22 13:55:10 +05:00
Artem Kashaev 34294a86e8 Add initial project files including configuration and LED control scripts
- Create .gitignore to exclude unnecessary files
- Add .micropico for project identification
- Implement config.json for switch configurations
- Develop led_panel_blink.py for LED control with color cycling
- Create main.py to manage switch commands and interactions
- Introduce servo.py for servo control functionality
- Define switch.py to handle switch operations with servo integration
2025-12-22 13:42:17 +05:00
7 changed files with 181 additions and 0 deletions

3
.gitignore vendored
View File

@ -160,3 +160,6 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
settings.json
extensions.json
*.code-workspace

3
.micropico Normal file
View File

@ -0,0 +1,3 @@
{
"info": "This file is just used to identify a project folder."
}

28
config.json Normal file
View File

@ -0,0 +1,28 @@
{
"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
}
]
}

29
led_panel_blink.py Normal file
View File

@ -0,0 +1,29 @@
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 Normal file
View File

@ -0,0 +1,82 @@
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 Normal file
View File

@ -0,0 +1,16 @@
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 Normal file
View File

@ -0,0 +1,20 @@
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 = "+"