source import

pull/1/head
Ondřej Hruška 10 years ago
parent 2690dea369
commit d2f22580f4
  1. 25
      README.md
  2. 46
      lib/adc.c
  3. 19
      lib/adc.h
  4. 42
      lib/arduino_pins.h
  5. 89
      lib/calc.h
  6. 84
      lib/colors.h
  7. 45
      lib/debounce.c
  8. 63
      lib/debounce.h
  9. 89
      lib/hsl.c
  10. 19
      lib/hsl.h
  11. 284
      lib/lcd.c
  12. 130
      lib/lcd.h
  13. 22
      lib/loops.h
  14. 6
      lib/meta.h
  15. 21
      lib/nsdelay.h
  16. 107
      lib/pins.h
  17. 97
      lib/uart.c
  18. 43
      lib/uart.h
  19. 127
      lib/ws_rgb.h
  20. 1
      lib_tests/lib
  21. 8
      lib_tests/run_tests.sh
  22. 356
      lib_tests/test_calc.c
  23. 26
      lib_tests/test_loops_visual.c
  24. 34
      lib_tests/test_yeolde.c
  25. BIN
      lib_tests/x

@ -1,2 +1,23 @@
# avr-lib
Utilities for AVR programming in C, aimed at m328 / Arduino.
# MightyPork's AVR Library
I program my Arduinos in plain C, compile it with `avr-gcc` and flash with `avrdude` (all on Linux).
Whenever I learn how to do something, I make a library file and put it here.
The code is tested and optimized for **ATmega328P**, which is used in most Arduinos. I use "Pro Mini" and "Nano".
# How to use
Link the `lib/` folder to your project, and make sure you add all lib `.c` files to your `Makefile`, so it builds are the needed code.
Some library files don't have `.c`, but many do.
## Useful things
- To easily alias I/O pins, use `lib/pins.h`.
- For Arduino pins, there are presets in `lib/arduino-pins.h`
- Binary/byte manipulation utilities are in `lib/calc.h`
- `lib/meta.h` contains some generally useful things that didn't fit elsewhere
Each header file contains a comment block with explanation, which will help you understand them.

@ -0,0 +1,46 @@
#include <avr/io.h>
#include <stdbool.h>
#include "calc.h"
#include "adc.h"
/** Initialize the ADC */
void adc_init()
{
ADCSRA |= _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0); // 128 prescaler -> 125 kHz
ADMUX |= _BV(REFS0); // Voltage reference
sbi(ADCSRA, ADEN); // Enable ADC
}
/** Disable AD */
void adc_disable()
{
cbi(ADCSRA, ADEN);
}
/** Sample analog pin with 8-bit precision */
uint8_t adc_read_byte(uint8_t channel)
{
write_low_nibble(ADMUX, channel); // Select channel to sample
sbi(ADMUX, ADLAR); // Align result to left
sbi(ADCSRA, ADSC); // Start conversion
while(bit_is_high(ADCSRA, ADSC)); // Wait for it...
return ADCH; // The upper 8 bits of ADC result
}
/** Sample analog pin with 10-bit precision */
uint16_t adc_read_word(uint8_t channel)
{
write_low_nibble(ADMUX, channel); // Select channel to sample
cbi(ADMUX, ADLAR); // Align result to right
sbi(ADCSRA, ADSC); // Start conversion
while(get_bit(ADCSRA, ADSC)); // Wait for it...
return ADCW; // The whole ADC word (10 bits)
}

@ -0,0 +1,19 @@
#pragma once
/*
Utilities for build-in A/D converter
*/
#include <avr/io.h>
/** Initialize the ADC */
void adc_init();
/** Disable AD (for power saving?) */
void adc_disable();
/** Sample analog pin with 8-bit precision */
uint8_t adc_read_byte(uint8_t channel);
/** Sample analog pin with 10-bit precision */
uint16_t adc_read_word(uint8_t channel);

@ -0,0 +1,42 @@
#pragma once
/**
Pin definitions for Arduino (Pro Mini with ATmega328P)
*/
#include "pins.h"
#define D0 D,0
#define D1 D,1
#define D2 D,2
#define D3 D,3
#define D4 D,4
#define D5 D,5
#define D6 D,6
#define D7 D,7
#define D8 B,0
#define D9 B,1
#define D10 B,2
// MOSI MISO SCK - not good for input
#define D11 B,3
#define D12 B,4
#define D13 B,5
#define D14 C,0
#define D15 C,1
#define D16 C,2
#define D17 C,3
#define D18 C,4
#define D19 C,5
#define D20 C,6
#define D21 C,7
#define A0 C,0
#define A1 C,1
#define A2 C,2
#define A3 C,3
#define A4 C,4
#define A5 C,5
#define A6 C,6
#define A7 C,7

@ -0,0 +1,89 @@
#pragma once
/**
Bit and byte manipulation utilities
*/
// --- Increment in range ---
// 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)
// --- 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)
// Clear bit
#define cbi(reg, bit) do { (reg) &= ~(1 << (uint8_t)(bit)); } while(0)
// Get n-th bit
#define read_bit(reg, bit) (((reg) >> (uint8_t)(bit)) & 0x1)
#define get_bit(reg, bit) read_bit(reg, bit)
// 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 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)
// --- 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)
// Clear n-th bit in pointee
#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 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 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)
// --- 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_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)
// --- Range tests ---
// 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))))

