Загрузка данных


# ==============================================================================
# ИМПОРТ БИБЛИОТЕК
# ==============================================================================
import random
import time
from machine import Pin, SPI, PWM, time_pulse_us
import bluetooth
from ble_simple_peripheral import BLESimplePeripheral
import dht

# Попытка импорта текстовой библиотеки
try:
    from ili9341 import Display, color565
    TEXT_LIB_AVAILABLE = True
except ImportError:
    TEXT_LIB_AVAILABLE = False
    print("Внимание: Библиотека ili9341.py не найдена. Текст на экран выводиться не будет.")

# ==============================================================================
# КЛАСС БЫСТРОГО ДРАЙВЕРА ДЛЯ АНИМАЦИЙ
# ==============================================================================
class ILI9341Direct:
    def __init__(self, spi, cs_pin, dc_pin, rst_pin):
        self.spi = spi
        self.cs = Pin(cs_pin, Pin.OUT)
        self.dc = Pin(dc_pin, Pin.OUT)
        self.rst = Pin(rst_pin, Pin.OUT)
        self.reset()
        self.init_display()

    def reset(self):
        self.rst.value(0)
        time.sleep_ms(50)
        self.rst.value(1)
        time.sleep_ms(100)

    def write_cmd(self, cmd):
        self.dc.value(0)
        self.cs.value(0)
        self.spi.write(bytearray([cmd]))
        self.cs.value(1)

    def write_data(self, data):
        self.dc.value(1)
        self.cs.value(0)
        self.spi.write(data)
        self.cs.value(1)

    def init_display(self):
        self.write_cmd(0x01)
        time.sleep_ms(100)
        self.write_cmd(0x11)
        time.sleep_ms(120)
        self.write_cmd(0x3A)
        self.write_data(b'\x55')
        self.write_cmd(0x36)
        self.write_data(b'\x28')
        self.write_cmd(0x29)
        time.sleep_ms(50)

    def set_window(self, x0, y0, x1, y1):
        self.write_cmd(0x2A)
        self.write_data(bytes([(x0 >> 8) & 0xFF, x0 & 0xFF, (x1 >> 8) & 0xFF, x1 & 0xFF]))
        self.write_cmd(0x2B)
        self.write_data(bytes([(y0 >> 8) & 0xFF, y0 & 0xFF, (y1 >> 8) & 0xFF, y1 & 0xFF]))
        self.write_cmd(0x2C)

    def clear(self, color=0x0000):
        self.set_window(0, 0, 319, 239)
        row_buf = color.to_bytes(2, 'big') * 320
        for _ in range(240):
            self.write_data(row_buf)


@micropython.viper
def decode_chunk_normal(raw_ptr: ptr8, rgb_ptr: ptr8, num_bytes: int, fg_h: int, fg_l: int, bg_h: int, bg_l: int):
    out_idx = 0
    for i in range(num_bytes):
        b = raw_ptr[i]
        for bit in range(7, -1, -1):
            if (b >> bit) & 1:
                rgb_ptr[out_idx] = fg_h
                rgb_ptr[out_idx + 1] = fg_l
            else:
                rgb_ptr[out_idx] = bg_h
                rgb_ptr[out_idx + 1] = bg_l
            out_idx += 2


def play_animation(tft, filename, width=240, height=240, fps=30, fg_color=0xFFFF, bg_color=0x0000):
    bytes_per_row = width // 8
    frame_bytes = bytes_per_row * height
    raw_buf = bytearray(frame_bytes)
    chunk_rows = 20
    chunk_raw_bytes = bytes_per_row * chunk_rows
    chunk_rgb = bytearray(width * chunk_rows * 2)

    fg_h, fg_l = (fg_color >> 8) & 0xFF, fg_color & 0xFF
    bg_h, bg_l = (bg_color >> 8) & 0xFF, bg_color & 0xFF

    x_offset = (320 - width) // 2
    y_offset = (240 - height) // 2
    delay_ms = int(1000 / fps) if fps > 0 else 0

    try:
        with open(filename, 'rb') as f:
            while True:
                read_bytes = f.readinto(raw_buf)
                if read_bytes < frame_bytes:
                    break
                
                t_start = time.ticks_ms()
                tft.set_window(x_offset, y_offset, x_offset + width - 1, y_offset + height - 1)
                
                for offset in range(0, frame_bytes, chunk_raw_bytes):
                    chunk_mem = memoryview(raw_buf)[offset : offset + chunk_raw_bytes]
                    decode_chunk_normal(chunk_mem, chunk_rgb, chunk_raw_bytes, fg_h, fg_l, bg_h, bg_l)
                    tft.write_data(chunk_rgb)
                
                if delay_ms > 0:
                    elapsed = time.ticks_diff(time.ticks_ms(), t_start)
                    sleep_time = delay_ms - elapsed
                    if sleep_time > 0:
                        time.sleep_ms(sleep_time)
    except OSError:
        print(f"Анимация {filename} не найдена")


