from pygame.time import get_ticks

from GameChild import GameChild

class TimeFilter(GameChild):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.ticks = self.unfiltered_ticks = self.last_ticks = get_ticks()
        self.open()

    def close(self):
        self.closed = True

    def open(self):
        self.closed = False

    def get_ticks(self):
        return self.ticks

    def get_unfiltered_ticks(self):
        return self.unfiltered_ticks

    def get_last_ticks(self):
        return self.last_ticks

    def get_last_frame_duration(self):
        return self.last_frame_duration

    def update(self):
        ticks = get_ticks()
        self.last_frame_duration = duration = ticks - self.last_ticks
        if not self.closed:
            self.ticks += duration
        self.unfiltered_ticks += duration
        self.last_ticks = ticks
class Vector(list):

    def __init__(self, x=0, y=0):
        list.__init__(self, (x, y))

    def __getattr__(self, name):
        if name == "x":
            return self[0]
        elif name == "y":
            return self[1]

    def __setattr__(self, name, value):
        if name == "x":
            self[0] = value
        elif name == "y":
            self[1] = value
        else:
            list.__setattr__(self, name, value)

    def __add__(self, other):
        return Vector(self.x + other[0], self.y + other[1])

    __radd__ = __add__

    def __iadd__(self, other):
        self.x += other[0]
        self.y += other[1]
        return self

    def __sub__(self, other):
        return Vector(self.x - other[0], self.y - other[1])

    def __rsub__(self, other):
        return Vector(other[0] - self.x, other[1] - self.y)

    def __isub__(self, other):
        self.x -= other[0]
        self.y -= other[1]
        return self

    def __mul__(self, other):
        return Vector(self.x * other, self.y * other)

    __rmul__ = __mul__

    def __imul__(self, other):
        self.x *= other
        self.y *= other
        return self

    def apply_to_components(self, function):
        self.x = function(self.x)
        self.y = function(self.y)

    def place(self, x=None, y=None):
        if x is not None:
            self.x = x
        if y is not None:
            self.y = y

    def move(self, dx=0, dy=0):
        if dx:
            self.x += dx
        if dy:
            self.y += dy

    def place_at_origin(self):
        self.x = 0
        self.y = 0
from os import listdir
from os.path import isfile, join
from sys import exc_info, stdout
from glob import glob
from traceback import print_exc, print_stack

from pygame import Color, Rect, Surface
from pygame.image import load
from pygame.transform import flip
from pygame.locals import *

from Animation import Animation
from Vector import Vector