@ -0,0 +1,84 @@
#pragma once
/*
Some useful utilities for RGB color manipulation
The XXXc macros don't use cast, so they can be used in array initializers.
xrgb ... 3-byte true-color RGB (8 bits per component)
rgbXX ... XX-bit color value, with equal nr of bits per component
XX_r (_g, _b) ... extract component from the color, and convert it to 0..255
*/
typedef struct {
uint8_t r;
uint8_t g;
uint8_t b;
} xrgb_t;
typedef uint32_t rgb24_t;
typedef uint16_t rgb15_t;
typedef uint16_t rgb12_t;
typedef uint8_t rgb6_t;
#define xrgb(rr, gg, bb) ((xrgb_t)xrgbc(rr, gg, bb))
// xrgb for constant array declarations
#define xrgbc(rr, gg, bb) { .r = ((uint8_t)(rr)), .g = ((uint8_t)(gg)), .b = ((uint8_t)(bb)) }
#define xrgb_r(c) ((uint8_t)(c.r))
#define xrgb_g(c) ((uint8_t)(c.g))
#define xrgb_b(c) ((uint8_t)(c.b))
#define xrgb_rgb24(c) ((((rgb24_t)c.r) << 16) | (((rgb24_t)c.g) << 8) | (((rgb24_t)c.b)))
#define xrgb_rgb15(c) (((((rgb15_t)c.r) & 0xF8) << 7) | ((((rgb15_t)c.g) & 0xF8) << 2) | ((((rgb15_t)c.b) & 0xF8) >> 3))
#define xrgb_rgb12(c) (((((rgb12_t)c.r) & 0xF0) << 4) | ((((rgb12_t)c.g) & 0xF0)) | ((((rgb12_t)c.b) & 0xF0) >> 4))
#define xrgb_rgb6(c) (((((rgb6_t)c.r) & 0xC0) >> 2) | ((((rgb6_t)c.g) & 0xC0) >> 4) | ((((rgb6_t)c.b) & 0xC0) >> 6))
#define rgb24c(r,g,b) (((((rgb24_t)r) & 0xFF) << 16) | ((((rgb24_t)g) & 0xFF) << 8) | (((rgb24_t)b) & 0xFF))
#define rgb24(r,g,b) ((rgb24_t) rgb24(r,g,b))
#define rgb24_r(c) ((((rgb24_t) (c)) >> 16) & 0xFF)
#define rgb24_g(c) ((((rgb24_t) (c)) >> 8) & 0xFF)
#define rgb24_b(c) ((((rgb24_t) (c)) >> 0) & 0xFF)
#define rgb24_xrgb(c) xrgb(rgb24_r(c), rgb24_g(c), rgb24_b(c))
#define rgb24_xrgbc(c) xrgbc(rgb24_r(c), rgb24_g(c), rgb24_b(c))
#define rgb15(r,g,b) ((rgb15_t) rgb15c(r,g,b))
#define rgb15c(r,g,b) (((r & 0x1F) << 10) | ((g & 0x1F) << 5) | (b & 0x1F))
#define rgb15_r(c) ((((rgb15_t) (c)) & 0x7C00) >> 7)
#define rgb15_g(c) ((((rgb15_t) (c)) & 0x3E0) >> 2)
#define rgb15_b(c) ((((rgb15_t) (c)) & 0x1F) << 3)
#define rgb15_xrgb(c) xrgb(rgb15_r(c), rgb15_g(c), rgb15_b(c))
#define rgb15_rgb24(c) rgb24(rgb15_r(c), rgb15_g(c), rgb15_b(c))
#define rgb15_rgb24c(c) rgb24c(rgb15_r(c), rgb15_g(c), rgb15_b(c))
#define rgb12(r,g,b) ((rgb12_t) rgb12c(r,g,b))
#define rgb12c(r,g,b) (((r & 0xF) << 8) | ((g & 0xF) << 4) | (b & 0xF))
#define rgb12_r(c) ((((rgb12_t) (c)) & 0xF00) >> 4)
#define rgb12_g(c) (((rgb12_t) (c)) & 0xF0)
#define rgb12_b(c) (((r(rgb12_t) (c)gb) & 0x0F) << 4)
#define rgb12_xrgb(c) xrgb(rgb12_r(c), rgb12_g(c), rgb12_b(c))
#define rgb12_xrgbc(c) xrgbc(rgb12_r(c), rgb12_g(c), rgb12_b(c))
#define rgb12_rgb24(c) rgb24(rgb12_r(c), rgb12_g(c), rgb12_b(c))
#define rgb12_rgb24c(c) rgb24c(rgb12_r(c), rgb12_g(c), rgb12_b(c))
#define rgb6(r,g,b) ((rgb6_t) rgb6c(r,g,b))
#define rgb6c(r,g,b) (((r & 3) << 4) | ((g & 3) << 2) | (b & 3))
#define rgb6_r(c) ((((rgb6_t) (c)) & 0x30) << 2)
#define rgb6_g(c) ((((rgb6_t) (c)) & 0xC) << 4)
#define rgb6_b(c) ((((rgb6_t) (c)) & 0x3) << 6)
#define rgb6_xrgb(c) xrgb(rgb6_r(c), rgb6_g(c), rgb6_b(c))
#define rgb6_xrgbc(c) xrgbc(rgb6_r(c), rgb6_g(c), rgb6_b(c))
#define rgb6_rgb24(c) rgb24(rgb6_r(c), rgb6_g(c), rgb6_b(c))
#define rgb6_rgb24c(c) rgb24c(rgb6_r(c), rgb6_g(c), rgb6_b(c))
#define add_xrgb(x, y) ((xrgb_t) { (((y).r > (255 - (x).r)) ? 255 : ((x).r + (y).r)), (((y).g > (255 - (x).g)) ? 255 : ((x).g + (y).g)), (((y).b > 255 - (x).b) ? 255 : ((x).b + (y).b)) })

