Skip to content

Commit

Permalink
test: add surface functions (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
b4nst authored Aug 19, 2023
1 parent e1da4f3 commit 8d77a50
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 140 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ jobs:
with:
submodules: true
- uses: carlosperate/arm-none-eabi-gcc-action@v1
- name: dependencies
run: sudo apt-get install -y libcmocka-dev
- name: build
run: make
- name: test
Expand Down
10 changes: 8 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ WORKDIR ${path}
#ADD . ${path}

# Distro management
RUN apt-get update && apt-get install -y build-essential gcc-arm-none-eabi && \
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN apt-get update && \
apt-get install -y \
build-essential \
libcmocka-dev \
clang \
gcc-arm-none-eabi && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Run shell
CMD ["/bin/bash"]
23 changes: 15 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ TOOLS = tools

SOURCES := $(wildcard src/*.c)
HEADERS := $(wildcard include/*.h)
TESTS := $(wildcard tests/*.c)
MOCKS := $(wildcard tests/mocks/*.c)

INCLUDES += -Iinclude -I

LIB = lib/launchpad_pro.a

OBJECTS = $(addprefix $(BUILDDIR)/, $(addsuffix .o, $(basename $(SOURCES))))
TEST_RUNS = $(addprefix $(BUILDDIR)/, $(addsuffix .run, $(basename $(TESTS))))

# output files
SYX = $(BUILDDIR)/polyboard.syx
ELF = $(BUILDDIR)/polyboard.elf
HEX = $(BUILDDIR)/polyboard.hex
HEXTOSYX = $(BUILDDIR)/hextosyx
SIMULATOR = $(BUILDDIR)/simulator

# tools
HOST_GPP = g++
Expand All @@ -36,21 +38,16 @@ LDSCRIPT = stm32_flash.ld

LDFLAGS += -T$(LDSCRIPT) -u _start -u _Minimum_Stack_Size -mcpu=cortex-m3 -mthumb -specs=nano.specs -specs=nosys.specs -nostdlib -Wl,-static -N -nostartfiles -Wl,--gc-sections

all: $(SYX)
all: test $(SYX)

# build the final sysex file from the ELF - run the simulator first
$(SYX): $(HEX) $(HEXTOSYX) $(SIMULATOR)
./$(SIMULATOR)
$(SYX): $(HEX) $(HEXTOSYX)
./$(HEXTOSYX) $(HEX) $(SYX)

# build the tool for conversion of ELF files to sysex, ready for upload to the unit
$(HEXTOSYX):
$(HOST_GPP) -Ofast -std=c++0x -I./$(TOOLS)/libintelhex/include ./$(TOOLS)/libintelhex/src/intelhex.cc $(TOOLS)/hextosyx.cpp -o $(HEXTOSYX)

# build the simulator (it's a very basic test of the code before it runs on the device!)
$(SIMULATOR):
$(HOST_GCC) -g3 -O0 -std=c99 -Iinclude $(TOOLS)/simulator.c $(SOURCES) -o $(SIMULATOR)

$(HEX): $(ELF)
$(OBJCOPY) -O ihex $< $@

Expand All @@ -65,6 +62,16 @@ $(BUILDDIR)/%.o: %.c
mkdir -p $(dir $@)
$(CC) -c $(CFLAGS) -MMD -o $@ $<

$(BUILDDIR)/tests/%.run: tests/%.c
mkdir -p $(dir $@)
$(HOST_GCC) -g3 -O0 -std=c99 -Iinclude $^ $(SOURCES) $(MOCKS) -o $@ -lcmocka

test: $(TEST_RUNS)
@for test in $(TEST_RUNS); do \
echo "Running $$test"; \
$$test; \
done

fmt: $(SOURCES) $(HEADERS)
clang-format --style=$(CODE_STYLE) -i $^
.PHONY: fmt
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ I won't describe how to use these tools, I'm sure you already know - and if you
1. Wait for the update to complete, and for the device to reboot!

> Tip - set the delay between sysex messages to as low a value as possible, so you're not waiting about for ages while the firmware uploads!
## Contributing

You will need a container runtime (containerd, podman, docker, etc...) or Vagrant.


13 changes: 13 additions & 0 deletions include/surface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef SURFACE_H
#define SURFACE_H

enum SurfaceKeyType { SURFACE_PAD, SURFACE_CTRL };

/**
* @brief index_to_surface_key_type Convert an index to a SurfaceKeyType
* @param index The index to convert
* @return The SurfaceKeyType
*/
enum SurfaceKeyType index_to_surface_key_type(unsigned char index);

#endif // SURFACE_H
18 changes: 18 additions & 0 deletions src/surface.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "surface.h"

enum SurfaceKeyType index_to_surface_key_type(unsigned char index) {
if (index >= 90) {
return SURFACE_CTRL;
}
if (index <= 10) {
return SURFACE_CTRL;
}
if (index % 10 == 9) {
return SURFACE_CTRL;
}
if (index % 10 == 1) {
return SURFACE_CTRL;
}

return SURFACE_PAD;
}
47 changes: 47 additions & 0 deletions tests/mocks/hal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "app.h"

void hal_plot_led(u8 type, u8 index, u8 red, u8 green, u8 blue)
{
}

void hal_read_led(u8 type, u8 index, u8 *red, u8 *green, u8 *blue)
{
}

void hal_send_midi(u8 port, u8 status, u8 d1, u8 d2)
{
}

void hal_send_sysex(u8 port, const u8* data, u16 length)
{
}

void hal_read_flash(u32 offset, u8 *data, u32 length)
{
}

void hal_write_flash(u32 offset,const u8 *data, u32 length)
{
}

static u16 raw_ADC[64];

void sim_app_init()
{
app_init(raw_ADC);
}

void sim_app_surface_event(u8 type, u8 index, u8 value)
{
app_surface_event(type, index, value);
}

void sim_app_midi_event(u8 port, u8 status, u8 d1, u8 d2)
{
app_midi_event(port, status, d1, d2);
}

void sim_app_timer_event()
{
app_timer_event();
}
28 changes: 28 additions & 0 deletions tests/surface_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <cmocka.h>
#include "surface.h"

/* A test case that does nothing and succeeds. */
static void test_index_to_surface_key_type(void **state) {
(void) state; /* unused */

assert_int_equal(index_to_surface_key_type(95), SURFACE_CTRL);
assert_int_equal(index_to_surface_key_type(5), SURFACE_CTRL);
assert_int_equal(index_to_surface_key_type(29), SURFACE_CTRL);
assert_int_equal(index_to_surface_key_type(21), SURFACE_CTRL);
assert_int_equal(index_to_surface_key_type(15), SURFACE_PAD);
assert_int_equal(index_to_surface_key_type(36), SURFACE_PAD);
assert_int_equal(index_to_surface_key_type(48), SURFACE_PAD);
}

int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_index_to_surface_key_type),
};

return cmocka_run_group_tests(tests, NULL, NULL);
}

130 changes: 0 additions & 130 deletions tools/simulator.c

This file was deleted.

0 comments on commit 8d77a50

Please sign in to comment.