# ==============================================================================
# НАСТРОЙКА ПИНОВ И ПЕРИФЕРИИ
# ==============================================================================
# 1. Дисплей
spi = SPI(2, baudrate=40000000, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
tft = ILI9341Direct(spi, cs_pin=5, dc_pin=2, rst_pin=4)
tft.clear(0x0000)

if TEXT_LIB_AVAILABLE:
    try:
        display = Display(spi, dc=Pin(2), cs=Pin(5), rst=Pin(4), width=320, height=240, rotation=90)
    except Exception as e:
        print("Ошибка инициализации текстового дисплея:", e)
        display = None
else:
    display = None

# 2. Сервопривод (Шея)
servo = PWM(Pin(25), freq=50)

# 3. Датчик DHT11
dht_sensor = dht.DHT11(Pin(33))

# 4. Ультразвуковой датчик
trig = Pin(16, Pin.OUT)
echo = Pin(35, Pin.IN)

# 5. Драйвер моторов
M_A_1A = Pin(26, Pin.OUT)
M_A_1B = Pin(27, Pin.OUT)
M_B_1A = Pin(13, Pin.OUT)
M_B_1B = Pin(14, Pin.OUT)

# 6. Bluetooth
ble = bluetooth.BLE()
sp = BLESimplePeripheral(ble, name="LOIK")


# ==============================================================================
# ВСПОМОГАТЕЛЬНЫЕ ФУНКЦИИ
# ==============================================================================
def set_servo_angle(angle):
    """Установка угла сервопривода от 0 до 180 градусов"""
    duty = int(4000 + (angle / 180) * 4000)
    servo.duty_u16(duty)


def measure_distance():
    """Измерение расстояния ультразвуком (в см)"""
    trig.value(0)
    time.sleep_us(2)
    trig.value(1)
    time.sleep_us(10)
    trig.value(0)
    
    try:
        duration = time_pulse_us(echo, 1, 30000)
        if duration > 0:
            return (duration * 0.0343) / 2
        return 999
    except OSError:
        return 999


def stop_motors():
    M_A_1A.value(0)
    M_A_1B.value(0)
    M_B_1A.value(0)
    M_B_1B.value(0)


def move_forward():
    M_A_1A.value(1)
    M_A_1B.value(0)
    M_B_1A.value(1)
    M_B_1B.value(0)


def move_backward():
    M_A_1A.value(0)
    M_A_1B.value(1)
    M_B_1A.value(0)
    M_B_1B.value(1)


def turn_left():
    M_A_1A.value(0)
    M_A_1B.value(1)
    M_B_1A.value(1)
    M_B_1B.value(0)


def turn_right():
    M_A_1A.value(1)
    M_A_1B.value(0)
    M_B_1A.value(0)
    M_B_1B.value(1)


def turn_around_180():
    """Разворот на 180 градусов (для края стола)"""
    turn_left()
    time.sleep(0.7)
    stop_motors()


# ==============================================================================
# ОБРАБОТКА КОМАНД BLUETOOTH
# ==============================================================================
def on_rx(data):
    try:
        cmd = data.decode('utf-8').strip().lower()
        print(f'[BT] Получена команда: {cmd}')
        
        # КОМАНДЫ ИЗ APP INVENTOR
        if cmd == 'w': 
            print("Команда LOVE (Анимация)")
            play_animation(tft, 'anim_1bit6.bin', fps=30) 
            
        elif cmd == 'q': 
            print("Команда HI (Анимация)")
            play_animation(tft, 'hi.bin', fps=30) 
            
        elif cmd == 'y': 
            print("Команда FORWARD (Вперед)")
            move_forward()
            time.sleep(1)
            stop_motors()
            
        elif cmd == 'o': 
            print("Команда BACK (Назад)")
            move_backward()
            time.sleep(1)
            stop_motors()
            
        # УПРАВЛЕНИЕ КОЛЕСАМИ
        elif cmd == 'r': 
            print("Команда LEFT (Колеса налево)")
            turn_left()
            time.sleep(0.5)
            stop_motors()
            
        elif cmd == 't': 
            print("Команда RIGHT (Колеса направо)")
            turn_right()
            time.sleep(0.5)
            stop_motors()
            
        # УПРАВЛЕНИЕ ТОЛЬКО ШЕЕЙ
        elif cmd == 'gleft': 
            print("Команда GLEFT (Шея налево)")
            set_servo_angle(180)
            time.sleep(1.5) # Ждем, пока серво повернется
            set_servo_angle(90) # Возврат в центр
            
        elif cmd == 'gright': 
            print("Команда GRIGHT (Шея направо)")
            set_servo_angle(0)
            time.sleep(1.5)
            set_servo_angle(90)

    except Exception as e:
        print("Ошибка обработки команды:", e)

sp.on_write(on_rx)


# ==============================================================================
# ПЕРЕМЕННЫЕ СОСТОЯНИЯ РОБОТА
# ==============================================================================
last_move_time = time.ticks_ms() # Время последней остановки
wait_time = random.randint(10, 17) * 1000 # Сколько ждать (в мс)
is_moving = False
move_end_time = 0
last_distance = 999
last_dht_time = time.ticks_ms()
last_blink_time = time.ticks_ms()
looked_around_this_wait = False 
has_blinked_this_wait = False


# ==============================================================================
# ГЛАВНЫЙ ЦИКЛ
# ==============================================================================
print("========================================")
print("Робот LOIK запущен! Автономный режим.")
print("Ожидание первого движения...")
print("========================================")

while True:
    now = time.ticks_ms()

    # --------------------------------------------------------------------------
    # 1. ЧТЕНИЕ ДАТЧИКА DHT11 (Раз в 3 секунды)
    # --------------------------------------------------------------------------
    if time.ticks_diff(now, last_dht_time) > 3000:
        try:
            dht_sensor.measure()
            t = dht_sensor.temperature()
            h = dht_sensor.humidity()
            
            if display:
                display.fill_rectangle(0, 0, 320, 40, color565(0, 0, 0)) 
                display.draw_text8x8(10, 10, f"T: {t}C  H: {h}%", color565(255, 255, 255), color565(0,0,0), 0)
        except OSError:
            pass
        last_dht_time = now

    # --------------------------------------------------------------------------
    # 2. УЛЬТРАЗВУК (Проверка на край стола)
    # --------------------------------------------------------------------------
    dist = measure_distance()
    
    if last_distance < 10 and dist > 20:
        print("[!] ОБНАРУЖЕН КРАЙ СТОЛА! Разворот.")
        stop_motors()
        turn_around_180()
        
        print("-> Едем вперед после разворота")
        move_forward()
        time.sleep(random.uniform(1.0, 2.5))
        stop_motors()
        
        # Сброс таймеров после экстренного разворота
        last_move_time = time.ticks_ms()
        wait_time = random.randint(10, 17) * 1000
        is_moving = False
        looked_around_this_wait = False
        has_blinked_this_wait = False
        
    last_distance = dist

    # --------------------------------------------------------------------------
    # 3. МОРГАНИЕ (Раз в 10 секунд во время ожидания)
    # --------------------------------------------------------------------------
    if not is_moving and not has_blinked_this_wait:
        if time.ticks_diff(now, last_move_time) >= 10000:
            print("[Моргание] Робот моргает...")
            play_animation(tft, 'blink.bin', fps=30)
            has_blinked_this_wait = True

    # --------------------------------------------------------------------------
    # 4. ЛОГИКА ОЖИДАНИЯ И ОСМОТРА
    # --------------------------------------------------------------------------
    if not is_moving:
        elapsed_wait = time.ticks_diff(now, last_move_time)
        
        # Осмотр на 10-й секунде
        if elapsed_wait >= 10000 and elapsed_wait < 10500 and not looked_around_this_wait:
            rand_check = random.randint(1, 20)
            print(f"[Осмотр] Проверка: выпало {rand_check}")
            
            if rand_check > 14:
                print("-> Скучно! Крутим шеей...")
                set_servo_angle(180) # Налево
                time.sleep(2)
                set_servo_angle(0)   # Направо
                time.sleep(4)
                set_servo_angle(90)  # В центр
                time.sleep(1)
                
            looked_around_this_wait = True 

        # Проверка, не пора ли ехать (только если время вышло)
        if elapsed_wait >= wait_time:
            # Выбираем случайное действие
            action = random.choice(['forward', 'forward', 'forward', 'left', 'right'])
            duration = random.uniform(1.0, 2.5) 
            
            print(f"[Движение] Едем: {action} ({duration:.1f} сек)")
            
            if action == 'forward':
                move_forward()
            elif action == 'left':
                turn_left()
            elif action == 'right':
                turn_right()
                
            is_moving = True
            move_end_time = time.ticks_add(now, int(duration * 1000))

    # --------------------------------------------------------------------------
    # 5. ЗАВЕРШЕНИЕ ДВИЖЕНИЯ
    # --------------------------------------------------------------------------
    else:
        if time.ticks_diff(now, move_end_time) >= 0:
            print("[Движение] Остановка.")
            stop_motors()
            is_moving = False
            
            # Генерируем новое время ожидания (от 10 до 17 секунд)
            last_move_time = time.ticks_ms()
            wait_time = random.randint(10, 17) * 1000 
            
            # Сбрасываем флаги для новой стоянки
            looked_around_this_wait = False 
            has_blinked_this_wait = False
            
            print(f"[Ожидание] Стоим {wait_time/1000:.0f} секунд...")

    time.sleep_ms(20)