class Sprite(Animation):

    def __init__(self, parent, framerate=None):
        Animation.__init__(self, parent, self.shift_frame, framerate)
        self.frames = []
        self.mirrored = False
        self.alpha = 255
        self.locations = []
        self.framesets = [Frameset(self, framerate=framerate)]
        self.set_frameset(0)
        self.locations.append(Location(self))
        self.motion_overflow = Vector()
        self.display_surface = self.get_display_surface()

    def __getattr__(self, name):
        if name in ("location", "rect"):
            return self.locations[0]
        if hasattr(Animation, "__getattr__"):
            return Animation.__getattr__(self, name)
        raise AttributeError, name

    def set_frameset(self, identifier):
        if isinstance(identifier, str):
            for ii, frameset in enumerate(self.framesets):
                if frameset.name == identifier:
                    identifier = ii
                    break
        self.frameset_index = identifier
        self.register_interval()
        self.update_location_size()
        if self.get_current_frameset().length() > 1:
            self.play()

    def register_interval(self):
        self.register(self.shift_frame,
                      interval=self.get_current_frameset().framerate)

    def get_current_frameset(self):
        return self.framesets[self.frameset_index]

    def update_location_size(self):
        size = self.get_current_frameset().rect.size
        for location in self.locations:
            location.size = size
            location.fader.init_surface()

    def set_framerate(self, framerate):
        self.get_current_frameset().set_framerate(framerate)
        self.register_interval()

    def load_from_path(self, path, transparency=False, ppa=True, key=None,
                       extension=None, omit=False):
        if isfile(path):
            paths = [path]
        else:
            if extension:
                paths = sorted(glob(join(path, "*." + extension)))
            else:
                paths = [join(path, name) for name in sorted(listdir(path))]
        for path in paths:
            img = load(path)
            if transparency:
                if ppa:
                    frame = img.convert_alpha()
                else:
                    frame = self.fill_colorkey(img, key)
            else:
                frame = img.convert()
            self.add_frame(frame, omit)

    def fill_colorkey(self, img, key=None):
        if not key:
            key = (255, 0, 255)
        img = img.convert_alpha()
        frame = Surface(img.get_size())
        frame.fill(key)
        frame.set_colorkey(key)
        frame.blit(img, (0, 0))
        return frame

    def add_frame(self, frame, omit=False):
        self.frames.append(frame)
        frame.set_alpha(self.alpha)
        if not omit:
            frameset = self.get_current_frameset()
            frameset.add_index(self.frames.index(frame))
            self.update_location_size()
            if frameset.length() > 1:
                self.play()

    def shift_frame(self):
        self.get_current_frameset().shift()

    def get_current_frame(self):
        return self.frames[self.get_current_frameset().get_current_id()]

    def move(self, dx=0, dy=0):
        for location in self.locations:
            location.move_ip(dx, dy)

    def reset_motion_overflow(self):
        for location in self.locations:
            location.reset_motion_overflow()

    def collide(self, other):
        if not isinstance(other, Rect):
            other = other.rect
        for location in self.locations:
            if location.colliderect(other):
                return location

    def mirror(self):
        frames = self.frames
        for ii, frame in enumerate(frames):
             frames[ii] = flip(frame, True, False)
        self.mirrored = not self.mirrored

    def clear_frames(self):
        self.frames = []
        for frameset in self.framesets:
            frameset.order = []
            frameset.reset()
            frameset.measure_rect()

    def add_location(self, topleft=None, offset=(0, 0), count=1, base=0):
        if topleft is not None:
            for ii in xrange(count):
                self.locations.append(Location(
                    self, Rect(topleft, self.locations[0].size)))
        else:
            base = self.locations[base]
            current_offset = list(offset)
            for ii in xrange(count):
                self.locations.append(Location(self,
                                               base.move(*current_offset)))
                current_offset[0] += offset[0]
                current_offset[1] += offset[1]
        return self.locations[-1]

    def fade(self, length=0, out=None, index=None):
        if index is None:
            for location in self.locations:
                location.fader.start(length, out)
        else:
            self.locations[index].fader.start(length, out)

    def set_alpha(self, alpha):
        self.alpha = alpha
        for frame in self.frames:
            frame.set_alpha(alpha)
        for location in self.locations:
            location.fader.set_alpha()

    def add_frameset(self, order=[], framerate=None, name=None, switch=False):
        frameset = Frameset(self, order, framerate, name)
        self.framesets.append(frameset)
        if switch:
            self.set_frameset(len(self.framesets) - 1)
        return frameset

    def hide(self):
        for location in self.locations:
            location.hide()

    def unhide(self):
        for location in self.locations:
            location.unhide()

    def is_hidden(self):
        return all(location.is_hidden() for location in self.locations)

    def remove_locations(self, location=None):
        if location:
            self.locations.remove(location)
        else:
            self.locations = self.locations[:1]

    def reverse(self, frameset=None):
        if frameset:
            frameset.reverse()
        else:
            for frameset in self.framesets:
                frameset.reverse()

    def update(self, flags=0):
        Animation.update(self)
        self.draw(flags)

    def draw(self, flags=0):
        for location in self.locations:
            location.fader.draw(flags)


class Location(Rect):

    def __init__(self, sprite, rect=(0, 0, 0, 0)):
        self.sprite = sprite
        Rect.__init__(self, rect)
        self.motion_overflow = Vector()
        self.fader = Fader(self)
        self.unhide()

    def move_ip(self, dx, dy):
        if isinstance(dx, float) or isinstance(dy, float):
            excess = self.update_motion_overflow(dx, dy)
            Rect.move_ip(self, int(dx) + excess[0], int(dy) + excess[1])
        else:
            Rect.move_ip(self, dx, dy)

    def update_motion_overflow(self, dx, dy):
        overflow = self.motion_overflow
        overflow.move(dx - int(dx), dy - int(dy))
        excess = map(int, overflow)
        overflow[0] -= int(overflow[0])
        overflow[1] -= int(overflow[1])
        return excess

    def reset_motion_overflow(self):
        self.motion_overflow.place_at_origin()

    def hide(self):
        self.hidden = True

    def unhide(self):
        self.hidden = False

    def is_hidden(self):
        return self.hidden