@ -0,0 +1,45 @@
#include <avr/io.h>
#include <stdbool.h>
#include "debounce.h"
#include "calc.h"
#include "pins.h"
#include "debo_config.h"
/** Debounce data array */
uint8_t debo_next_slot = 0;
uint8_t debo_register(PORT_P reg, uint8_t bit, bool invert)
{
debo_slots[debo_next_slot] = (debo_slot_t){
.reg = reg,
.bit = bit | ((invert & 1) << 7) | (get_bit_p(reg, bit) << 6), // bit 7 = invert, bit 6 = state
.count = 0,
};
return debo_next_slot++;
}
/** Check debounced pins, should be called periodically. */
void debo_tick()
{
for (uint8_t i = 0; i < debo_next_slot; i++) {
// current pin value (right 3 bits, xored with inverse bit)
bool value = get_bit_p(debo_slots[i].reg, debo_slots[i].bit & 0x7);
if (value != get_bit(debo_slots[i].bit, 6)) {
// different pin state than last recorded state
if (debo_slots[i].count < DEBO_TICKS) {
debo_slots[i].count++;
} else {
// overflown -> latch value
set_bit(debo_slots[i].bit, 6, value); // set state bit
debo_slots[i].count = 0;
}
} else {
debo_slots[i].count = 0; // reset the counter
}
}
}

@ -0,0 +1,63 @@
#pragma once
/**
An implementation of button debouncer.
----
You must provide a config file debo_config.h (next to your main.c)
Example:
#pragma once
#define DEBO_CHANNELS 2
#define DDEBO_TICKS 5
----
A pin is registered like this:
#define BTN1 B,0
#define BTN2 B,1
debo_add(BTN0); // The function returns number assigned to the pin (0, 1, ...)
debo_add_rev(BTN1); // active low
debo_register(&PINB, PB2, 0); // direct access - register, pin & invert
Then periodically call the tick function (perhaps in a timer interrupt):
debo_tick();
To check if input is active, use
debo_get_pin(0); // state of input #0 (registered first)
debo_get_pin(1); // state of input #1 (registered second)
*/
#include <avr/io.h>
#include <stdbool.h>
#include "calc.h"
#include "pins.h"
#include "debo_config.h"
/* Internal deboucer entry */
typedef struct {
PORT_P reg; // pointer to IO register
uint8_t bit; // bits 6 and 7 of this hold "state" & "invert" flag
uint8_t count; // number of ticks this was in the new state
} debo_slot_t;
debo_slot_t debo_slots[DEBO_CHANNELS];
/** Add a pin for debouncing */
#define debo_add_rev(io) debo_register(&io2pin(io_pack(io)), io2n(io_pack(io)), 1)
#define debo_add(io) debo_register(&io2pin(io_pack(io)), io2n(io_pack(io)), 0)
/** Add a pin for debouncing (low level function) */
uint8_t debo_register(PORT_P pin_reg_pointer, uint8_t bit, bool invert);
/** Check debounced pins, should be called periodically. */
void debo_tick();
/** 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))

@ -0,0 +1,89 @@
#include <stdlib.h>
#include <stdint.h>
#include "colors.h"
#include "hsl.h"
#ifdef HSL_LINEAR
const uint8_t FADE_128[] = {
0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4,
5, 5, 6, 6, 6, 7, 7, 8, 8, 8, 9, 10, 10, 10, 11, 12, 13, 14,
14, 15, 16, 17, 18, 20, 21, 22, 24, 26, 27, 28, 30, 31, 32, 34, 35, 36,
38, 39, 40, 41, 42, 44, 45, 46, 48, 49, 50, 52, 54, 56, 58, 59, 61, 63,
65, 67, 68, 69, 71, 72, 74, 76, 78, 80, 82, 85, 88, 90, 92, 95, 98, 100,
103, 106, 109, 112, 116, 119, 122, 125, 129, 134, 138, 142, 147, 151,
153, 156, 160, 163, 165, 170, 175, 180, 185, 190, 195, 200, 207, 214, 218,
221, 225, 228, 232, 234, 241, 248, 254, 255
};
#endif
// based on: https://github.com/lewisd32/avr-hsl2rgb
xrgb_t hsl2xrgb(const hsl_t cc)
{
// 0 .. 256*3
const uint16_t hh = (uint16_t) cc.h * 3;
const uint8_t hue_mod = hh % 256;
uint8_t r_temp, g_temp, b_temp;
if (hh < 256) {
r_temp = hue_mod ^ 255;
g_temp = hue_mod;
b_temp = 0;
} else if (hh < 512) {
r_temp = 0;
g_temp = hue_mod ^ 255;
b_temp = hue_mod;
} else if (hh < 768) {
r_temp = hue_mod;
g_temp = 0;
b_temp = hue_mod ^ 255;
} else {
r_temp = 0;
g_temp = 0;
b_temp = 0;
}
const uint8_t inverse_sat = (cc.s ^ 255);
xrgb_t rgb;
uint8_t t8;
uint16_t t16;
#ifdef HSL_LINEAR
const uint8_t bri = FADE_128[cc.l>>1];
#else
const uint8_t bri = cc.l;
#endif
t8 = r_temp;
t16 = t8 * cc.s + t8;
t16 = t16 + t8;
t8 = t16 >> 8;
t8 = t8 + inverse_sat;
t16 = t8 * bri;
t16 = t16 + t8;
t8 = t16 >> 8;
rgb.r = t8;
t8 = g_temp;
t16 = t8 * cc.s;
t16 = t16 + t8;
t8 = t16 >> 8;
t8 = t8 + inverse_sat;
t16 = t8 * bri;
t16 = t16 + t8;
t8 = t16 >> 8;
rgb.g = t8;
t8 = b_temp;
t16 = t8 * cc.s;
t16 = t16 + t8;
t8 = t16 >> 8;
t8 = t8 + inverse_sat;
t16 = t8 * bri;
t16 = t16 + t8;
t8 = t16 >> 8;
rgb.b = t8;
return rgb;
}

@ -0,0 +1,19 @@
#pragma once
/*
HSL support (addition to colors.h)
*/
#include "colors.h"
// Define HSL_LINEAR to get more linear brightness in hsl->rgb conversion
// HSL data structure
typedef struct {
uint8_t h;
uint8_t s;
uint8_t l;
} hsl_t;
/* Convert HSL to XRGB */
xrgb_t hsl2xrgb(const hsl_t color);

