added unit tests for calc.h
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define loop(varname, count) for(uint8_t varname = 0; varname < (count); varname++)
|
||||||
|
#define repeat(count) for(uint8_t _repeat_i = 0; _repeat_i < (count); _repeat_i++)
|
||||||
|
#define until(cond) while(!(cond))
|
||||||
+5
-3
@@ -1,6 +1,8 @@
|
|||||||
MightyPorsk's AVR utils library
|
AVR utils library
|
||||||
===============================
|
=================
|
||||||
|
|
||||||
This is my ever-evolving library. When I'm done with a project, I copy the current library to the project, so it doesn't break when I do further improvements.
|
This is my ever-evolving library (not only) for AVR programming.
|
||||||
|
|
||||||
|
When I'm done with a project, I copy the current library to the project, so it doesn't break when I do further improvements.
|
||||||
|
|
||||||
Each library file contains a large comment block explaining it's function.
|
Each library file contains a large comment block explaining it's function.
|
||||||
|
|||||||
+59
-16
@@ -1,46 +1,89 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
/**
|
/**
|
||||||
General purpose calculation and bit manipulation utilities.
|
Bit and byte manipulation utilities
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// if max, go to zero. Else increment.
|
|
||||||
#define inc_wrap(var, min, max) do { if ((var) >= (max)) { (var)=min; } else { (var)++; } } while(0)
|
|
||||||
|
|
||||||
// If zero, go to max. Else decrement,
|
// --- Increment in range ---
|
||||||
#define dec_wrap(var, min, max) do { if ((var) > min) { (var)--; } else { (var)=(max); } } while(0)
|
// when overflown, wraps within range. Lower bound < upper bound.
|
||||||
|
// ..., upper bound excluded
|
||||||
|
#define inc_wrap(var, min, max) do { if ((var) >= (max - 1)) { (var) = (min); } else { (var)++; } } while(0)
|
||||||
|
// ..., upper bound included
|
||||||
|
#define inc_wrapi(var, min, max) inc_wrap((var), (min), (max) + 1)
|
||||||
|
|
||||||
// === general bit manipulation with register ===
|
|
||||||
|
// --- Decrement in range ---
|
||||||
|
// when underflown, wraps within range. Lower bound < upper bound.
|
||||||
|
// ..., upper bound excluded
|
||||||
|
#define dec_wrap(var, min, max) do { if ((var) <= (min)) { (var) = (max) - 1; } else { (var)--; } } while(0)
|
||||||
|
// ..., upper bound included
|
||||||
|
#define dec_wrapi(var, min, max) dec_wrap((var), (min), (max) + 1)
|
||||||
|
|
||||||
|
|
||||||
|
// --- Bit manipulation --
|
||||||
|
|
||||||
|
// Set bit
|
||||||
#define sbi(reg, bit) do { (reg) |= (1 << (uint8_t)(bit)); } while(0)
|
#define sbi(reg, bit) do { (reg) |= (1 << (uint8_t)(bit)); } while(0)
|
||||||
|
|
||||||
|
// Clear bit
|
||||||
#define cbi(reg, bit) do { (reg) &= ~(1 << (uint8_t)(bit)); } while(0)
|
#define cbi(reg, bit) do { (reg) &= ~(1 << (uint8_t)(bit)); } while(0)
|
||||||
|
|
||||||
#define read_bit(reg, bit) ((((uint8_t)(reg)) >> (uint8_t)(bit)) & 0x1)
|
// Get n-th bit
|
||||||
|
#define read_bit(reg, bit) (((reg) >> (uint8_t)(bit)) & 0x1)
|
||||||
#define get_bit(reg, bit) read_bit(reg, bit)
|
#define get_bit(reg, bit) read_bit(reg, bit)
|
||||||
#define bit_is_high(reg, bit) read_bit(reg, bit)
|
|
||||||
#define bit_is_low(reg, bit) !read_bit(reg, bit)
|
|
||||||
// Can't use bit_is_set, as it's redefined in sfr_def.h
|
|
||||||
|
|
||||||
|
// Test n-th bit (Can't use bit_is_set, as it's redefined in sfr_def.h)
|
||||||
|
#define bit_is_high(reg, bit) read_bit(reg, bit)
|
||||||
|
#define bit_is_low(reg, bit) (!read_bit(reg, bit))
|
||||||
|
|
||||||
|
// Write value to n-th bit
|
||||||
#define write_bit(reg, bit, value) do { (reg) = ((reg) & ~(1 << (uint8_t)(bit))) | (((uint8_t)(value) & 0x1) << (uint8_t)(bit)); } while(0)
|
#define write_bit(reg, bit, value) do { (reg) = ((reg) & ~(1 << (uint8_t)(bit))) | (((uint8_t)(value) & 0x1) << (uint8_t)(bit)); } while(0)
|
||||||
#define set_bit(reg, bit, value) write_bit(reg, bit, value)
|
#define set_bit(reg, bit, value) write_bit(reg, bit, value)
|
||||||
|
|
||||||
|
// Invert n-th bit
|
||||||
#define toggle_bit(reg, bit) do { (reg) ^= (1 << (uint8_t)(bit)); } while(0)
|
#define toggle_bit(reg, bit) do { (reg) ^= (1 << (uint8_t)(bit)); } while(0)
|
||||||
|
|
||||||
// general pin manipulation - with pointer to register
|
|
||||||
|
// --- Bit manipulation with pointer to variable ---
|
||||||
|
|
||||||
|
// Set n-th bit in pointee
|
||||||
#define sbi_p(reg_p, bit) do { (*(reg_p)) |= (1 << (uint8_t)(bit)); } while(0)
|
#define sbi_p(reg_p, bit) do { (*(reg_p)) |= (1 << (uint8_t)(bit)); } while(0)
|
||||||
|
// Clear n-th bit in pointee
|
||||||
#define cbi_p(reg_p, bit) do { (*(reg_p)) &= ~(1 << (uint8_t)(bit)); } while(0)
|
#define cbi_p(reg_p, bit) do { (*(reg_p)) &= ~(1 << (uint8_t)(bit)); } while(0)
|
||||||
|
|
||||||
|
// Get n-th bit in pointee
|
||||||
#define read_bit_p(reg_p, bit) ((*(reg_p) >> (uint8_t)(bit)) & 0x1)
|
#define read_bit_p(reg_p, bit) ((*(reg_p) >> (uint8_t)(bit)) & 0x1)
|
||||||
#define get_bit_p(reg_p, bit) read_bit_p(reg_p, bit)
|
#define get_bit_p(reg_p, bit) read_bit_p(reg_p, bit)
|
||||||
|
|
||||||
|
// Test n-th bit in pointee (Can't use bit_is_set, as it's redefined in sfr_def.h)
|
||||||
|
#define bit_is_high_p(reg_p, bit) read_bit_p(reg_p, bit)
|
||||||
|
#define bit_is_low_p(reg_p, bit) (!read_bit_p(reg_p, bit))
|
||||||
|
|
||||||
|
// Write value to a bit in pointee
|
||||||
#define write_bit_p(reg_p, bit, value) do { *(reg_p) = (*(reg_p) & ~(1 << ((uint8_t)(bit) & 0x1))) | (((uint8_t)(value) & 0x1) << (uint8_t)(bit)); } while(0)
|
#define write_bit_p(reg_p, bit, value) do { *(reg_p) = (*(reg_p) & ~(1 << ((uint8_t)(bit) & 0x1))) | (((uint8_t)(value) & 0x1) << (uint8_t)(bit)); } while(0)
|
||||||
#define set_bit_p(reg, bit, value) write_bit_p(reg_p, bit, value)
|
#define set_bit_p(reg_p, bit, value) write_bit_p(reg_p, bit, value)
|
||||||
#define toggle_bit_p(reg_p, bit) do { *(reg_p) ^= (1 << (uint8_t)(bit)); } while(0)
|
#define toggle_bit_p(reg_p, bit) do { *(reg_p) ^= (1 << (uint8_t)(bit)); } while(0)
|
||||||
|
|
||||||
|
|
||||||
|
// --- Nibble manipulation ---
|
||||||
|
|
||||||
|
// Replace nibble in a byte
|
||||||
#define write_low_nibble(reg, value) do { (reg) = ((reg) & 0xF0) | ((uint8_t)(value) & 0xF); } while(0)
|
#define write_low_nibble(reg, value) do { (reg) = ((reg) & 0xF0) | ((uint8_t)(value) & 0xF); } while(0)
|
||||||
#define write_high_nibble(reg, value) do { (reg) = ((reg) & 0x0F) | (((uint8_t)(value) & 0xF) << 4); } while(0)
|
#define write_high_nibble(reg, value) do { (reg) = ((reg) & 0x0F) | (((uint8_t)(value) & 0xF) << 4); } while(0)
|
||||||
|
|
||||||
|
#define write_low_nibble_p(reg_p, value) do { *(reg_p) = (*(reg_p) & 0xF0) | ((uint8_t)(value) & 0xF); } while(0)
|
||||||
|
#define write_high_nibble_p(reg_p, value) do { *(reg_p) = (*(reg_p) & 0x0F) | (((uint8_t)(value) & 0xF) << 4); } while(0)
|
||||||
|
|
||||||
// Check if value is in range A..B or B..A
|
|
||||||
#define in_range(x, low, high) (((low) < (high)) && ((x) > (low) && (x) < (high))) || (((low) > (high)) && ((x) < (low) || (x) > (high)))
|
|
||||||
|
|
||||||
// Check if value is in range A..B. If B < A, matches all outside B..A
|
// --- Range tests ---
|
||||||
#define in_range_wrap(x, low, high) (((low) < (high)) && ((x) > (low) && (x) < (high))) || (((low) > (high)) && ((x) > (low) || (x) < (high)))
|
|
||||||
|
// Test if X is within low..high, regardless of bounds order
|
||||||
|
#define in_range(x, low, high) ((((low) < (high)) && ((x) >= (low) && (x) < (high))) || (((low) > (high)) && ((x) >= (high) && (x) < (low))))
|
||||||
|
// ..., include greater bound
|
||||||
|
#define in_rangei(x, low, high) ((((low) <= (high)) && ((x) >= (low) && (x) <= (high))) || (((low) > (high)) && ((x) >= (high) && (x) <= (low))))
|
||||||
|
|
||||||
|
// Test if X in low..high, wrap around ends if needed.
|
||||||
|
#define in_range_wrap(x, low, high) ((((low) < (high)) && ((x) >= (low) && (x) < (high))) || (((low) > (high)) && ((x) >= (low) || (x) < (high))))
|
||||||
|
// ..., include upper bound
|
||||||
|
#define in_range_wrapi(x, low, high) ((((low) <= (high)) && ((x) >= (low) && (x) <= (high))) || (((low) > (high)) && ((x) >= (low) || (x) <= (high))))
|
||||||
|
|||||||
@@ -49,9 +49,9 @@
|
|||||||
|
|
||||||
/* Internal deboucer entry */
|
/* Internal deboucer entry */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
PORT_P reg;
|
PORT_P reg; // pointer to IO register
|
||||||
uint8_t bit;
|
uint8_t bit; // bits 6 and 7 of this hold "state" & "invert" flag
|
||||||
uint8_t count;
|
uint8_t count; // number of ticks this was in the new state
|
||||||
} debo_slot_t;
|
} debo_slot_t;
|
||||||
|
|
||||||
/** Debounce data array */
|
/** Debounce data array */
|
||||||
@@ -101,4 +101,3 @@ void debo_tick()
|
|||||||
|
|
||||||
/** Get a value of debounced pin */
|
/** Get a value of debounced pin */
|
||||||
#define debo_get_pin(i) (get_bit(debo_slots[i].bit, 6) ^ get_bit(debo_slots[i].bit, 7))
|
#define debo_get_pin(i) (get_bit(debo_slots[i].bit, 6) ^ get_bit(debo_slots[i].bit, 7))
|
||||||
//(get_bit(debo_slots[i].bit, 6) ^ get_bit(debo_slots[i].bit, 7))
|
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
/**
|
||||||
|
Custom loops
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Repeat code n times (uint8_t counter)
|
||||||
|
#define repeat(count) repeat_aux(count, _repeat_##__COUNTER__)
|
||||||
|
#define repeat_aux(count, cntvar) for (uint8_t cntvar = 0; cntvar < (count); cntvar++)
|
||||||
|
|
||||||
|
// Repeat code n times (uint16_t counter)
|
||||||
|
#define repeatx(count) repeatx_aux(count, _repeatx_##__COUNTER__)
|
||||||
|
#define repeatx_aux(count, cntvar) for (uint16_t cntvar = 0; cntvar < (count); cntvar++)
|
||||||
|
|
||||||
|
// Repeat with custom counter name (uint8_t)
|
||||||
|
#define loop(var, count) repeat_aux(count, var)
|
||||||
|
// ..., uint16_t
|
||||||
|
#define loopx(var, count) repeatx_aux(count, var)
|
||||||
|
|
||||||
|
// Do until condition is met
|
||||||
|
#define until(what) while(!(what))
|
||||||
|
|
||||||
|
// Because why the hell not
|
||||||
|
#define whilst(what) while((what))
|
||||||
|
|
||||||
Symlink
+1
@@ -0,0 +1 @@
|
|||||||
|
/home/ondra/devel/avr/avr-projects/devel/lib
|
||||||
Executable
+8
@@ -0,0 +1,8 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
dir=/tmp/avrclib_unittest
|
||||||
|
|
||||||
|
[ -d "$dir" ] || mkdir "$dir"
|
||||||
|
|
||||||
|
gcc test_calc.c -o "$dir/test_calc" && "$dir/test_calc"
|
||||||
|
|
||||||
@@ -0,0 +1,356 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include "lib/calc.h"
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
printf("== calc.h ==\n");
|
||||||
|
|
||||||
|
int i;
|
||||||
|
int a;
|
||||||
|
|
||||||
|
printf("fn: inc_wrap()\n");
|
||||||
|
{
|
||||||
|
// normal operation
|
||||||
|
a = 1;
|
||||||
|
inc_wrap(a, 0, 15);
|
||||||
|
assert(a == 2);
|
||||||
|
|
||||||
|
// overflow
|
||||||
|
a = 14;
|
||||||
|
inc_wrap(a, 0, 15);
|
||||||
|
assert(a == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: inc_wrapi()\n");
|
||||||
|
{
|
||||||
|
// normal operation
|
||||||
|
a = 1;
|
||||||
|
inc_wrapi(a, 0, 15);
|
||||||
|
assert(a == 2);
|
||||||
|
|
||||||
|
// not overflow yet
|
||||||
|
a = 14;
|
||||||
|
inc_wrapi(a, 0, 15);
|
||||||
|
assert(a == 15);
|
||||||
|
|
||||||
|
// overflow
|
||||||
|
a = 15;
|
||||||
|
inc_wrap(a, 0, 15);
|
||||||
|
assert(a == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: dec_wrap()\n");
|
||||||
|
{
|
||||||
|
// normal operation
|
||||||
|
a = 5;
|
||||||
|
dec_wrap(a, 0, 15);
|
||||||
|
assert(a == 4);
|
||||||
|
|
||||||
|
// underflow
|
||||||
|
a = 0;
|
||||||
|
dec_wrap(a, 0, 15);
|
||||||
|
assert(a == 14);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: dec_wrapi()\n");
|
||||||
|
{
|
||||||
|
// normal operation
|
||||||
|
a = 5;
|
||||||
|
dec_wrapi(a, 0, 15);
|
||||||
|
assert(a == 4);
|
||||||
|
|
||||||
|
// underflow
|
||||||
|
a = 0;
|
||||||
|
dec_wrapi(a, 0, 15);
|
||||||
|
assert(a == 15);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: sbi()\n");
|
||||||
|
{
|
||||||
|
a = 0;
|
||||||
|
sbi(a, 2);
|
||||||
|
assert(a == 0b100);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: cbi()\n");
|
||||||
|
{
|
||||||
|
a = 0b11111;
|
||||||
|
cbi(a, 2);
|
||||||
|
assert(a == 0b11011);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: read_bit()\n");
|
||||||
|
{
|
||||||
|
a = 0b101;
|
||||||
|
assert(read_bit(a, 0) == 1);
|
||||||
|
assert(read_bit(a, 1) == 0);
|
||||||
|
assert(read_bit(a, 2) == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: bit_is_high()\n");
|
||||||
|
{
|
||||||
|
a = 0b101;
|
||||||
|
assert(bit_is_high(a, 0) == 1);
|
||||||
|
assert(bit_is_high(a, 1) == 0);
|
||||||
|
assert(bit_is_high(a, 2) == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: bit_is_low()\n");
|
||||||
|
{
|
||||||
|
a = 0b101;
|
||||||
|
assert(bit_is_low(a, 0) == 0);
|
||||||
|
assert(bit_is_low(a, 1) == 1);
|
||||||
|
assert(bit_is_low(a, 2) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: toggle_bit()\n");
|
||||||
|
{
|
||||||
|
a = 0;
|
||||||
|
toggle_bit(a, 2);
|
||||||
|
assert(a == 0b00100);
|
||||||
|
toggle_bit(a, 4);
|
||||||
|
assert(a == 0b10100);
|
||||||
|
toggle_bit(a, 4);
|
||||||
|
assert(a == 0b00100);
|
||||||
|
}
|
||||||
|
|
||||||
|
// pointer variants
|
||||||
|
|
||||||
|
int* b = &a;
|
||||||
|
|
||||||
|
printf("fn: sbi_p()\n");
|
||||||
|
{
|
||||||
|
*b = 0;
|
||||||
|
sbi_p(b, 2);
|
||||||
|
assert(*b == 0b100);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: cbi_p()\n");
|
||||||
|
{
|
||||||
|
*b = 0b11111;
|
||||||
|
cbi_p(b, 2);
|
||||||
|
assert(*b == 0b11011);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: read_bit_p()\n");
|
||||||
|
{
|
||||||
|
*b = 0b101;
|
||||||
|
assert(read_bit_p(b, 0) == 1);
|
||||||
|
assert(read_bit_p(b, 1) == 0);
|
||||||
|
assert(read_bit_p(b, 2) == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: bit_is_high_p()\n");
|
||||||
|
{
|
||||||
|
*b = 0b101;
|
||||||
|
assert(bit_is_high_p(b, 0) == 1);
|
||||||
|
assert(bit_is_high_p(b, 1) == 0);
|
||||||
|
assert(bit_is_high_p(b, 2) == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: bit_is_low_p()\n");
|
||||||
|
{
|
||||||
|
*b = 0b101;
|
||||||
|
assert(bit_is_low_p(b, 0) == 0);
|
||||||
|
assert(bit_is_low_p(b, 1) == 1);
|
||||||
|
assert(bit_is_low_p(b, 2) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: toggle_bit_p()\n");
|
||||||
|
{
|
||||||
|
*b = 0;
|
||||||
|
toggle_bit_p(b, 2);
|
||||||
|
assert(*b == 0b00100);
|
||||||
|
toggle_bit_p(b, 4);
|
||||||
|
assert(*b == 0b10100);
|
||||||
|
toggle_bit_p(b, 4);
|
||||||
|
assert(*b == 0b00100);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -- nibbles --
|
||||||
|
|
||||||
|
printf("fn: write_low_nibble()\n");
|
||||||
|
{
|
||||||
|
a = 0xAC;
|
||||||
|
write_low_nibble(a, 0x5); // shifted 4 left
|
||||||
|
assert(a == 0xA5);
|
||||||
|
|
||||||
|
a = 0xAB;
|
||||||
|
write_low_nibble(a, 0x65); // shifted 4 left, extra ignored
|
||||||
|
assert(a == 0xA5);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: write_high_nibble()\n");
|
||||||
|
{
|
||||||
|
a = 0xAC;
|
||||||
|
write_high_nibble(a, 0x5); // shifted 4 left
|
||||||
|
assert(a == 0x5C);
|
||||||
|
|
||||||
|
a = 0xAB;
|
||||||
|
write_high_nibble(a, 0x65); // shifted 4 left, extra ignored
|
||||||
|
assert(a == 0x5B);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: write_low_nibble_p()\n");
|
||||||
|
{
|
||||||
|
*b = 0xAC;
|
||||||
|
write_low_nibble_p(b, 0x5); // shifted 4 left
|
||||||
|
assert(*b == 0xA5);
|
||||||
|
|
||||||
|
*b = 0xAB;
|
||||||
|
write_low_nibble_p(b, 0x65); // shifted 4 left, extra ignored
|
||||||
|
assert(*b == 0xA5);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: write_high_nibble_p()\n");
|
||||||
|
{
|
||||||
|
*b = 0xAC;
|
||||||
|
write_high_nibble_p(b, 0x5); // shifted 4 left
|
||||||
|
assert(*b == 0x5C);
|
||||||
|
|
||||||
|
*b = 0xAB;
|
||||||
|
write_high_nibble_p(b, 0x65); // shifted 4 left, extra ignored
|
||||||
|
assert(*b == 0x5B);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: in_range()\n");
|
||||||
|
{
|
||||||
|
// regular
|
||||||
|
assert(in_range(10, 10, 15));
|
||||||
|
assert(in_range(14, 10, 15));
|
||||||
|
assert(in_range(13, 10, 15));
|
||||||
|
// under
|
||||||
|
assert(!in_range(9, 10, 15));
|
||||||
|
assert(!in_range(0, 10, 15));
|
||||||
|
// above
|
||||||
|
assert(!in_range(15, 10, 15));
|
||||||
|
assert(!in_range(99, 10, 15));
|
||||||
|
|
||||||
|
// swapped bounds
|
||||||
|
|
||||||
|
// regular
|
||||||
|
assert(in_range(10, 15, 10));
|
||||||
|
assert(in_range(14, 15, 10));
|
||||||
|
assert(in_range(13, 15, 10));
|
||||||
|
// under
|
||||||
|
assert(!in_range(9, 15, 10));
|
||||||
|
assert(!in_range(0, 15, 10));
|
||||||
|
// above
|
||||||
|
assert(!in_range(15, 15, 10));
|
||||||
|
assert(!in_range(99, 15, 10));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: in_rangei()\n");
|
||||||
|
{
|
||||||
|
// regular
|
||||||
|
assert(in_rangei(10, 10, 15));
|
||||||
|
assert(in_rangei(15, 10, 15));
|
||||||
|
assert(in_rangei(13, 10, 15));
|
||||||
|
// under
|
||||||
|
assert(!in_rangei(9, 10, 15));
|
||||||
|
assert(!in_rangei(0, 10, 15));
|
||||||
|
// above
|
||||||
|
assert(!in_rangei(16, 10, 15));
|
||||||
|
assert(!in_rangei(99, 10, 15));
|
||||||
|
|
||||||
|
// -- swapped bounds --
|
||||||
|
|
||||||
|
// regular
|
||||||
|
assert(in_rangei(10, 15, 10));
|
||||||
|
assert(in_rangei(15, 15, 10));
|
||||||
|
assert(in_rangei(13, 15, 10));
|
||||||
|
// under
|
||||||
|
assert(!in_rangei(9, 15, 10));
|
||||||
|
assert(!in_rangei(0, 15, 10));
|
||||||
|
// above
|
||||||
|
assert(!in_rangei(16, 15, 10));
|
||||||
|
assert(!in_rangei(99, 15, 10));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: in_range_wrap()\n");
|
||||||
|
{
|
||||||
|
// as regular range
|
||||||
|
|
||||||
|
// regular
|
||||||
|
assert(in_range_wrap(10, 10, 15));
|
||||||
|
assert(in_range_wrap(14, 10, 15));
|
||||||
|
assert(in_range_wrap(13, 10, 15));
|
||||||
|
// under
|
||||||
|
assert(!in_range_wrap(9, 10, 15));
|
||||||
|
assert(!in_range_wrap(0, 10, 15));
|
||||||
|
// above
|
||||||
|
assert(!in_range_wrap(15, 10, 15));
|
||||||
|
assert(!in_range_wrap(16, 10, 15));
|
||||||
|
assert(!in_range_wrap(99, 10, 15));
|
||||||
|
|
||||||
|
// with wrap (>= 15, < 10)
|
||||||
|
|
||||||
|
// regular
|
||||||
|
assert(!in_range_wrap(10, 15, 10));
|
||||||
|
assert(!in_range_wrap(14, 15, 10));
|
||||||
|
assert(!in_range_wrap(13, 15, 10));
|
||||||
|
// under
|
||||||
|
assert(in_range_wrap(9, 15, 10));
|
||||||
|
assert(in_range_wrap(0, 15, 10));
|
||||||
|
// above
|
||||||
|
assert(in_range_wrap(16, 15, 10));
|
||||||
|
assert(in_range_wrap(99, 15, 10));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("fn: in_range_wrapi()\n");
|
||||||
|
{
|
||||||
|
// as regular range
|
||||||
|
|
||||||
|
// regular
|
||||||
|
assert(in_range_wrapi(10, 10, 15));
|
||||||
|
assert(in_range_wrapi(15, 10, 15));
|
||||||
|
assert(in_range_wrapi(13, 10, 15));
|
||||||
|
// under
|
||||||
|
assert(!in_range_wrapi(9, 10, 15));
|
||||||
|
assert(!in_range_wrapi(0, 10, 15));
|
||||||
|
// above
|
||||||
|
assert(!in_range_wrapi(16, 10, 15));
|
||||||
|
assert(!in_range_wrapi(99, 10, 15));
|
||||||
|
|
||||||
|
// with wrap (>= 15, < 10)
|
||||||
|
|
||||||
|
// regular
|
||||||
|
assert(in_range_wrapi(10, 15, 10));
|
||||||
|
assert(!in_range_wrapi(14, 15, 10));
|
||||||
|
assert(!in_range_wrapi(13, 15, 10));
|
||||||
|
// under
|
||||||
|
assert(in_range_wrapi(10, 15, 10));
|
||||||
|
assert(in_range_wrapi(0, 15, 10));
|
||||||
|
// above
|
||||||
|
assert(in_range_wrapi(16, 15, 10));
|
||||||
|
assert(in_range_wrapi(99, 15, 10));
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("== PASSED ==\n");
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include "lib/loops.h"
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
repeat(5) printf("Five times!\n");
|
||||||
|
repeatx(500) printf(".");
|
||||||
|
printf("\n");
|
||||||
|
int i = 0;
|
||||||
|
until (i == 100) i++;
|
||||||
|
|
||||||
|
whilst (i > 0) {
|
||||||
|
i--;
|
||||||
|
printf("i = %d\n", i);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
loop(moo, 7) {
|
||||||
|
printf("moo = %d\n", moo);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,11 +4,13 @@
|
|||||||
General purpose calculation and bit manipulation utilities.
|
General purpose calculation and bit manipulation utilities.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// if max, go to zero. Else increment.
|
// if max, go to zero. Else increment. wrape = wrap exclusive
|
||||||
#define inc_wrap(var, min, max) do { if ((var) >= (max)) { (var)=min; } else { (var)++; } } while(0)
|
#define inc_wrap(var, min, max) do { if ((var) >= (max - 1)) { (var) = (min); } else { (var)++; } } while(0)
|
||||||
|
#define inc_wrapi(var, min, max) inc_wrap((var), (min), (max) + 1)
|
||||||
|
|
||||||
// If zero, go to max. Else decrement,
|
// If zero, go to max. Else decrement. wrape = wrap exclusive
|
||||||
#define dec_wrap(var, min, max) do { if ((var) > min) { (var)--; } else { (var)=(max); } } while(0)
|
#define dec_wrap(var, min, max) do { if ((var) > (min)) { (var)--; } else { (var) = (max) - 1; } } while(0)
|
||||||
|
#define dec_wrapi(var, min, max) dec_wrap((var), (min), (max) + 1)
|
||||||
|
|
||||||
// === general bit manipulation with register ===
|
// === general bit manipulation with register ===
|
||||||
#define sbi(reg, bit) do { (reg) |= (1 << (uint8_t)(bit)); } while(0)
|
#define sbi(reg, bit) do { (reg) |= (1 << (uint8_t)(bit)); } while(0)
|
||||||
@@ -40,7 +42,9 @@
|
|||||||
|
|
||||||
|
|
||||||
// Check if value is in range A..B or B..A
|
// Check if value is in range A..B or B..A
|
||||||
#define in_range(x, low, high) (((low) < (high)) && ((x) > (low) && (x) < (high))) || (((low) > (high)) && ((x) < (low) || (x) > (high)))
|
#define in_range(x, low, high) ((((low) < (high)) && ((x) >= (low) && (x) < (high))) || (((low) > (high)) && ((x) < (low) && (x) >= (high))))
|
||||||
|
#define in_rangei(x, low, high) ((((low) <= (high)) && ((x) >= (low) && (x) <= (high))) || (((low) > (high)) && ((x) <= (low) && (x) >= (high))))
|
||||||
|
|
||||||
// Check if value is in range A..B. If B < A, matches all outside B..A
|
// Check if value is in range A..B. If B < A, matches all outside B..A
|
||||||
#define in_range_wrap(x, low, high) (((low) < (high)) && ((x) > (low) && (x) < (high))) || (((low) > (high)) && ((x) > (low) || (x) < (high)))
|
#define in_range_wrap(x, low, high) ((((low) < (high)) && ((x) >= (low) && (x) < (high))) || (((low) > (high)) && ((x) >= (low) || (x) < (high))))
|
||||||
|
#define in_range_wrapi(x, low, high) ((((low) <= (high)) && ((x) >= (low) && (x) <= (high))) || (((low) > (high)) && ((x) >= (low) || (x) <= (high))))
|
||||||
|
|||||||
@@ -17,48 +17,17 @@
|
|||||||
|
|
||||||
#include "lib/debounce.h"
|
#include "lib/debounce.h"
|
||||||
|
|
||||||
|
#define WIDTH 6
|
||||||
// #define BOARD_WIDTH 6
|
#define HEIGHT 5
|
||||||
// #define BOARD_HEIGHT 5
|
|
||||||
#define BOARD_WIDTH 6
|
|
||||||
#define BOARD_HEIGHT 5
|
|
||||||
|
|
||||||
// number of cards
|
// number of cards
|
||||||
#define CARD_COUNT (BOARD_WIDTH * BOARD_HEIGHT)
|
#define CARD_COUNT (WIDTH * HEIGHT)
|
||||||
|
|
||||||
// number of pairs
|
|
||||||
#define PAIR_COUNT (CARD_COUNT / 2)
|
|
||||||
|
|
||||||
// when the "small" pin is DOWN, only this many cards are dealt - on the same board size
|
// when the "small" pin is DOWN, only this many cards are dealt - on the same board size
|
||||||
#define CARD_COUNT_SMALL 12
|
#define CARD_COUNT_SMALL WIDTH * 2
|
||||||
|
|
||||||
// color palette
|
|
||||||
const xrgb_t COLORS[] = {
|
|
||||||
rgb24_xrgbc(0x00FF99), // emerald
|
|
||||||
rgb24_xrgbc(0x0000CC), // full blue
|
|
||||||
rgb24_xrgbc(0xFF00FF), // magenta
|
|
||||||
rgb24_xrgbc(0xFF0000), // red
|
|
||||||
rgb24_xrgbc(0xFF2B00), // orange
|
|
||||||
rgb24_xrgbc(0xFFFF00), // yellow
|
|
||||||
rgb24_xrgbc(0x0BEE00), // green
|
|
||||||
rgb24_xrgbc(0xFF6D00), // tangerine yellow/orange
|
|
||||||
rgb24_xrgbc(0x00CCCC), // cyan
|
|
||||||
rgb24_xrgbc(0x4400FF), // blue-purple
|
|
||||||
rgb24_xrgbc(0x5FBA00), // yellow-green
|
|
||||||
rgb24_xrgbc(0xD70053), // wine
|
|
||||||
rgb24_xrgbc(0xCD2B64), // brick
|
|
||||||
rgb24_xrgbc(0xED1B24), // firetruck red
|
|
||||||
rgb24_xrgbc(0xFF6D55), // salmon?
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// assert valid board size
|
// --- Pin assignments (see pins.h) ---
|
||||||
#if CARD_COUNT % 2 == 1
|
|
||||||
# error "Board size is not even!"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
// Pin assignments (see pins.h)
|
|
||||||
|
|
||||||
// RGB LED strip data line
|
// RGB LED strip data line
|
||||||
#define WS1 D10
|
#define WS1 D10
|
||||||
@@ -71,7 +40,7 @@ const xrgb_t COLORS[] = {
|
|||||||
#define BTN_SELECT D6
|
#define BTN_SELECT D6
|
||||||
#define BTN_RESTART D7
|
#define BTN_RESTART D7
|
||||||
|
|
||||||
// connect this pin to ground to get smaller board size (for kids ^^)
|
// Connect to GROUND to get small board size (for kids ^_^)
|
||||||
#define FLAG_SMALL D12
|
#define FLAG_SMALL D12
|
||||||
|
|
||||||
// Debouncer channels for buttons
|
// Debouncer channels for buttons
|
||||||
@@ -88,10 +57,44 @@ const xrgb_t COLORS[] = {
|
|||||||
// entropy for the random number generator
|
// entropy for the random number generator
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// number of pairs
|
||||||
|
#define PAIR_COUNT (CARD_COUNT / 2)
|
||||||
|
|
||||||
|
// assert valid board size
|
||||||
|
#if CARD_COUNT % 2 == 1
|
||||||
|
# error "CARD_COUNT is not even!"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if CARD_COUNT_SMALL % 2 == 1
|
||||||
|
# error "CARD_COUNT_SMALL is not even!"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// color palette
|
||||||
|
const xrgb_t COLORS[] = {
|
||||||
|
rgb24_xrgbc(0x0000CC), // full blue
|
||||||
|
rgb24_xrgbc(0x0BEE00), // green
|
||||||
|
rgb24_xrgbc(0xFFFF00), // yellow
|
||||||
|
rgb24_xrgbc(0xFF0000), // red
|
||||||
|
rgb24_xrgbc(0x00CCFF), // cyan
|
||||||
|
rgb24_xrgbc(0xFF3300), // orange
|
||||||
|
rgb24_xrgbc(0xFF00FF), // magenta
|
||||||
|
rgb24_xrgbc(0x00FF88), // emerald
|
||||||
|
rgb24_xrgbc(0x5FBA00), // yellow-green
|
||||||
|
rgb24_xrgbc(0xFF7700), // tangerine yellow/orange
|
||||||
|
rgb24_xrgbc(0x4400FF), // blue-purple
|
||||||
|
rgb24_xrgbc(0xD70053), // wine
|
||||||
|
rgb24_xrgbc(0xED1B24), // firetruck red
|
||||||
|
rgb24_xrgbc(0xFF6D55), // salmon?
|
||||||
|
rgb24_xrgbc(0xFF4C4C), // skin pink
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// Prototypes
|
// Prototypes
|
||||||
void render();
|
void render();
|
||||||
void update();
|
void update();
|
||||||
void deal_cards();
|
void deal_cards();
|
||||||
|
void safe_press_arrow_key(uint8_t n);
|
||||||
|
|
||||||
|
|
||||||
/** Program initialization */
|
/** Program initialization */
|
||||||
@@ -194,7 +197,10 @@ ISR(TIMER0_COMPA_vect)
|
|||||||
|
|
||||||
|
|
||||||
// player cursor position
|
// player cursor position
|
||||||
uint8_t cursor = 0;
|
uint8_t pos_x = 0;
|
||||||
|
uint8_t pos_y = 0;
|
||||||
|
#define Cursor (pos_y * WIDTH + pos_x)
|
||||||
|
|
||||||
uint8_t animframe = 0;
|
uint8_t animframe = 0;
|
||||||
|
|
||||||
bool hide_timeout_match;
|
bool hide_timeout_match;
|
||||||
@@ -220,46 +226,36 @@ void button_click(uint8_t n)
|
|||||||
{
|
{
|
||||||
switch (n) {
|
switch (n) {
|
||||||
case D_UP:
|
case D_UP:
|
||||||
if (cursor < BOARD_WIDTH) // first row
|
dec_wrap(pos_y, 0, HEIGHT);
|
||||||
cursor += (CARD_COUNT - BOARD_WIDTH);
|
|
||||||
else
|
|
||||||
cursor -= BOARD_WIDTH;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case D_DOWN:
|
case D_DOWN:
|
||||||
if (cursor >= (CARD_COUNT - BOARD_WIDTH)) // last row
|
inc_wrap(pos_y, 0, HEIGHT);
|
||||||
cursor -= (CARD_COUNT - BOARD_WIDTH);
|
|
||||||
else
|
|
||||||
cursor += BOARD_WIDTH;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case D_LEFT:
|
case D_LEFT:
|
||||||
if (cursor > 0) // last row
|
if (pos_x == 0) dec_wrap(pos_y, 0, HEIGHT); // go to previous row
|
||||||
cursor--;
|
dec_wrap(pos_x, 0, WIDTH);
|
||||||
else
|
|
||||||
cursor = (CARD_COUNT - 1);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case D_RIGHT:
|
case D_RIGHT:
|
||||||
if (cursor < (CARD_COUNT - 1)) // last row
|
if (pos_x == WIDTH - 1) inc_wrap(pos_y, 0, HEIGHT); // go to next row
|
||||||
cursor++;
|
inc_wrap(pos_x, 0, WIDTH);
|
||||||
else
|
|
||||||
cursor = 0;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case D_SELECT:
|
case D_SELECT:
|
||||||
if (tiles_revealed == 2) break; // two already shown
|
if (tiles_revealed == 2) break; // two already shown
|
||||||
if (board[cursor].state != SECRET) break; // selected tile not secret
|
if (board[Cursor].state != SECRET) break; // selected tile not secret
|
||||||
|
|
||||||
// reveal a tile
|
// reveal a tile
|
||||||
if (tiles_revealed < 2) {
|
if (tiles_revealed < 2) {
|
||||||
board[cursor].state = REVEALED;
|
board[Cursor].state = REVEALED;
|
||||||
tiles_revealed++;
|
tiles_revealed++;
|
||||||
|
|
||||||
if(tiles_revealed == 1) {
|
if(tiles_revealed == 1) {
|
||||||
tile1 = cursor;
|
tile1 = Cursor;
|
||||||
} else {
|
} else {
|
||||||
tile2 = cursor;
|
tile2 = Cursor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,6 +269,9 @@ void button_click(uint8_t n)
|
|||||||
|
|
||||||
case D_RESTART:
|
case D_RESTART:
|
||||||
deal_cards();
|
deal_cards();
|
||||||
|
if (board[Cursor].state == GONE)
|
||||||
|
safe_press_arrow_key(D_RIGHT);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -281,20 +280,49 @@ void button_click(uint8_t n)
|
|||||||
/** Press arrow key, skip empty tiles */
|
/** Press arrow key, skip empty tiles */
|
||||||
void safe_press_arrow_key(uint8_t n)
|
void safe_press_arrow_key(uint8_t n)
|
||||||
{
|
{
|
||||||
// attempt to arrive at some secret tile
|
// preserve old position
|
||||||
for (uint8_t j = 0; j < BOARD_HEIGHT; j++) {
|
const uint8_t x1 = pos_x;
|
||||||
|
const uint8_t y1 = pos_y;
|
||||||
|
|
||||||
for (uint8_t k = 0; k < BOARD_WIDTH; k++) {
|
if (n == D_LEFT || n == D_RIGHT) {
|
||||||
|
|
||||||
|
// traverse all buttons
|
||||||
|
for (uint8_t i = 0; i < CARD_COUNT; i++) {
|
||||||
button_click(n);
|
button_click(n);
|
||||||
if (board[cursor].state != GONE) break;
|
|
||||||
|
if (board[Cursor].state != GONE) return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (board[cursor].state != GONE) break;
|
} else {
|
||||||
|
|
||||||
// traverse right since current column is empty
|
for (uint8_t i = 0; i < HEIGHT; i++) {
|
||||||
//
|
|
||||||
button_click(D_RIGHT);
|
// Go up/down
|
||||||
|
button_click(n);
|
||||||
|
if (board[Cursor].state != GONE) return;
|
||||||
|
|
||||||
|
for (int8_t x = 0; x < WIDTH; x++) {
|
||||||
|
|
||||||
|
if ((int8_t) pos_x - x >= 0) {
|
||||||
|
if (board[Cursor - x].state != GONE) {
|
||||||
|
pos_x -= x;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((int8_t) pos_x + x < WIDTH) {
|
||||||
|
if (board[Cursor + x].state != GONE) {
|
||||||
|
pos_x += x;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// restore original position
|
||||||
|
pos_x = x1;
|
||||||
|
pos_y = y1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -331,10 +359,10 @@ void update()
|
|||||||
board[tile1].state = GONE;
|
board[tile1].state = GONE;
|
||||||
board[tile2].state = GONE;
|
board[tile2].state = GONE;
|
||||||
|
|
||||||
if (board[cursor].state == GONE) {
|
if (board[Cursor].state == GONE) {
|
||||||
// move to some other tile
|
// move to some other tile
|
||||||
// try not to change row if possible
|
// try not to change row if possible
|
||||||
if ((cursor % BOARD_WIDTH) == (BOARD_WIDTH - 1))
|
if ((Cursor % WIDTH) == (WIDTH - 1))
|
||||||
safe_press_arrow_key(D_LEFT);
|
safe_press_arrow_key(D_LEFT);
|
||||||
else
|
else
|
||||||
safe_press_arrow_key(D_RIGHT);
|
safe_press_arrow_key(D_RIGHT);
|
||||||
@@ -350,7 +378,7 @@ void update()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Animation for pulsing the active color
|
// Animation for pulsing the active color
|
||||||
inc_wrap(animframe, 0, F_ANIM_LEN * 2);
|
inc_wrapi(animframe, 0, F_ANIM_LEN * 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// LED off
|
// LED off
|
||||||
@@ -384,7 +412,7 @@ void render()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// pulse active tile
|
// pulse active tile
|
||||||
if (i == cursor) {
|
if (i == Cursor) {
|
||||||
uint16_t mult;
|
uint16_t mult;
|
||||||
|
|
||||||
if (animframe < F_ANIM_LEN) {
|
if (animframe < F_ANIM_LEN) {
|
||||||
|
|||||||
Reference in New Issue
Block a user