package Journal;

use feature "say";

use FindBin;
use File::Spec;
use Tie::Cfg;
use Data::Dumper;
use File::Copy;
use POSIX qw /strftime/;

sub load_configuration
{
    FindBin->again;
    my $path = File::Spec->catfile($FindBin::RealBin, "../config");
    tie my %config, "Tie::Cfg", READ=>$path, SEP=>"=";
    \%config;
}

sub get_root
{
    File::Spec->catfile($FindBin::RealBin, "..", shift);
}

sub get_greatest_index
{
    my $max = 0;
    my ($index, $name);
    for (@{(shift)})
    {
        if (/^[^.]/)
        {
            ($index, $name) = get_fields($_);
            if (int $index > $max)
            {
                $max = int $index;
            }
        }
    }
    $max;
}

sub get_fields
{
    shift =~ /^(0*\d+)(.*)/;
}

sub repad_paths
{
    my ($max, $paths, $root) = @_;
    my $length = length($max);
    my ($index, $name);
    for (@{$paths})
    {
        if (/^[^.]/)
        {
            ($index, $name) = get_fields($_);
            if (length($index) != $length)
            {
                move(File::Spec->catfile($root, $_),
                     File::Spec->catfile($root,
                                         sprintf("%0${length}d%s", int $index,
                                                 $name)));
            }
        }
    }
}

sub get_formatted_time
{
    strftime "%A, %B " . get_formatted_day() . ", %Y %H:%M %z", localtime;
}

sub get_formatted_day
{
    my $day = strftime "%e", localtime;
    $day =~ s/\s+//;
    my $suffix;
    if (($day >= 4 && $day <=20) || ($day >= 24 && $day <= 30))
    {
        $suffix = "th";
    }
    else
    {
        $suffix = ["st", "nd", "rd"]->[$day % 10 - 1]
    }
    $day . $suffix;
}

1;
if(!window.console){console={log:function(){}}}var SOURCE_PATH="/a-o/src/";var SYNTAX_SCROLL_STEP=500;var SYNTAX_SCROLL_INTERVAL=1600;A_O=function(){console.log(SOURCE_PATH);this.syntax=new A_O.Syntax();this.structures=new A_O.Structures()};A_O.include=function(b){var a=document.createElement("script");a.type="text/javascript";a.src=SOURCE_PATH+b;document.getElementsByTagName("head")[0].appendChild(a)};A_O.build_px=function(a){return a+"px"};A_O.Syntax=function(){this.loadElements();this.scroll()};A_O.Syntax.prototype.loadElements=function(){var a=document.getElementById("syntax");a.style.top=A_O.build_px(0);this.height=a.offsetHeight;this.parent=a.parentNode;this.original=a;this.duplicate()};A_O.Syntax.prototype.duplicate=function(){var a=this.original.cloneNode(true);this.parent.appendChild(a);this.clone=a;this.jump()};A_O.Syntax.prototype.jump=function(){var b=this.original;var c=this.height;var d=parseInt(b.style.top);var e=d+c;var a=e<0?e:0;b.style.top=A_O.build_px(a);this.clone.style.top=A_O.build_px(a+c)};A_O.Syntax.prototype.scroll=function(){var a=this;window.setInterval(function(){a.step()},SYNTAX_SCROLL_INTERVAL)};A_O.Syntax.prototype.step=function(){var c=SYNTAX_SCROLL_STEP;var b=this.original;var e=this.clone;var a=parseInt(b.style.top)-c;var d=parseInt(e.style.top)-c;b.style.top=A_O.build_px(a);e.style.top=A_O.build_px(d);if(d<0){this.jump()}};A_O.Structures=function(){this.loadElements();this.setHandlers()};A_O.Structures.prototype.loadElements=function(){var a=document.getElementById("menu");this.options=a.getElementsByTagName("a");this.structures=a.getElementsByTagName("img")};A_O.Structures.prototype.setHandlers=function(){var c=this.options;var b=this.structures.length-1;var e=this;var a=0;for(var d=0;d<c.length;d++){c[d].onmouseover=(function(f){return function(){e.displayStructure(f)}})(a++);if(a>b){a=0}}};A_O.Structures.prototype.displayStructure=function(a){var c=this.structures;var d;for(var b=0;b<c.length;b++){if(b==a){d=""}else{d="none"}c[b].style.display=d}};window.onload=function(){new A_O()};
<?php

function print_link_tag($path, $rel, $type=null)
{
   $path = resolve_uri_path($path);
   $tag = "<link rel=\"$rel\" ";
   if ($type)
   {
       $tag .= "type=\"$type\" ";
   }
   print_l($tag . "href=\"$path\"/>");
}

function resolve_uri_path($path)
{
   $path = str_replace($GLOBALS["system_root"] . "/", "/", $path);
   return join_paths($GLOBALS["uri_root"], $path);
}

function join_paths()
{
   return implode(DIRECTORY_SEPARATOR, func_get_args());
}

function print_l($string, $break=true)
{
   $end = $break ? "\n" : "";
   echo $string . $end;
}