@ -0,0 +1,284 @@
#include <stdbool.h>
#include <stdint.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "calc.h"
#include "pins.h"
#include "nsdelay.h"
#include "lcd.h"
#include "lcd_config.h"
// Start address of rows
const uint8_t LCD_ROW_ADDR[] = {0x00, 0x40, 0x14, 0x54};
// Internal prototypes
void _lcd_mode_r();
void _lcd_mode_w();
void _lcd_clk();
void _lcd_wait_bf();
void _lcd_write_byte(uint8_t bb);
uint8_t _lcd_read_byte();
// Write utilities
#define _lcd_write_low(bb) _lcd_write_nibble((bb) & 0x0F)
#define _lcd_write_high(bb) _lcd_write_nibble(((bb) & 0xF0) >> 4)
#define _lcd_write_nibble(nib) do { \
write_pin(LCD_D7, get_bit((nib), 3)); \
write_pin(LCD_D6, get_bit((nib), 2)); \
write_pin(LCD_D5, get_bit((nib), 1)); \
write_pin(LCD_D4, get_bit((nib), 0)); \
} while(0)
// 0 W, 1 R
bool _lcd_mode;
/** Initialize the display */
void lcd_init()
{
// configure pins as output
as_output(LCD_E);
as_output(LCD_RW);
as_output(LCD_RS);
_lcd_mode = 1; // force data pins to output
_lcd_mode_w();
// Magic sequence to invoke Cthulhu (or enter 4-bit mode)
_delay_ms(16);
_lcd_write_nibble(0b0011);
_lcd_clk();
_delay_ms(5);
_lcd_clk();
_delay_ms(5);
_lcd_clk();
_delay_ms(5);
_lcd_write_nibble(0b0010);
_lcd_clk();
_delay_us(100);
// Configure the display
lcd_write_command(LCD_IFACE_4BIT_2LINE);
lcd_write_command(LCD_DISABLE);
lcd_write_command(LCD_CLEAR);
lcd_write_command(LCD_MODE_INC);
// mark as enabled
lcd_enable();
}
/** Send a pulse on the ENABLE line */
void _lcd_clk()
{
pin_up(LCD_E);
delay_ns(420);
pin_down(LCD_E);
}
/** Enter READ mode */
void _lcd_mode_r()
{
if (_lcd_mode == 1) return; // already in R mode
pin_up(LCD_RW);
as_input_pu(LCD_D7);
as_input_pu(LCD_D6);
as_input_pu(LCD_D5);
as_input_pu(LCD_D4);
_lcd_mode = 1;
}
/** Enter WRITE mode */
void _lcd_mode_w()
{
if (_lcd_mode == 0) return; // already in W mode
pin_down(LCD_RW);
as_output(LCD_D7);
as_output(LCD_D6);
as_output(LCD_D5);
as_output(LCD_D4);
_lcd_mode = 0;
}
/** Read a byte */
uint8_t _lcd_read_byte()
{
_lcd_mode_r();
uint8_t res = 0;
_lcd_clk();
res = (read_pin(LCD_D7) << 7) | (read_pin(LCD_D6) << 6) | (read_pin(LCD_D5) << 5) | (read_pin(LCD_D4) << 4);
_lcd_clk();
res |= (read_pin(LCD_D7) << 3) | (read_pin(LCD_D6) << 2) | (read_pin(LCD_D5) << 1) | (read_pin(LCD_D4) << 0);
return res;
}
/** Write an instruction byte */
void lcd_write_command(uint8_t bb)
{
_lcd_wait_bf();
pin_down(LCD_RS); // select instruction register
_lcd_write_byte(bb); // send instruction byte
}
/** Write a data byte */
void lcd_write_data(uint8_t bb)
{
_lcd_wait_bf();
pin_up(LCD_RS); // select data register
_lcd_write_byte(bb); // send data byte
}
/** Read BF & Address */
uint8_t lcd_read_bf_addr()
{
pin_down(LCD_RS);
return _lcd_read_byte();
}
/** Read CGRAM or DDRAM */
uint8_t lcd_read_ram()
{
pin_up(LCD_RS);
return _lcd_read_byte();
}
/** Write a byte using the 8-bit interface */
void _lcd_write_byte(uint8_t bb)
{
_lcd_mode_w(); // enter W mode
_lcd_write_high(bb);
_lcd_clk();
_lcd_write_low(bb);
_lcd_clk();
}
/** Wait until the device is ready */
void _lcd_wait_bf()
{
uint8_t d = 0;
while(d++ < 120 && lcd_read_bf_addr() & _BV(7))
_delay_us(1);
}
/** Send a string to LCD */
void lcd_puts(char* str_p)
{
while (*str_p)
lcd_putc(*str_p++);
}
/** Sedn a char to LCD */
void lcd_putc(const char c)
{
lcd_write_data(c);
}
/** Set cursor position */
void lcd_xy(const uint8_t x, const uint8_t y)
{
lcd_set_addr(LCD_ROW_ADDR[y] + (x));
}
uint8_t _lcd_old_cursor = CURSOR_NONE;
bool _lcd_enabled = false;
/** Set LCD cursor. If not enabled, only remember it. */
void lcd_cursor(uint8_t type)
{
_lcd_old_cursor = (type & CURSOR_BOTH);
if (_lcd_enabled) lcd_write_command(LCD_CURSOR_NONE | _lcd_old_cursor);
}
/** Display display (preserving cursor) */
void lcd_disable()
{
lcd_write_command(LCD_DISABLE);
_lcd_enabled = false;
}
/** Enable display (restoring cursor) */
void lcd_enable()
{
_lcd_enabled = true;
lcd_cursor(_lcd_old_cursor);
}
/** Go home */
void lcd_home()
{
lcd_write_command(LCD_HOME);
}
/** Clear the screen */
void lcd_clear()
{
lcd_write_command(LCD_CLEAR);
}
/** Define a glyph */
void lcd_define_glyph(const uint8_t index, const uint8_t* array)
{
lcd_set_addr_cgram(index * 8);
for (uint8_t i = 0; i < 8; ++i) {
lcd_write_data(array[i]);
}
}
/** Define a glyph */
void lcd_define_glyph_pgm(const uint8_t index, const uint8_t* array)
{
lcd_set_addr_cgram(index * 8);
for (uint8_t i = 0; i < 8; ++i) {
lcd_write_data(pgm_read_byte(&array[i]));
}
}
/** Set address in CGRAM */
void lcd_set_addr_cgram(const uint8_t acg)
{
lcd_write_command(0b01000000 | ((acg) & 0b00111111));
}
/** Set address in DDRAM */
void lcd_set_addr(const uint8_t add)
{
lcd_write_command(0b10000000 | ((add) & 0b01111111));
}