class Fader(Surface):

    def __init__(self, location):
        self.location = location
        self.time_filter = location.sprite.get_game().time_filter
        self.reset()

    def reset(self):
        self.init_surface()
        self.fade_remaining = None

    def init_surface(self):
        Surface.__init__(self, self.location.size)
        if self.location.sprite.get_current_frameset().length():
            background = Surface(self.get_size())
            sprite = self.location.sprite
            key = sprite.get_current_frame().get_colorkey() or (255, 0, 255)
            self.set_colorkey(key)
            background.fill(key)
            self.background = background
            self.set_alpha()

    def set_alpha(self, alpha=None):
        if alpha is None:
            alpha = self.location.sprite.alpha
        Surface.set_alpha(self, alpha)

    def start(self, length, out=None):
        if self.fade_remaining <= 0:
            alpha = self.get_alpha()
            maximum = self.location.sprite.alpha
            if out is None:
                out = alpha == maximum
            if out and alpha > 0 or not out and alpha < maximum:
                self.fade_length = self.fade_remaining = length
                self.start_time = self.time_filter.get_ticks()
                self.fading_out = out

    def draw(self, flags):
        sprite = self.location.sprite
        if self.fade_remaining >= 0:
            self.update_alpha()
            self.clear()
            frame = sprite.get_current_frame()
            frame.set_alpha(255)
            self.blit(frame, (0, 0))
            frame.set_alpha(sprite.alpha)
            if not self.location.hidden:
                self.blit_to_display(self, flags)
        elif self.fade_remaining is None or self.get_alpha() >= sprite.alpha:
            if self.fade_remaining >= 0:
                self.update_alpha()
            if not self.location.hidden:
                self.blit_to_display(sprite.get_current_frame(), flags)

    def blit_to_display(self, frame, flags):
        self.location.sprite.display_surface.blit(frame, self.location, None,
                                                  flags)

    def update_alpha(self):
        remaining = self.fade_remaining = self.fade_length - \
                    (self.time_filter.get_ticks() - self.start_time)
        ratio = self.fade_length and float(remaining) / self.fade_length
        if not self.fading_out:
            ratio = 1 - ratio
        maximum = self.location.sprite.alpha
        alpha = int(ratio * maximum)
        if alpha > maximum:
            alpha = maximum
        elif alpha < 0:
            alpha = 0
        self.set_alpha(alpha)

    def clear(self):
        self.blit(self.background, (0, 0))


class Frameset():

    def __init__(self, sprite, order=[], framerate=None, name=None):
        self.sprite = sprite
        self.name = name
        self.reversed = False
        self.order = []
        self.rect = Rect(0, 0, 0, 0)
        self.add_index(order)
        self.set_framerate(framerate)
        self.reset()

    def add_index(self, order):
        if isinstance(order, int):
            order = [order]
        self.order += order
        self.measure_rect()

    def set_framerate(self, framerate):
        self.framerate = framerate

    def reset(self):
        self.current_index = 0

    def get_current_id(self):
        return self.order[self.current_index]

    def measure_rect(self):
        max_width, max_height = 0, 0
        frames = self.sprite.frames
        for index in self.order:
            frame = frames[index]
            width, height = frame.get_size()
            max_width = max(width, max_width)
            max_height = max(height, max_height)
        self.rect.size = max_width, max_height

    def shift(self):
        if len(self.order) > 1:
            self.increment_index()

    def increment_index(self):
        increment = 1 if not self.reversed else -1
        index = self.current_index + increment
        if index < 0:
            index = self.length() - 1
        elif index >= self.length():
            index = 0
        self.current_index = index

    def length(self):
        return len(self.order)

    def reverse(self):
        self.reversed = not self.reversed
216.73.216.185
216.73.216.185
216.73.216.185
 
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 😇