from pygame import Surface
from pygame.locals import *
from pygame.image import load

from esp_hadouken.pgfw.GameChild import GameChild

class Glass(GameChild):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.display_surface = self.get_screen()
        self.load_image()

    def load_image(self):
        self.image = load(self.get_resource("title", "glass-path")).convert()

    def update(self):
        self.display_surface.blit(self.image, (0, 0), None, BLEND_MAX)
from pygame.locals import *
from pygame.image import load
from pygame.font import Font

from esp_hadouken.pgfw.GameChild import GameChild
from esp_hadouken.pgfw.Input import Input
from esp_hadouken.Toy import Toy
from esp_hadouken.title.background.Background import Background
from esp_hadouken.title.menu.Menu import Menu
from esp_hadouken.title.Glass import Glass

class Title(GameChild):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.leave_limit = self.get_screen().get_rect().right
        self.audio_path = self.get_resource("title", "audio-path")
        self.toy_position = self.get_configuration("title", "toy-position")
        self.delegate = self.get_delegate()
        self.deactivate()
        self.set_children()
        self.place_toy()
        self.subscribe(self.respond)

    def deactivate(self):
        self.active = False

    def set_children(self):
        self.toy = Toy(self)
        self.background = Background(self)
        self.glass = Glass(self)
        self.menu = Menu(self)

    def place_toy(self):
        self.toy.rect.topleft = self.toy_position

    def respond(self, event):
        compare = self.delegate.compare
        if compare(event, "reset-game"):
            self.activate()
        elif compare(event, "main-menu-selection", option="play"):
            self.deactivate()
        elif compare(event, "main-menu-selection", option="records"):
            self.deactivate()

    def activate(self):
        self.active = True
        self.start_music()

    def start_music(self):
        self.get_audio().play_bgm(self.audio_path)

    def update(self):
        if self.active:
            self.background.update()
            self.toy.update()
            self.glass.update()
            self.menu.update()
from pygame import Rect

from esp_hadouken.pgfw.GameChild import GameChild
from esp_hadouken.title.menu.Option import Option

class Menu(Rect, GameChild):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.display_surface = self.get_screen()
        self.fx = self.get_configuration("main-menu", "select-fx")
        self.delegate = self.get_delegate()
        self.init_rect()
        self.add_options()
        self.subscribe(self.respond)

    def init_rect(self):
        Rect.__init__(self, (0, 0), self.get_configuration("main-menu", "size"))
        self.center = self.display_surface.get_rect().center

    def add_options(self):
        self.options = [Option(self, ii) for ii in range(2)]

    def respond(self, event):
        if self.parent.active:
            compare = self.delegate.compare
            if compare(event, ["left", "right"]):
                play = self.get_audio().play_fx
                post = self.delegate.post
                name = "main-menu-selection"
                if compare(event, "left"):
                    play(self.fx[0])
                    post(name, option="play")
                elif compare(event, "right"):
                    play(self.fx[1])
                    post(name, option="records")

    def apply_to_options(self, name):
        for option in self.options:
            getattr(option, name)()

    def update(self):
        self.apply_to_options("update")
from random import randint

from pygame import Surface, PixelArray, Rect
from pygame.font import Font

from esp_hadouken.pgfw.Sprite import Sprite

class Option(Sprite):

    def __init__(self, parent, index):
        Sprite.__init__(self, parent)
        self.index = index
        self.load_configuration()
        self.set_framerate(self.framerate)
        self.add_frames()
        self.place()

    def load_configuration(self):
        config = self.get_configuration("main-menu")
        self.framerate = config["framerate"]
        self.width = config["option-width"]
        self.frame_count = config["frame-count"]
        self.text_color = config["text-color"]
        self.text_size = config["text-size"]
        self.text = config["option-text"]
        self.background_color_range = config["background-color-range"]
        self.font_path = self.get_resource("main-menu", "font-path")

    def add_frames(self):
        size = self.width, self.parent.h
        for ii in range(self.frame_count):
            surface = Surface(size)
            self.randomize(surface)
            self.add_text(surface)
            self.add_frame(surface)

    def randomize(self, surface):
        pixels = PixelArray(surface)
        for ii, column in enumerate(pixels):
            for jj, pixel in enumerate(column):
                pixels[ii][jj] = self.build_random_color()

    def build_random_color(self):
        color = self.background_color_range
        return randint(*color), randint(*color), randint(*color)

    def add_text(self, parent):
        index, color, antialias = self.index, self.text_color, True
        font = Font(self.font_path, self.text_size)
        if index == 0:
            surface = font.render(self.text[0], antialias, color)
        elif index == 1:
            surface = font.render(self.text[1], antialias, color)
        rect = surface.get_rect()
        rect.center = parent.get_rect().center
        parent.blit(surface, rect)

    def place(self):
        index = self.index
        if index == 0:
            self.rect.topleft = self.parent.topleft
        elif index == 1:
            self.rect.topright = self.parent.topright