@ -0,0 +1,130 @@
#pragma once
/*
HD44780 LCD display driver - 4-bit mode
LCD pins are configured using a file lcd_config.h, which you
have to add next to your main.c file.
Content can be something like this:
#pragma once
#include "lib/arduino_pins.h"
#define LCD_RS D10
#define LCD_RW D11
#define LCD_E D12
#define LCD_D4 D13
#define LCD_D5 D14
#define LCD_D6 D15
#define LCD_D7 D16
*/
#include <stdint.h>
#include "lcd_config.h"
// Commands
// Clear screen (reset)
#define LCD_CLEAR 0b00000001
// Move cursor to (0,0), unshift...
#define LCD_HOME 0b00000010
// Set mode: Increment + NoShift
#define LCD_MODE_INC 0b00000110
// Set mode: Increment + Shift
#define LCD_MODE_INC_SHIFT 0b00000111
// Set mode: Decrement + NoShift
#define LCD_MODE_DEC 0b00000100
// Set mode: Decrement + Shift
#define LCD_MODE_DEC_SHIFT 0b00000101
// Disable display (data remains untouched)
#define LCD_DISABLE 0b00001000
// Disable cursor
#define LCD_CURSOR_NONE 0b00001100
// Set cursor to still underscore
#define LCD_CURSOR_BAR 0b00001110
// Set cursor to blinking block
#define LCD_CURSOR_BLINK 0b00001101
// Set cursor to both of the above at once
#define LCD_CURSOR_BOTH (LCD_CURSOR_BAR | LCD_CURSOR_BLINK)
// Move cursor
#define LCD_MOVE_LEFT 0b00010000
#define LCD_MOVE_RIGHT 0b00010100
// Shift display
#define LCD_SHIFT_LEFT 0b00011000
#define LCD_SHIFT_RIGHT 0b00011100
// Set iface to 5x7 font, 1-line
#define LCD_IFACE_4BIT_1LINE 0b00100000
#define LCD_IFACE_8BIT_1LINE 0b00110000
// Set iface to 5x7 font, 2-line
#define LCD_IFACE_4BIT_2LINE 0b00101000
#define LCD_IFACE_8BIT_2LINE 0b00111000
/** Initialize the display */
void lcd_init();
/** Write an instruction byte */
void lcd_write_command(uint8_t bb);
/** Write a data byte */
void lcd_write_data(uint8_t bb);
/** Read BF & Address */
uint8_t lcd_read_bf_addr();
/** Read CGRAM or DDRAM */
uint8_t lcd_read_ram();
/** Send a string to LCD */
void lcd_puts(char* str_p);
/** Sedn a char to LCD */
void lcd_putc(const char c);
/** Show string at X, Y */
#define lcd_str_xy(x, y, str_p) do { lcd_xy((x), (y)); lcd_puts((str_p)); } while(0)
/** Show char at X, Y */
#define lcd_char_xy(x, y, c) do { lcd_xy((x), (y)); lcd_putc((c)); } while(0)
/** Set cursor position */
void lcd_xy(const uint8_t x, const uint8_t y);
/** Set LCD cursor. If not enabled, only remember it. */
#define CURSOR_NONE 0b00
#define CURSOR_BAR 0b10
#define CURSOR_BLINK 0b01
#define CURSOR_BOTH 0b11
void lcd_cursor(uint8_t type);
/** Display display (preserving cursor) */
void lcd_disable();
/** Enable display (restoring cursor) */
void lcd_enable();
/** Go home */
void lcd_home();
/** Clear the screen */
void lcd_clear();
/** Define a glyph */
void lcd_define_glyph(const uint8_t index, const uint8_t* array);
/** Define a glyph that's in PROGMEM */
void lcd_define_glyph_pgm(const uint8_t index, const uint8_t* array);
/** Set address in CGRAM */
void lcd_set_addr_cgram(const uint8_t acg);
/** Set address in DDRAM */
void lcd_set_addr(const uint8_t add);

