from pygame import Rect

from dark_stew.pgfw.GameChild import GameChild
from dark_stew.pool.Water import Water
from dark_stew.pool.Ring import Ring
from dark_stew.pool.stool.Stools import Stools
from dark_stew.pool.LED import LED

class Pool(GameChild, Rect):

    def __init__(self, parent, center, index):
        GameChild.__init__(self, parent)
        self.init_rect(center)
        self.water = Water(self, index)
        self.ring = Ring(self, index)
        self.stools = Stools(self)
        self.grow()
        # self.led = LED(self)

    def init_rect(self, center):
        Rect.__init__(self, 0, 0, 0, 0)
        self.center = center

    def grow(self):
        padding = self.get_configuration("pool", "padding")
        radius = self.ring.rect.w / 2 + padding
        self.inflate_ip(radius * 2, radius * 2)

    def update(self):
        self.water.update()
        self.ring.update()
        self.stools.draw()
        # self.led.update()
from os import listdir
from os.path import join
from math import sin, cos

from dark_stew.pgfw.Sprite import Sprite

class LED(Sprite):

    def __init__(self, parent):
        Sprite.__init__(self, parent, 20000)
        self.load_configuration()
        self.load_frames()
        self.set_position()

    def load_configuration(self):
        config = self.get_configuration("led")
        self.angle = config["angle"]
        self.offset = config["offset"]
        self.root = self.get_resource("led", "path")

    def load_frames(self):
        root = self.root
        for directory in listdir(root):
            self.load_from_path(join(root, directory), transparency=True,
                                ppa=False)

    def set_position(self):
        parent = self.parent
        cx, cy = parent.center
        distance = parent.ring.rect.w / 2 + self.offset
        angle = self.angle
        dx, dy = cos(angle) * distance, sin(angle) * distance
        self.rect.topleft = cx + dx, cy - dy

    def draw(self):
        x, y = self.rect.topleft
        surface = self.display_surface
        frame = self.get_current_frame()
        for dx, dy in self.parent.parent.parent.get_corners():
            surface.blit(frame, (x + dx, y + dy))
from pygame import Rect

from dark_stew.pgfw.GameChild import GameChild

class Stool(GameChild, Rect):

    def __init__(self, parent, center):
        GameChild.__init__(self, parent)
        self.display_surface = self.get_display_surface()
        self.image = self.parent.image
        self.init_rect(center)

    def init_rect(self, center):
        Rect.__init__(self, (0, 0), self.image.get_size())
        self.center = center

    def draw(self):
        x, y = self.topleft
        surface = self.display_surface
        for dx, dy in self.parent.parent.parent.parent.get_corners():
            surface.blit(self.image, (x + dx, y + dy))
from os.path import join
from math import sin, cos

from pygame import Surface
from pygame.image import load

from dark_stew.pgfw.GameChild import GameChild
from dark_stew.pool.stool.Stool import Stool

class Stools(GameChild, list):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.transparent_color = (0, 0, 0)
        self.load_configuration()
        self.load_image()
        self.add_stools()

    def load_configuration(self):
        config = self.get_configuration("stool")
        self.angles = config["angles"]
        self.offsets = config["offsets"]
        self.image_path = self.get_resource("stool", "path")

    def load_image(self):
        image = load(self.image_path)
        surface = Surface(image.get_size())
        color = self.transparent_color
        surface.fill(color)
        surface.set_colorkey(color)
        surface.blit(image, (0, 0))
        self.image = surface

    def add_stools(self):
        parent = self.parent
        radius = parent.ring.rect.w / 2
        cx, cy = parent.center
        for ii, measure in enumerate(zip(self.angles, self.offsets)):
            angle, offset = measure
            distance = radius + offset
            dx = cos(angle) * distance
            dy = sin(angle) * distance * (-1 if ii == 0 else 1)
            self.append(Stool(self, (cx - dx, cy + dy)))
            self.append(Stool(self, (cx + dx, cy + dy)))

    def draw(self):
        for stool in self:
            stool.draw()
from os import listdir
from os.path import join
from random import shuffle, random
from math import atan2, pi

from pygame import Rect
from pygame.image import load
from pygame.transform import flip

from dark_stew.pgfw.Sprite import Sprite
from dark_stew.aphids.Rod import Rod