from math import pi

from pygame import Surface, Rect
from pygame.draw import arc

from esp_hadouken.pgfw.Sprite import Sprite

class Sky(Sprite):

    def __init__(self, parent):
        Sprite.__init__(self, parent)
        self.load_configuration()
        self.add_frames()
        self.set_framerate(self.framerate)

    def load_configuration(self):
        config = self.get_configuration("title")
        self.framerate = config["sky-framerate"]
        self.step = config["sky-arc-step"]
        self.arc_thickness = config["sky-arc-thickness"]
        self.arc_color = config["sky-arc-color"]
        self.color = config["sky-color"]

    def add_frames(self):
        for ii in 0, 1:
            offset = -ii * self.step / 2
            surface = Surface(self.display_surface.get_size())
            surface.fill(self.color)
            self.draw_arcs(surface, offset)
            self.add_frame(surface)

    def draw_arcs(self, surface, offset):
        step = self.step
        reference = self.display_surface.get_rect()
        limit = reference.w
        rect = Rect(0, 0, step + offset, step + offset)
        center = reference.centerx, reference.bottom
        rect.center = center
        start_angle = pi / 2
        thickness = self.arc_thickness
        color = self.arc_color
        while rect.w / 2 <= limit:
            arc(surface, color, rect, start_angle, 0, thickness)
            rect.w += step
            rect.h += step
            rect.center = center
216.73.216.209
216.73.216.209
216.73.216.209
 
January 23, 2021

I wanted to document this chat-controlled robot I made for Babycastles' LOLCAM📸 that accepts a predefined set of commands like a character in an RPG party 〰 commands like walk, spin, bash, drill. It can also understand donut, worm, ring, wheels, and more. The signal for each command is transmitted as a 24-bit value over infrared using two Arduinos, one with an infrared LED, and the other with an infrared receiver. I built the transmitter circuit, and the receiver was built into the board that came with the mBot robot kit. The infrared library IRLib2 was used to transmit and receive the data as a 24-bit value.


fig. 1.1: the LEDs don't have much to do with this post!

I wanted to control the robot the way the infrared remote that came with the mBot controlled it, but the difference would be that since we would be getting input from the computer, it would be like having a remote with an unlimited amount of buttons. The way the remote works is each button press sends a 24-bit value to the robot over infrared. Inspired by Game Boy Advance registers and tracker commands, I started thinking that if we packed multiple parameters into the 24 bits, it would allow a custom move to be sent each time, so I wrote transmitter and receiver code to process commands that looked like this:

bit
name
description
00
time
multiply by 64 to get duration of command in ms
01
02
03
04
left
multiply by 16 to get left motor power
05
06
07
08
right
multiply by 16 to get right motor power
09
10
11
12
left sign
0 = left wheel backward, 1 = left wheel forward
13
right sign
0 = right wheel forward, 1 = right wheel backward
14
robot id
0 = send to player one, 1 = send to player two
15
flip
negate motor signs when repeating command
16
repeats
number of times to repeat command
17
18
19
delay
multiply by 128 to get time between repeats in ms
20
21
22
23
swap
swap the motor power values on repeat
fig 1.2: tightly stuffed bits

The first command I was able to send with this method that seemed interesting was one that made the mBot do a wheelie.

$ ./send_command.py 15 12 15 1 0 0 0 7 0 1
sending 0xff871fcf...


fig 1.3: sick wheels

A side effect of sending the signal this way is any button on any infrared remote will cause the robot to do something. The star command was actually reverse engineered from looking at the code a random remote button sent. For the robot's debut, it ended up with 15 preset commands (that number is in stonks 📈). I posted a highlights video on social media of how the chat controls turned out.

This idea was inspired by a remote frog tank LED project I made for Ribbit's Frog World which had a similar concept: press a button, and in a remote location where 🐸 and 🐠 live, an LED would turn on.


fig 2.1: saying hi to froggo remotely using an LED

😇 The transmitter and receiver Arduino programs are available to be copied and modified 😇