@ -0,0 +1,22 @@
#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))

@ -0,0 +1,6 @@
#pragma once
/** Weird constructs for the compiler */
// general macros
#define SECTION(pos) __attribute__((naked, used, section(pos)))

@ -0,0 +1,21 @@
#pragma once
/**
Functions for precise delays (nanoseconds / cycles)
*/
#include <avr/io.h>
#include <util/delay_basic.h>
#include <stdint.h>
/* Convert nanoseconds to cycle count */
#define ns2cycles(ns) ( (ns) / (1000000000L / (signed long) F_CPU) )
/** Wait c cycles */
#define delay_c(c) (((c) > 0) ? __builtin_avr_delay_cycles(c) : __builtin_avr_delay_cycles(0))
/** Wait n nanoseconds, plus c cycles */
#define delay_ns_c(ns, c) delay_c(ns2cycles(ns) + (c))
/** Wait n nanoseconds */
#define delay_ns(ns) delay_c(ns2cycles(ns))

@ -0,0 +1,107 @@
#pragma once
/**
This file provides macros for pin manipulation.
You can define your application pins like so:
// Led at PORTB, pin 1
#define LED B,1
// Switch at PORTD, pin 7
#define SW1 D,7
Now you can use macros from this file to wirh with the pins, eg:
as_output(LED);
as_input(SW1);
pullup_on(SW1);
toggle_pin(LED);
while (pin_is_low(SW1));
- The macros io2XXX() can be used to get literal name of register associated with the pin.
- io2n() provides pin number.
- The XXX_aux() macros are internal and should not be used elsewhere.
- The io_pack() macro is used to pass pin (io) to other macro without expanding it.
*/
#include <avr/io.h>
#include "calc.h"
// Get particular register associated with the name X (eg. D -> PORTD)
#define reg_ddr(X) DDR ## X
#define reg_port(X) PORT ## X
#define reg_pin(X) PIN ## X
#define io2ddr_aux(reg, bit) reg_ddr(reg)
#define io2ddr(io) io2ddr_aux(io)
#define io2port_aux(reg, bit) reg_port(reg)
#define io2port(io) io2port_aux(io)
#define io2pin_aux(reg, bit) reg_pin(reg)
#define io2pin(io) io2pin_aux(io)
#define io2n_aux(reg, bit) bit
#define io2n(io) io2n_aux(io)
#define io_pack(port, bit) port, bit
// pointer to port
typedef volatile uint8_t* PORT_P;
// number of bit in port
typedef uint8_t BIT_N;
// === pin manipulation ===
#define set_pin_aux(port, bit) sbi(reg_port(port), (bit))
#define clear_pin_aux(port, bit) cbi(reg_port(port), (bit))
#define read_pin_aux(port, bit) get_bit(reg_pin(port), (bit))
#define write_pin_aux(port, bit, value) set_bit(reg_port(port), (bit), (value))
#define toggle_pin_aux(port, bit) sbi(reg_pin(port), (bit))
#define pin_up(io) set_pin_aux(io)
#define pin_high(io) set_pin_aux(io)
#define pin_down(io) clear_pin_aux(io)
#define pin_low(io) clear_pin_aux(io)
#define get_pin(io) read_pin_aux(io)
#define read_pin(io) read_pin_aux(io)
#define pin_is_low(io) !read_pin_aux(io)
#define pin_is_high(io) read_pin_aux(io)
#define set_pin(io, value) write_pin_aux(io, (value))
#define write_pin(io, value) write_pin_aux(io, (value))
#define toggle_pin(io) toggle_pin_aux(io)
// setting pin direction
#define as_input_aux(port, bit) cbi(reg_ddr(port), (bit))
#define as_output_aux(port, bit) sbi(reg_ddr(port), (bit))
#define set_dir_aux(port, bit, dir) write_bit(reg_ddr(port), (bit), (dir))
#define as_input(io) as_input_aux(io)
#define as_input_pu(io) do { as_input_aux(io); pullup_enable_aux(io); } while(0)
#define as_output(io) as_output_aux(io)
#define set_dir(io, dir) set_dir_aux(io, (dir))
// setting pullup
#define pullup_enable_aux(port, bit) sbi(reg_port(port), (bit))
#define pullup_disable_aux(port, bit) cbi(reg_port(port), (bit))
#define set_pullup_aux(port, bit, on) write_bit(reg_port(port), (bit), (on))
#define pullup_enable(io) pullup_enable_aux(io)
#define pullup_on(io) pullup_enable_aux(io)
#define pullup_disable(io) pullup_disable_aux(io)
#define pullup_off(io) pullup_disable_aux(io)
#define set_pullup(io, on) set_pullup_aux(io, on)