class Aphids(Sprite):

    directions = 0, 1, 2, 3

    def __init__(self, parent):
        Sprite.__init__(self, parent)
        self.input = self.get_input()
        self.delegate = self.get_delegate()
        self.direction = self.directions[2]
        self.center = self.display_surface.get_rect().center
        self.sitting = False
        self.load_configuration()
        self.reset_chance()
        self.rod = Rod(self)
        self.load_frames()
        self.activate_frames(self.standing_front_frames, self.rest_framerate)
        self.place_collision_rect()
        self.play()
        self.subscribe(self.respond)

    def load_configuration(self):
        config = self.get_configuration("aphids")
        self.rest_framerate = config["rest-framerate"]
        self.walk_framerate = config["walk-framerate"]
        self.run_framerate = config["run-framerate"]
        self.collision_rect = Rect(config["collision-rect"])
        self.initial_chance = config["initial-chance"]
        self.chance_increase = config["chance-increase"]
        root = self.get_resource("aphids", "path")
        animation_path = config["animation-path"]
        self.moving_path = join(root, config["moving-path"], animation_path)
        standing_path = join(root, config["standing-path"])
        front_path = config["front-path"]
        self.standing_front_path = join(standing_path, front_path,
                                        animation_path)
        self.standing_side_path = join(standing_path, config["side-path"],
                                       animation_path)
        back_path = config["back-path"]
        self.standing_back_path = join(standing_path, config["back-path"],
                                       animation_path)
        sitting_path = join(root, config["sitting-path"])
        self.sitting_front_path = join(sitting_path, front_path, animation_path)
        self.sitting_back_path = join(sitting_path, back_path, animation_path)
        self.sitting_path = sitting_path

    def reset_chance(self):
        self.catch_chance = self.initial_chance

    def load_frames(self):
        load_path = self.load_path
        self.moving_right_frames = load_path(self.moving_path, random=False)
        self.moving_left_frames = load_path(self.moving_path, True, False)
        self.standing_front_frames = load_path(self.standing_front_path)
        self.standing_right_frames = load_path(self.standing_side_path)
        self.standing_left_frames = load_path(self.standing_side_path, True)
        self.standing_back_frames = load_path(self.standing_back_path)
        self.sitting_northwest_frames = load_path(self.sitting_front_path)
        self.sitting_northeast_frames = load_path(self.sitting_front_path, True)
        self.sitting_southwest_frames = load_path(self.sitting_back_path)
        self.sitting_southeast_frames = load_path(self.sitting_back_path, True)

    def load_path(self, root, mirror=False, random=True):
        frames = []
        names = listdir(root)
        if random:
            shuffle(names)
        else:
            names.sort()
        for path in [join(root, name) for name in names]:
            image = load(path)
            if mirror:
                image = flip(image, True, False)
            frames.append(self.fill_colorkey(image))
        return frames

    def place_collision_rect(self):
        self.collision_rect.move_ip(self.rect.topleft)

    def respond(self, event):
        if self.delegate.compare(event, "action"):
            monsters = self.parent.parent.monsters
            if not self.sitting and not monsters.current:
                self.find_seat()
            elif monsters.current:
                monsters.close()
            else:
                self.sitting = False
                self.rod.deactivate()
                if random() <= self.catch_chance:
                    self.parent.parent.monsters.open_random()
                self.reset_chance()

    def find_seat(self):
        parent = self.parent
        for dx, dy in parent.get_corners():
            rect = self.collision_rect.move(-dx, -dy)
            for pool in parent.pools:
                if rect.colliderect(pool):
                    self.sit(pool, rect)
                    return

    def sit(self, pool, rect):
        cx, cy = pool.center
        x, y = rect.center
        angle = atan2(y - cy, x - cx)
        framerate = self.rest_framerate
        stools = pool.stools
        self.parent.reset_active_directions()
        if angle >= 0 and angle < pi / 2:
            self.activate_frames(self.sitting_southeast_frames, framerate)
            stool = stools[3]
            dx, dy = 11, -27
            self.rod.activate(-33, -9)
        elif angle >= pi / 2 and angle < pi:
            self.activate_frames(self.sitting_southwest_frames, framerate)
            stool = stools[2]
            dx, dy = -14, -27
            self.rod.activate(12, -8, True)
        elif angle >= -pi and angle < -pi / 2:
            self.activate_frames(self.sitting_northwest_frames, framerate)
            stool = stools[0]
            dx, dy = -13, -29
            self.rod.activate(10, -12, True)
        else:
            self.activate_frames(self.sitting_northeast_frames, framerate)
            stool = stools[1]
            dx, dy = 11, -29
            self.rod.activate(-30, -12)
        self.sitting = True
        self.parent.shift(rect.centerx - stool.centerx + dx,
                          rect.centery - stool.centery + dy, False)

    def activate_frames(self, frames, framerate):
        if self.frames != frames:
            self.frames = frames
            self.frame_index = 0
            self.measure_rect()
            self.rect.center = self.center
            self.set_framerate(framerate)

    def update(self):
        if self.sitting:
            self.catch_chance += (1 - self.catch_chance) * self.chance_increase
        self.set_frames()
        Sprite.update(self)
        self.rod.update()

    def set_frames(self):
        parent = self.parent
        direction = parent.direction
        up, right, down, left = parent.directions
        if self.parent.scroll_active:
            if self.input.is_command_active("run"):
                framerate = self.run_framerate
            else:
                framerate = self.walk_framerate
            if direction in [right, up]:
                self.activate_frames(self.moving_right_frames, framerate)
            else:
                self.activate_frames(self.moving_left_frames, framerate)
        elif not self.sitting:
            framerate = self.rest_framerate
            if direction == up:
                self.activate_frames(self.standing_back_frames, framerate)
            elif direction == right:
                self.activate_frames(self.standing_right_frames, framerate)
            elif direction == down:
                self.activate_frames(self.standing_front_frames, framerate)
            else:
                self.activate_frames(self.standing_left_frames, framerate)
216.73.216.33
216.73.216.33
216.73.216.33
 
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 😇