GioCities

blogs by Gio

⚙ ACNH Printer - a writeup!

Posted by Gio in dev

This is a writeup of a project I did in April but never released. Well, I’ve definitely released it now, if you want to give it a try!

Instead of a real introduction, here’s a video demo, with camcorder LP technology from 2005:

I am not going to buy a capture card

Ever since Wild World, Animal Crossing has had a pattern system, where players can design their own textures and use them as clothes or decoration. New Horizons has one, but since it doesn’t have a stylus you have to either use the directional pad to mark individual pixels or draw with your fingertip.

I thought it would be fun to find a way to automate that. Now, granted, it takes a while, but it’s still much faster than trying to copy pixels over by hand.

The USB bit

Using work done by shinyquagsire23’s Switch Flightstick project, you can use a Teensy board to simulate a controller and send commands programatically.

The Teensy only has a tiny amount of memory, though, so any program program has to be pretty small. This rules out, say, loading a 32x32 png file and some commands to the board; you need to generate a compressed instruction set on a computer and compile that.

The engine I wrote for the Teensy is fairly simple: I define a series of steps in a large array, and then iterate through that:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include "types.h"
uint8_t step[] = {
    8, 8, 1, // X (SetDrawingF) x 1
    0, 0, 1, // HAT_UP x 1
    4, 8, 1, // A (SetTool) x 1
    0, 8, 1, // NONE x 1
    0, 4, 1, // HAT_DOWN x 1
    0, 8, 1, // NONE x 1
    4, 8, 1, // A (ResetPalette) x 1
    0, 8, 1, // NONE x 1
    0, 2, 1, // HAT_RIGHT x 1
    4, 8, 1, // A (SetTool) x 1
    ...

(It also appends some boilerplate for setting up the controller, like pressing the triggers together.)

My code treats each group of 3 uint8s as a logical frame, and sends one frame every 12 cycles. This times to just about 30 fps.

The logical frame itself is extremely compressed: I never need to touch the analog stick, just the buttons and the hat. The first number is the data set for the hat, the second number is the data sent for the buttons, and the last number is the number of times that frame is repeated. Button data and hat data here are, of course, bitmasks, using the descriptors in types.h.

When the Teensy is flashed and plugged in to the Switch via USB, it starts pressing buttons, until it has pressed all the buttons, and then it stops.

That’s enough for the Teensy to run simple programs, so long as I can write and compile them.

Generating the program

So, with that working, you still need to do all the logical work of figuring out what buttons to press, and in what order.

First, a breakdown of how patterns actually work:

The canvas1 is 32x32, and you can draw on it with a number of simple, MSPaint style tools. For this project, I use the pencil and the global fill tool, along with the “mirror” option. There are likely more optimizations you could do by using more tools, but I haven’t done that yet.

In the New Horizons pattern designer, you can define your own color palette. You get 15 color slots to work with, and each color is defined by hue, saturation, and value. There are 30 hue increments, and 15 increments each for saturation and value.

Now, the first thing you need to do, strictly speaking, is generate a 32x32 image file with only 15 unique colors. I don’t automate that, I just use Paint.NET. Then you open that file with makesteps.py, which is where all the logic for the program lives.

The general system for generating the instruction set is thus:

  1. Load an image
  2. Create a virtual 32x32 canvas and set it to the defaults the game uses
  3. Generate a game-ready palette
  4. Incrementally edit the virtual canvas using the tools available in the game, recording each step
  5. Convert that series of steps to button presses
  6. Flash the Teensy and let it run

1. Load an image

When I load the image, I validate it. It needs to be 32x32, it needs to have a palette of 15 colors or fewer, etc.

2. Create a virtual canvas

In order to make sure I draw the image correctly — and so it’s easy to check for errors — I make a virtual canvas and simulate the entire drawing process.

The actual virtual canvas is a simple python object:

1
2
3
4
5
6
7
8
class ACCanvas():
    def __init__(self, width, height):
        super(ACCanvas, self).__init__()
        self.w = width
        self.h = height
        self.canvas = [[None for x in range(width)] for y in range(height)]
        self.palette = None
        self.steps = []

Here, the pixel values are stored in self.canvas, which is just a two-dimentional list initialized to all Nones. The ACCanvas class is also where I store helper functions that work on the canvas array, like genPalette to determine what colors are on the canvas, or toImage to create a PIL.Image version I can save as a png.

This is especially nice because it lets me export the finished image locally. Because of the color limitations of the game, the colors in the original image will be clamped somewhat, and the virtual canvas lets me test what that looks like.

ACNH also uses the 6xBRZ scaling algorithm for displaying patterns in-game. 6xBRZ is a pixel-scaling algorithm, so instead of pixel art, the final pattern is made of curves and triangles. The pixels are replaced with lines and shapes that are meant to better represent the meaning of the shape.

Here is an example, where the input file is pixel art of a horse fighting a football player:

Without xBRZ scaling With xBRZ scaling

3. Generate a game-ready palette

Now I take all the colors in the input image and put together a palette that can be used in the game.

The first and most obvious step is converting the colors: as mentioned before, ACNH has 30 possible hues and 15 possible saturations and values, whereas the input image has 256 possible states for each. The first order of business, then, is reducing the bits on these.

Here’s what that looks like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import colorsys

# Zero index
HUES = 30 - 1
SATS = 15 - 1
VALS = 15 - 1

def percToInt(max):
    def c(val):
        return round(val * max)
    return c


def rgbaToACColor(pixel):
    if pixel[3] == 0:
        # Transparent, bad
        # raise NotImplementedError
        return (-1, -1, -1)
    # Get RGB values from pixel tuple
    r, g, b, *_ = pixel
    # Convert to HSV 
    h, s, v = colorsys.rgb_to_hsv(r, g, b)
    # Colorsys returns V as an int from 0-255, so turn that into a percentage.
    # s and v are already floats from 0.0-1.0, so no correction needed.
    v /= 255
    tup = (percToInt(HUES)(h), percToInt(SATS)(s), percToInt(VALS)(v))
    return tup

This gives us a nice hsv tuple with the game’s drastically reduced domain.

(The steps for this, by the way, are hideous: hundreds of individual ticks to configure the sliders exactly right for each color.)

This isn’t good yet, though, because there are a number of compression steps to do:

  • Set all colors with V=0 to the same hue (they’re white/grey/black)
  • Set all colors with S=0 to the same hue and vividness (they’re black)

Otherwise, we might waste multiple color slots on identical colors.

In the future, it might be possible to some sort of optimization by putting colors that frequently appear adjacent to each other in the image closer together on the palette, to reduce the number of color swaps.

4. Incrementally edit the virtual canvas

This is, by far, the most complicated part of the program, because we don’t just have to mark each pixel correctly, we need to do it efficiently. Otherwise it’ll take a very long time to run, or else it might not fit on the Teensy at all.

All our drawing methods are stored in a class called Printer; every actual printer is a class that inheirits from this. It has functions like moveTo to move the cursor to a pixel, setColor to change the selected color, setTool to set the selected tool, etc. More on the printer implementation in section 5.

The very simplest implementation of this is NaivePrinter, which simply visits every pixel on the canvas and marks it:

1
2
3
4
5
6
7
8
class NaivePrinter(Printer):

    def drawImageCustom(self, source):
        new_steps = []

        for x, y in self.smartTraverse(source):
            new_steps += self.markPixel(x, y, source.canvas[x][y])
        return new_steps

This isn’t quite as bad as it sounds, because smartTraverse tries to be somewhat sensible in how it traverses the canvas: by default it snakes around, so as to traverse the full canvas in one continuous line without wasting time by covering any spots it already visited: (4x4 canvas to demonstrate):

1
2
3
4
1  2  3  4
8  7  6  5
9  10 11 12
16 15 14 13

Now, I’ll be honest with you, I’m lousy2 with algorithms. Algorithms are obtuse, horrible little things that should be buried away in functions and never looked directly at. So, instead of doing any incredibly robust analysis of the source image to determine what method would correctly print the full image in the fewest number of steps, I just try a lot of different methods and check afterwards to see which one is fastest. Because each step takes a logical frame, it’s easy to quickly time this.

In addition to the NaivePrinter shown above, here are some other methodologies I use:

ScreenPrinter prints one color at a time:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class ScreenPrinter(Printer):

    def drawImageCustom(self, source):
        new_steps = []

        for color in source.palette:
            for x, y in self.smartTraverse(source):
                if source.canvas[x][y] == color:
                    new_steps += self.markPixel(x, y, color)
        return new_steps

This can save a significant amount of time depending on the source image, as changing color midway through the image can be expensive.

SpiralPrinter starts in the middle of the canvas (where your cursor defaults to) and traverses the canvas in a spiral outward.

There are also other settings I permute through, like smartTraversing vertically instead of horizontally, or setting the game’s “Mirror” mode to print two spaces at once, or using the “Fill All” tool at the beginning of the print.

5. Convert that series of steps to button presses

Each of these functions that edits the virtual canvas also yields a list of steps. In the python script, steps are stored as enums that represent button presses, with their values being (button, hat) tuples, as such:

1
2
3
4
5
6
7
8
9
class Step(enum.Enum):
    NONE = (0x0, 0x08)
    Y = (0x01, 0x08)
    B = (0x02, 0x08)
    A = (0x04, 0x08)
    ...
    HAT_UP = (0, 0x00)
    HAT_RIGHT = (0, 0x02)
    ...

As I edit the canvas, I collect those movements into a list, and at the end of the process I serialize that list to the teensy steps format, as described earlier.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Printer():
    ...

    def toSteps(self):
        new_steps = []
        self.source.genPalette()

        new_steps += self.setPalette(self.source.palette)
        new_steps.append(Step.NONE)
        new_steps += self.drawImage(self.source)
        return new_steps

Except I don’t just do that, because that would be extremely slow. If you did every step sequentially, you would end up wasting a ton of time, because there are a lot of steps that can run in parallel. By the time you actually press the A button and write the pixel, everything needs to be set properly. However, before then, multiple steps can be folded together.

Take, as an example, needing to mark a pixel that is 4 pixels away and 5 colors away. Moving the cursor uses the hat only, and changing the color uses the trigger buttons.

Instead of doing

  • Right x4 (move cursor)
  • LR x5 (change color)
  • A x1 (mark pixel)

sequentially, you can collapse the steps into

  • Right + LR x4 (move cursor & hange color)
  • LR x1 (change color)
  • A x1 (mark pixel)

See how this dramatically improves performance:

gantt
    axisFormat %S
    section Sequential
    Set color       :a1, 0, 5s
    Move cursor     :a2, after a1, 4s
    Mark spot       :a3, after a1 a2, 1s

    section Parallel
    Set color       :b1, 0, 5s
    Move cursor     :b2, 0, 4s
    Mark spot       :b3, after b1 b2, 1s

This algorithm actually implements step folding:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from itertools import zip_longest

def foldSteps(step1, step2):
    if step1.value[0] != 0x0 and step2.value[0] != 0x0: 
        # Both steps contain a button press, cannot fold
        return [step1, step2]
    if step1.value[0] == Step.LR.value[0] or step2.value[0] == Step.LR.value[0]:
        # At least one step contains LR, cannot fold
        return [step1, step2]
    if step1.value[1] != 0x8 and step2.value[1] != 0x8: 
        # Steps contain different hat directions, cannot fold
        return [step1, step2]
    else:
        return [PseudoStep(step1).plus(step2)]


def foldStepSeqs(steps1, steps2):
    if not steps1:
        return steps2
    if not steps2:
        return steps1

    new_steps_compressed = []   
    for xstep, ystep in zip_longest(steps1, steps2, fillvalue=Step.NONE):
        new_steps_compressed += foldSteps(xstep, ystep)
    return new_steps_compressed

In addition to this postprocessing, there’s additional compression and optimization throughout the step generation process. Printer.moveCursorTo(x, y) uses zip to group steps together and use diagonal hat movement when possible. Setting commands like setMirrored and setTool use state to keep track of the setting, and don’t require a step if the option is already set correctly.

6. Flash the Teensy and let it run

And I’m done! Just plug the teensy into the switch at the right moment and it will print the pattern autonomously.


That’s, uh, all I’ve got, really. If you’ve got a Teensy or $20 and want to print your own patterns, or you just want to take a closer look at how this was done, you’re welcome to grab the code.

Technical Demo

The program itself runs something like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
$ python3 makesteps.py --help
usage: makesteps.py [-h] [--dump | --no-dump] [--gen | --no-gen] [--preview | --no-preview] [--bogo | --no-bogo]
                    [--make | --no-make]
                    infile

positional arguments:
  infile        Input pattern file

optional arguments:
  -h, --help    show this help message and exit
  --dump        Save pattern (Default: True)
  --no-dump     Save pattern (Default: True)
  --gen         Generate steps file (Default: True)
  --no-gen      Generate steps file (Default: True)
  --preview     Generate scaled preview file (Default: True)
  --no-preview  Generate scaled preview file (Default: True)
  --bogo        Find fastest solution (Default: True)
  --no-bogo     Find fastest solution (Default: True)
  --make        Automatically run make after completion (Default: False)
  --no-make     Automatically run make after completion (Default: False)

And, running it: (this is the configuration used in the demo video, with SpiralPrinter temporarily disabled)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
$ python3 makesteps.py s/despair.png --make
(32, 32)
Generated preview at scalepreview.png
<class '__main__.NaivePrinter'> (00100) printed pattern in 1582 steps (593.25 bytes) (~5m 16s runtime)
BEST!
<class '__main__.NaivePrinter'> (00101) printed pattern in 1582 steps (593.25 bytes) (~5m 16s runtime)
<class '__main__.NaivePrinter'> (00110) printed pattern in 1106 steps (414.75 bytes) (~3m 41s runtime)
BEST!
<class '__main__.NaivePrinter'> (00111) printed pattern in 1110 steps (416.25 bytes) (~3m 42s runtime)
<class '__main__.NaivePrinter'> (01000) printed pattern in 1512 steps (567.0 bytes) (~5m 2s runtime)
<class '__main__.NaivePrinter'> (01001) printed pattern in 1512 steps (567.0 bytes) (~5m 2s runtime)
<class '__main__.NaivePrinter'> (01010) printed pattern in 1047 steps (392.625 bytes) (~3m 29s runtime)
BEST!
<class '__main__.NaivePrinter'> (01011) printed pattern in 1047 steps (392.625 bytes) (~3m 29s runtime)
<class '__main__.NaivePrinter'> (10100) printed pattern in 1246 steps (467.25 bytes) (~4m 9s runtime)
<class '__main__.NaivePrinter'> (10101) printed pattern in 1253 steps (469.875 bytes) (~4m 10s runtime)
<class '__main__.NaivePrinter'> (10110) printed pattern in 933 steps (349.875 bytes) (~3m 6s runtime)
BEST!
<class '__main__.NaivePrinter'> (10111) printed pattern in 941 steps (352.875 bytes) (~3m 8s runtime)
<class '__main__.NaivePrinter'> (11000) printed pattern in 1469 steps (550.875 bytes) (~4m 53s runtime)
<class '__main__.NaivePrinter'> (11001) printed pattern in 1469 steps (550.875 bytes) (~4m 53s runtime)
<class '__main__.NaivePrinter'> (11010) printed pattern in 1031 steps (386.625 bytes) (~3m 26s runtime)
<class '__main__.NaivePrinter'> (11011) printed pattern in 1031 steps (386.625 bytes) (~3m 26s runtime)
<class '__main__.ScreenPrinter'> (00100) printed pattern in 1641 steps (615.375 bytes) (~5m 28s runtime)
<class '__main__.ScreenPrinter'> (00101) printed pattern in 1634 steps (612.75 bytes) (~5m 26s runtime)
<class '__main__.ScreenPrinter'> (00110) printed pattern in 1115 steps (418.125 bytes) (~3m 43s runtime)
<class '__main__.ScreenPrinter'> (00111) printed pattern in 1108 steps (415.5 bytes) (~3m 41s runtime)
<class '__main__.ScreenPrinter'> (01000) printed pattern in 1749 steps (655.875 bytes) (~5m 49s runtime)
<class '__main__.ScreenPrinter'> (01001) printed pattern in 1735 steps (650.625 bytes) (~5m 47s runtime)
<class '__main__.ScreenPrinter'> (01010) printed pattern in 1085 steps (406.875 bytes) (~3m 37s runtime)
<class '__main__.ScreenPrinter'> (01011) printed pattern in 1071 steps (401.625 bytes) (~3m 34s runtime)
<class '__main__.ScreenPrinter'> (10100) printed pattern in 1283 steps (481.125 bytes) (~4m 16s runtime)
<class '__main__.ScreenPrinter'> (10101) printed pattern in 1277 steps (478.875 bytes) (~4m 15s runtime)
<class '__main__.ScreenPrinter'> (10110) printed pattern in 980 steps (367.5 bytes) (~3m 16s runtime)
<class '__main__.ScreenPrinter'> (10111) printed pattern in 974 steps (365.25 bytes) (~3m 14s runtime)
<class '__main__.ScreenPrinter'> (11000) printed pattern in 1697 steps (636.375 bytes) (~5m 39s runtime)
<class '__main__.ScreenPrinter'> (11001) printed pattern in 1691 steps (634.125 bytes) (~5m 38s runtime)
<class '__main__.ScreenPrinter'> (11010) printed pattern in 1063 steps (398.625 bytes) (~3m 32s runtime)
<class '__main__.ScreenPrinter'> (11011) printed pattern in 1057 steps (396.375 bytes) (~3m 31s runtime)
<__main__.NaivePrinter object at 0x000001F4576CCE20>
{'startfill': True, 'usemirror': True, 'vertical': True, 'horizontal': False, 'adjpalette': False}
makefile:59: warning: overriding recipe for target 'clean'
../LUFA/LUFA/Build/DMBS/DMBS/gcc.mk:182: warning: ignoring old recipe for target 'clean'
 [GCC]     : Compiling C file "nextreport.c"
avr-gcc -c -pipe -gdwarf-2 -g2 -mmcu=at90usb1286 -fshort-enums -fno-inline-small-functions -fpack-struct -Wall -fno-strict-aliasing -funsigned-char -funsigned-bitfields -ffunction-sections -I. -DARCH=ARCH_AVR8 -DF_CPU=16000000UL -mrelax -fno-jump-tables -x c -Os -std=gnu99 -Wstrict-prototypes -DUSE_LUFA_CONFIG_HEADER -IConfig/ -I. -I../LUFA/LUFA/.. -DARCH=ARCH_AVR8 -DBOARD=BOARD_NONE -DF_USB=16000000UL  -MMD -MP -MF obj/nextreport.d nextreport.c -o obj/nextreport.o
 [LNK]     : Linking object files into "Joystick.elf"
avr-gcc obj/nextreport.o obj/Joystick.o obj/Descriptors.o obj/HIDParser.o obj/Device_AVR8.o obj/EndpointStream_AVR8.o obj/Endpoint_AVR8.o obj/Host_AVR8.o obj/PipeStream_AVR8.o obj/Pipe_AVR8.o obj/USBController_AVR8.o obj/USBInterrupt_AVR8.o obj/ConfigDescriptors.o obj/DeviceStandardReq.o obj/Events.o obj/HostStandardReq.o obj/USBTask.o -o Joystick.elf -lm -Wl,-Map=Joystick.map,--cref -Wl,--gc-sections -Wl,--relax -mmcu=at90usb1286
 [OBJCPY]  : Extracting HEX file data from "Joystick.elf"
avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature Joystick.elf Joystick.hex
g++   -c steps.c
g++   -c nextreport.c
g++  -o test.exe nextreport.o test.o
 [INFO]    : Begin compilation of project "Joystick"...

avr-gcc.exe (GCC) 4.8.2 20131010 (prerelease)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 [OBJCPY]  : Extracting EEP file data from "Joystick.elf"
avr-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings Joystick.elf Joystick.eep || exit 0
 [OBJCPY]  : Extracting BIN file data from "Joystick.elf"
avr-objcopy -O binary -R .eeprom -R .fuse -R .lock -R .signature Joystick.elf Joystick.bin
 [OBJDMP]  : Extracting LSS file data from "Joystick.elf"
avr-objdump -h -d -S -z Joystick.elf > Joystick.lss
 [NM]      : Extracting SYM file data from "Joystick.elf"
avr-nm -n Joystick.elf > Joystick.sym
 [SIZE]    : Determining size of "Joystick.elf"

avr-size --mcu=at90usb1286 --format=avr Joystick.elf
AVR Memory Usage
----------------
Device: at90usb1286

Program:    4858 bytes (3.7% Full)
(.text + .data + .bootloader)

Data:       1495 bytes (18.2% Full)
(.data + .bss + .noinit)


 [INFO]    : Finished building project "Joystick".
Waiting for AT90USB1286...

(the bitmasks in parenthesis are permutations of settings)

And the generated steps.c file, for those curious:


  1. For standard patterns, which is all I’ve done so far 

  2. Lousy” here meaning “poor” (re: performance) rather than “overrun with”, although an argument could be made for either. 

Comments