@ -0,0 +1,97 @@
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdbool.h>
#include <stdint.h>
#include "uart.h"
void _uart_init_do(uint16_t ubrr) {
/*Set baud rate */
UBRR0H = (uint8_t) (ubrr >> 8);
UBRR0L = (uint8_t) ubrr;
// Enable Rx and Tx
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
// 8-bit data, 1 stop bit
UCSR0C = (0b11 << UCSZ00);
}
/** Enable or disable RX ISR */
void uart_isr_rx(bool yes)
{
if(yes) {
UCSR0B |= (1 << RXCIE0);
} else {
UCSR0B &= ~(1 << RXCIE0);
}
}
/** Enable or disable TX ISR (1 byte is sent) */
void uart_isr_tx(bool yes)
{
if(yes) {
UCSR0B |= (1 << TXCIE0);
} else {
UCSR0B &= ~(1 << TXCIE0);
}
}
/** Enable or disable DRE ISR (all is sent) */
void uart_isr_dre(bool yes)
{
if(yes) {
UCSR0B |= (1 << UDRIE0);
} else {
UCSR0B &= ~(1 << UDRIE0);
}
}
/** Send byte over UART */
void uart_tx(uint8_t data)
{
// Wait for transmit buffer
while (!uart_tx_ready());
// send it
UDR0 = data;
}
/** Send string over UART */
void uart_puts(const char* str)
{
while (*str) {
uart_tx(*str++);
}
}
/** Send progmem string over UART */
void uart_puts_pgm(const char* str)
{
char c;
while ((c = pgm_read_byte(str++))) {
uart_tx(c);
}
}
/** Receive one byte over UART */
uint8_t uart_rx()
{
// Wait for data to be received
while (!uart_rx_ready());
// Get and return received data from buffer
return UDR0;
}
/** Clear receive buffer */
void uart_flush()
{
uint8_t dummy;
while (UCSR0A & (1 << RXC0))
dummy = UDR0;
}

@ -0,0 +1,43 @@
#pragma once
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdbool.h>
#include <stdint.h>
/** Init UART for given baudrate */
#define uart_init(baud) _uart_init_do(F_CPU / 16 / (baud) - 1)
void _uart_init_do(uint16_t ubrr);
/** Check if there's a byte in the RX register */
#define uart_rx_ready() (0 != (UCSR0A & (1 << RXC0)))
/** Check if transmission of everything is done */
#define uart_tx_ready() (0 != (UCSR0A & (1 << UDRE0)))
/** Enable or disable RX ISR */
void uart_isr_rx(bool enable);
/** Enable or disable TX ISR (1 byte is sent) */
void uart_isr_tx(bool enable);
/** Enable or disable DRE ISR (all is sent) */
void uart_isr_dre(bool enable);
/** Send byte over UART */
#define uart_putc(data) uart_tx(data)
void uart_tx(uint8_t data);
/** Send string over UART */
void uart_puts(const char* str);
/** Send progmem string over UART */
void uart_puts_pgm(const char* str);
/** Receive one byte over UART */
uint8_t uart_rx();
/** Clear receive buffer */
void uart_flush();

