|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Copyright (c) 2020 The Pybricks Authors |
| 3 | + |
| 4 | +#include <stdint.h> |
| 5 | +#include <stdio.h> |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +#include <contiki.h> |
| 9 | +#include <tinytest.h> |
| 10 | +#include <tinytest_macros.h> |
| 11 | + |
| 12 | +#include <pbio/error.h> |
| 13 | +#include <pbio/light_grid.h> |
| 14 | + |
| 15 | +#include "../src/light/light_grid.h" |
| 16 | + |
| 17 | +#define GRID_SIZE 3 |
| 18 | +#define INTERVAL 10 |
| 19 | + |
| 20 | +#define DATA_SIZE (GRID_SIZE * GRID_SIZE) |
| 21 | +#define ROW_DATA(...) (const uint8_t[GRID_SIZE]) {__VA_ARGS__} |
| 22 | +#define IMAGE_DATA(...) (const uint8_t[DATA_SIZE]) {__VA_ARGS__} |
| 23 | + |
| 24 | +#define tt_want_light_grid_data(...) \ |
| 25 | + tt_want_int_op(memcmp(test_light_grid_set_pixel_last_brightness, \ |
| 26 | + (const uint8_t[DATA_SIZE]) {__VA_ARGS__}, DATA_SIZE), ==, 0) |
| 27 | + |
| 28 | +static const uint8_t test_animation[] = { |
| 29 | + 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 30 | + 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| 31 | +}; |
| 32 | + |
| 33 | +static uint8_t test_light_grid_set_pixel_last_brightness[GRID_SIZE][GRID_SIZE]; |
| 34 | + |
| 35 | +static void test_light_grid_reset() { |
| 36 | + memset(test_light_grid_set_pixel_last_brightness, 0, DATA_SIZE); |
| 37 | +} |
| 38 | + |
| 39 | +static pbio_error_t test_light_grid_set_pixel(pbio_light_grid_t *light_grid, uint8_t row, uint8_t col, uint8_t brightness) { |
| 40 | + test_light_grid_set_pixel_last_brightness[row][col] = brightness; |
| 41 | + return PBIO_SUCCESS; |
| 42 | +} |
| 43 | + |
| 44 | +static const pbio_light_grid_funcs_t test_light_grid_funcs = { |
| 45 | + .set_pixel = test_light_grid_set_pixel, |
| 46 | +}; |
| 47 | + |
| 48 | +PT_THREAD(test_light_grid(struct pt *pt)) { |
| 49 | + PT_BEGIN(pt); |
| 50 | + |
| 51 | + static pbio_light_grid_t test_light_grid; |
| 52 | + pbio_light_grid_init(&test_light_grid, 3, &test_light_grid_funcs); |
| 53 | + |
| 54 | + // ensure get size works |
| 55 | + tt_want_uint_op(pbio_light_grid_get_size(&test_light_grid), ==, GRID_SIZE); |
| 56 | + |
| 57 | + // set pixel should only set one pixel |
| 58 | + test_light_grid_reset(); |
| 59 | + tt_want_uint_op(pbio_light_grid_set_pixel(&test_light_grid, 0, 0, 100), ==, PBIO_SUCCESS); |
| 60 | + tt_want_light_grid_data(100); |
| 61 | + |
| 62 | + tt_want_uint_op(pbio_light_grid_set_pixel(&test_light_grid, |
| 63 | + GRID_SIZE - 1, GRID_SIZE - 1, 100), ==, PBIO_SUCCESS); |
| 64 | + tt_want_light_grid_data(100, 0, 0, 0, 0, 0, 0, 0, 100); |
| 65 | + |
| 66 | + // out of bounds checking |
| 67 | + tt_want_uint_op(pbio_light_grid_set_pixel(&test_light_grid, GRID_SIZE, 0, 100), ==, PBIO_ERROR_INVALID_ARG); |
| 68 | + tt_want_light_grid_data(100, 0, 0, 0, 0, 0, 0, 0, 100); |
| 69 | + tt_want_uint_op(pbio_light_grid_set_pixel(&test_light_grid, 0, GRID_SIZE, 100), ==, PBIO_ERROR_INVALID_ARG); |
| 70 | + tt_want_light_grid_data(100, 0, 0, 0, 0, 0, 0, 0, 100); |
| 71 | + |
| 72 | + // bitwise mapping |
| 73 | + test_light_grid_reset(); |
| 74 | + tt_want_uint_op(pbio_light_grid_set_rows(&test_light_grid, ROW_DATA(0b100, 0b010, 0b001)), ==, PBIO_SUCCESS); |
| 75 | + tt_want_light_grid_data(100, 0, 0, 0, 100, 0, 0, 0, 100); |
| 76 | + |
| 77 | + // bytewise mapping |
| 78 | + test_light_grid_reset(); |
| 79 | + tt_want_uint_op(pbio_light_grid_set_image(&test_light_grid, IMAGE_DATA(1, 2, 3, 4, 5, 6, 7, 8, 9)), ==, PBIO_SUCCESS); |
| 80 | + tt_want_light_grid_data(1, 2, 3, 4, 5, 6, 7, 8, 9); |
| 81 | + |
| 82 | + // starting animation should call set_pixel() synchonously with the first cell data |
| 83 | + test_light_grid_reset(); |
| 84 | + pbio_light_grid_start_animation(&test_light_grid, test_animation, 2, INTERVAL); |
| 85 | + tt_want_light_grid_data(1, 2, 3, 4, 5, 6, 7, 8, 9); |
| 86 | + |
| 87 | + // set_pixel() should not be called again until after a delay and it should |
| 88 | + // receive the next hue in the list |
| 89 | + static struct timer timer; |
| 90 | + timer_set(&timer, INTERVAL); |
| 91 | + PT_WAIT_UNTIL(pt, test_light_grid_set_pixel_last_brightness[0][0] != 1); |
| 92 | + tt_want_light_grid_data(11, 12, 13, 14, 15, 16, 17, 18, 19); |
| 93 | + tt_want(timer_expired(&timer)); |
| 94 | + |
| 95 | + // then the next animation update should wrap back to the start of the list |
| 96 | + timer_set(&timer, INTERVAL); |
| 97 | + PT_WAIT_UNTIL(pt, test_light_grid_set_pixel_last_brightness[0][0] != 11); |
| 98 | + tt_want_light_grid_data(1, 2, 3, 4, 5, 6, 7, 8, 9); |
| 99 | + tt_want(timer_expired(&timer)); |
| 100 | + |
| 101 | + // stopping the animation should not change any pixels |
| 102 | + test_light_grid_reset(); |
| 103 | + pbio_light_grid_stop_animation(&test_light_grid); |
| 104 | + timer_set(&timer, INTERVAL * 2); |
| 105 | + PT_WAIT_UNTIL(pt, timer_expired(&timer)); |
| 106 | + tt_want_light_grid_data(0); |
| 107 | + |
| 108 | + PT_END(pt); |
| 109 | +} |
0 commit comments