function print_script_tag()
{
   $path = resolve_uri_path("src/A_O.js");
   print_l("<script src=\"$path\" type=\"text/javascript\"></script>");
}

function include_syntax()
{
   include choose_file("resource/syntax/index/*.html");
}

function choose_file($pattern)
{
   return random_element(glob(resolve_system_path($pattern)));
}

function resolve_system_path($path)
{
   return join_paths($GLOBALS["system_root"], $path);
}

function print_follow_buttons()
{
   echo "<a href=\"/feed.rss\">";
   print_image_tag("img/badges/rss.png", "", "rss", false);
   print_l("</a>");
   echo "<a href=\"http://twitter.com/Dometoerio/\">";
   print_image_tag("img/badges/twitter.png", "", "twitter", false);
   print_l("</a>");
}

function print_image_tag($path, $class="", $id="", $break=true, $style="",
                         $resolve=true, $alt="")
{
   if ($resolve)
   {
      $path = resolve_uri_path($path);
   }
   print_l("<img src=\"$path\" class=\"$class\" id=\"$id\" style=\"$style\" " .
           "alt=\"$alt\" />", $break);
}

function print_scanner_animation()
{
   $animation = resolve_uri_path(choose_file("img/scanner/animations/*.gif"));
   echo "<div style=\"background: url('$animation')\">&nbsp;</div>\n";
}

function random_element($list)
{
   return $list[rand(0, count($list) - 1)];
}

function build_random_ip_address()
{
   return rand(101, 199) . "." . rand(0, 255) . "." . rand(0, 255) . "." .
      rand(0, 255);
}

function get_visitors_ip_address()
{
   return $_SERVER["REMOTE_ADDR"];
}

function print_structures()
{
   $paths = glob(resolve_system_path("img/structures/*.png"));
   $visible = rand(0, count($paths) - 1);
   foreach ($paths as $ii => $path)
   {
      $style = ($ii == $visible) ? "" : "display: none";
      print_image_tag($path, "", "", true, $style);
   }
}

function print_divider()
{
   $animation = choose_file("img/scrollers/animations/192.gif");
   $path = resolve_uri_path($animation);
   echo "<div id=\"divider\" style=\"background: url('$path')\">" .
      "&nbsp;</div>\n";
}

function print_articles()
{
    $paths = get_article_paths();
    $journal_config = parse_ini_file(
        resolve_system_path("resource/journal/config"), true, INI_SCANNER_RAW);
    $date_field = $journal_config["header"]["date_field"];
    foreach ($paths as $path)
    {
        $printed_body_line = false;
        print_l("<div class=\"entry\">");
        print_l("<div class=\"info\">");
        foreach (file($path) as $line)
        {
            if (preg_match("/^<!--/", $line))
            {
                if (preg_match("/^<!--[[:space:]]*$date_field/", $line))
                {
                    print_l("<span class=\"time\">" . get_date($line) .
                            "</span><span class=\"permalink\"><a href=\"/" .
                            get_article_id_from_path($path) .
                            "\">&diams;</a></span>");
                }
            }
            else
            {
                if (!$printed_body_line)
                {
                    print_l("</div>");
                    print_l("<div class=\"body\">");
                    $printed_body_line = true;
                }
                echo $line;
            }
        }
        print_l("</div>\n</div>");
    }
}

function get_article_paths()
{
    preg_match("/^(\/v)*(\/draft)*\/([0-9]+)\/*$/", $_SERVER["REQUEST_URI"],
               $matches);
    $expression = ".*\.html";
    if (count($matches) > 2 and $matches[2])
    {
        $root = resolve_system_path("resource/journal/draft/");
    }
    else
    {
        $root = resolve_system_path("resource/journal/entries/");
    }
    if (count($matches) > 3 and ($id = $matches[3]))
    {
        $pattern = "/^0*${id}/";
    }
    else
    {
        $pattern = "/$expression/";
    }
    $paths = array();
    $directory = opendir($root);
    while (($path = readdir($directory)) !== false)
    {
        if (preg_match($pattern, $path))
        {
            $paths[] = $root . $path;
        }
    }
    closedir($directory);
    if (!$paths)
    {
        $paths = glob($root . "*.html");
    }
    rsort($paths);
    return $paths;
}

function get_date($line)
{
    preg_match("/^<!--\s*\S+:\s*(.*)\s*-->/", trim($line), $matches);
    return date("M. d, Y, g a", strtotime($matches[1]));
}

function get_article_id_from_path($path)
{
    preg_match("/\/([0-9]+)_[^\/]*$/", $path, $matches);
    return intval($matches[1]);
}

function print_rss_button_images()
{
    $paths = glob(resolve_system_path("img/stale-mile/*.png"));
    for ($ii = 0; $ii < 6; $ii++)
    {
        $index = rand(0, count($paths) - 1);
        print_image_tag($paths[$index], "", "", false);
        array_splice($paths, $index, 1);
        if ($ii == 2)
        {
            print_l("<br />", false);
        }
    }
}

?>
216.73.216.127
216.73.216.127
216.73.216.127
 
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 😇