@ -0,0 +1,127 @@
#pragma once
/**
Utils for driving a WS28xx (tested on WS2812B) RGB LED strips.
It's implemented as macros to avoid overhead when passing values, and to
enable driving multiple strips at once.
To avoid bloating your code, try to reduce the number of invocations -
compute color and then send it.
[IMPORTANT]
Some seemingly random influences can ruin the communication.
If you have enough memory, consider preparing the colors in array,
and sending this array using one of the "ws_send_XXX_array" macros.
*/
#include <avr/io.h>
#include "pins.h"
#include "nsdelay.h"
#include "colors.h"
/* Driver code for WS2812B */
// --- timing constraints (NS) ---
#ifndef WS_T_1H
# define WS_T_1H 700
#endif
#ifndef WS_T_1L
# define WS_T_1L 150
#endif
#ifndef WS_T_0H
# define WS_T_0H 150
#endif
#ifndef WS_T_0L
# define WS_T_0L 700
#endif
#ifndef WS_T_LATCH
# define WS_T_LATCH 7000
#endif
/** Wait long enough for the colors to show */
#define ws_show() do {delay_ns_c(WS_T_LATCH, 0); } while(0)
/** Send one byte to the RGB strip */
#define ws_send_byte(io, bb) do { \
for (volatile int8_t __ws_tmp = 7; __ws_tmp >= 0; --__ws_tmp) { \
if ((bb) & (1 << __ws_tmp)) { \
pin_high(io_pack(io)); delay_ns_c(WS_T_1H, -2); \
pin_low(io_pack(io)); delay_ns_c(WS_T_1L, -10); \
} else { \
pin_high(io_pack(io)); delay_ns_c(WS_T_0H, -2); \
pin_low(io_pack(io)); delay_ns_c(WS_T_0L, -10); \
} \
} \
} while(0)
/** Send R,G,B color to the strip */
#define ws_send_rgb(io, r, g, b) do { \
ws_send_byte(io_pack(io), g); \
ws_send_byte(io_pack(io), r); \
ws_send_byte(io_pack(io), b); \
} while(0)
/** Send a RGB struct */
#define ws_send_xrgb(io, xrgb) ws_send_rgb(io_pack(io), (xrgb).r, (xrgb).g, (xrgb).b)
/** Send color hex */
#define ws_send_rgb24(io, rgb) ws_send_rgb(io_pack(io), rgb24_r(rgb), rgb24_g(rgb), rgb24_b(rgb))
#define ws_send_rgb15(io, rgb) ws_send_rgb(io_pack(io), rgb15_r(rgb), rgb15_g(rgb), rgb15_b(rgb))
#define ws_send_rgb12(io, rgb) ws_send_rgb(io_pack(io), rgb12_r(rgb), rgb12_g(rgb), rgb12_b(rgb))
#define ws_send_rgb6(io, rgb) ws_send_rgb(io_pack(io), rgb6_r(rgb), rgb6_g(rgb), rgb6_b(rgb))
/** Send array of colors */
#define ws_send_xrgb_array(io, rgbs, length) __ws_send_array_proto(io_pack(io), (rgbs), (length), xrgb)
#define ws_send_rgb24_array(io, rgbs, length) __ws_send_array_proto(io_pack(io), (rgbs), (length), rgb24)
#define ws_send_rgb15_array(io, rgbs, length) __ws_send_array_proto(io_pack(io), (rgbs), (length), rgb15)
#define ws_send_rgb12_array(io, rgbs, length) __ws_send_array_proto(io_pack(io), (rgbs), (length), rgb12)
#define ws_send_rgb6_array(io, rgbs, length) __ws_send_array_proto(io_pack(io), (rgbs), (length), rgb6)
// prototype for sending array. it's ugly, sorry.
#define __ws_send_array_proto(io, rgbs, length, style) do { \
for (uint8_t __ws_sap_i = 0; __ws_sap_i < length; __ws_sap_i++) { \
style ## _t __ws_sap2 = (rgbs)[__ws_sap_i]; \
ws_send_ ## style(io_pack(io), __ws_sap2); \
} \
} while(0)
/** Send a 2D array to a zig-zag display */
#define ws_send_xrgb_array_zigzag(io, rgbs, width, height) do { \
int8_t __ws_sxaz_y, __ws_sxaz_x; \
for(__ws_sxaz_y = 0; __ws_sxaz_y < (height); __ws_sxaz_y ++) { \
for(__ws_sxaz_x = 0; __ws_sxaz_x < (width); __ws_sxaz_x++) { \
ws_send_xrgb(io_pack(io), (rgbs)[__ws_sxaz_y][__ws_sxaz_x]); \
} \
__ws_sxaz_y++; \
for(__ws_sxaz_x = (width) - 1; __ws_sxaz_x >= 0; __ws_sxaz_x--) { \
ws_send_xrgb(io_pack(io), (rgbs)[__ws_sxaz_y][__ws_sxaz_x]); \
} \
} \
} while(0)
/** Send a linear array to a zig-zag display as a n*m board (row-by-row) */
#define ws_send_xrgb_array_zigzag_linear(io, rgbs, width, height) do { \
int8_t __ws_sxazl_x, __ws_sxazl_y; \
for(__ws_sxazl_y = 0; __ws_sxazl_y < (height); __ws_sxazl_y++) { \
for(__ws_sxazl_x = 0; __ws_sxazl_x < (width); __ws_sxazl_x++) { \
ws_send_xrgb(io_pack(io), (rgbs)[__ws_sxazl_y * (width) + __ws_sxazl_x]); \
} \
__ws_sxazl_y++; \
for(__ws_sxazl_x = width-1; __ws_sxazl_x >=0; __ws_sxazl_x--) { \
ws_send_xrgb(io_pack(io), (rgbs)[__ws_sxazl_y * (width) + __ws_sxazl_x]); \
} \
} \
} while(0)

@ -0,0 +1 @@
../lib

@ -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++;
until (i == 0) {
i--;
printf("i = %d\n", i);
}
printf("\n");
loop(moo, 7) {
printf("moo = %d\n", moo);
}
}

@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include "lib/loops.h"
#include "lib/yeolde.h"
void main()
commence
let number a be 0;
whilst (a under 13)
commence
raise (a);
when (a is 5)
then
printf("A is ugly 5\n");
replay;
cease;
choose (a)
commence
option 1:
printf("A is 1\n");
shatter;
option 2:
printf("A is 2\n");
shatter;
option 3:
printf("Neither 1 nor 2\n");
cease;
cease;
cease

Binary file not shown.
Loading…
Cancel
Save