from glob import glob
from os.path import join

from pygame import Color, PixelArray
from pygame.image import load

from lib.pgfw.pgfw.GameChild import GameChild

class View(GameChild):

    MAX_ALPHA = 255

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.index = 0
        self.alpha = self.MAX_ALPHA
        self.frame_index = 1
        self.display_surface = self.get_display_surface()
        self.load_configuration()
        self.set_frames()

    def load_configuration(self):
        config = self.get_configuration("view")
        self.coordinates = config["coordinates"]
        self.root = config["path"]
        self.alpha_step = config["alpha-step"]

    def set_frames(self):
        frames = []
        for path in sorted(glob(join(self.get_resource(self.root), "[0-9]*"))):
            frames.append(load(path).convert())
        self.frames = frames

    def update(self):
        self.decrement_alpha()
        self.draw()

    def decrement_alpha(self):
        alpha = self.alpha - self.alpha_step
        if alpha <= 0:
            alpha = self.MAX_ALPHA
            self.get_previous_frame().set_alpha(alpha)
            self.increment_frame_index()
        self.get_previous_frame().set_alpha(alpha)
        self.alpha = alpha

    def get_current_frame(self):
        return self.frames[self.frame_index]

    def increment_frame_index(self):
        index = self.frame_index + 1
        if index >= len(self.frames):
            index = 0
        self.frame_index = index

    def draw(self):
        display_surface = self.display_surface
        coordinates = self.coordinates
        display_surface.blit(self.get_current_frame(), coordinates)
        display_surface.blit(self.get_previous_frame(), coordinates)

    def get_previous_frame(self):
        frames = self.frames
        index = self.frame_index - 1
        if index < 0:
            index = len(frames) - 1
        return frames[index]
from pygame import Rect

from lib.pgfw.pgfw.GameChild import GameChild

class Collection(GameChild, Rect):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.load_configuration()
        self.init_rect()

    def load_configuration(self):
        config = self.get_configuration("collection")
        self.coordinates = config["coordinates"]
        self.size = config["size"]

    def init_rect(self):
        Rect.__init__(self, self.coordinates, self.size)
from os.path import join
from glob import glob

from pygame import Surface
from pygame.image import load

from lib.pgfw.pgfw.GameChild import GameChild
from food_spring.gaia.Swapper import Swapper

class Gaia(GameChild):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.offset = self.get_configuration("gaia", "offset")
        self.set_images()
        self.swapper = Swapper(self)

    def set_images(self):
        images = []
        for path in sorted(glob(join(self.get_resource("gaia", "path"),
                                     "*.png"))):
            images.append(load(path).convert_alpha())
        self.images = images
from random import shuffle, choice

from pygame import Surface
from pygame.time import get_ticks
from pygame.locals import *

from lib.pgfw.pgfw.GameChild import GameChild
from lib.pgfw.pgfw.Sprite import Sprite

class Swapper(GameChild, Surface):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.load_configuration()
        self.init_surface()
        self.set_segments()

    def load_configuration(self):
        config = self.get_configuration("introduction")
        self.segment_count = config["segments"]
        self.transition_time = config["transition"]

    def init_surface(self):
        parent = self.parent
        offset = parent.offset
        line, spread  = parent.images
        Surface.__init__(self, (line.get_width(), line.get_height() + offset),
                         SRCALPHA)
        self.blit(line, (0, 0))
        self.blit(spread, (0, offset))
        rect = self.get_rect()
        rect.left = self.get_display_surface().get_width() / 2 - \
                    self.get_width() / 2
        self.rect = rect

    def set_segments(self):
        width, height = self.get_size()
        count = self.segment_count
        segment_width = width / count
        remainder = width % count
        x = 0
        segments = []
        for _ in xrange(count):
            adjusted_width = segment_width + (remainder > 0)
            segments.append(Segment(self, x, 0, adjusted_width, height))
            x += adjusted_width
            remainder -= 1
        self.segments = segments

    def swap(self, p, q):
        if isinstance(p, int):
            p, q = self.segments[p], self.segments[q]
        if not (p.moving() or q.moving()):
            p.set_destination(q.location.right)
            q.set_destination(p.location.right)

    def update(self):
        for segment in self.segments:
            segment.update()
        self.swap(choice(self.segments), choice(self.segments))


class Segment(Sprite):

    def __init__(self, parent, *args):
        Sprite.__init__(self, parent)
        self.add_frame(self.parent.subsurface(args).copy())
        self.location.topleft = args[0] + self.parent.rect.left, args[1]
        self.destination = self.location.right

    def moving(self):
        return self.destination != self.location.right

    def set_destination(self, right):
        self.destination = right
        self.distance = abs(self.location.right - right)
        self.last_ticks = get_ticks()

    def update(self):
        right = self.location.right
        destination = self.destination
        ticks = get_ticks()
        if self.moving():
            elapsed = float(ticks - self.last_ticks)
            step = int(self.distance * elapsed / self.parent.transition_time) \
                   or 1
            if destination < right:
                step = -step
            self.move(step)
            if abs(step) >= abs(self.location.right - destination):
                self.location.right = destination
        self.last_ticks = ticks
        Sprite.update(self)
18.97.14.88
18.97.14.88
18.97.14.88
 
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 😇