aboutsummaryrefslogtreecommitdiffstats
path: root/examples/test-ring-buffer-insertion.cpp
blob: 8372a9610fb1cb17ee02fec4390b5a8b82309759 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Simple ring_buffer test.
 *
 * Does a basic test of functionality on rb_full_count(), rb_reset(),
 * rb_push_insert(), and rb_safe_insert().
 *
 * To test (no external hardware required):
 *
 *     - Connect a serial monitor to SerialUSB
 *     - Press any key
 *
 * This file is released into the public domain.
 */

#include "wirish.h"

#include "ring_buffer.h"

#define BUF_SIZE 64
ring_buffer ring_buf;
ring_buffer *rb;
uint8 rb_buffer[BUF_SIZE];

void test_rb_push_insert(int num_bytes_to_insert);
void test_rb_safe_insert(int num_bytes_to_insert);
void test_rb_insertion_function(int num_bytes_to_insert,
                                int (*insertion_fn)(ring_buffer*, uint8),
                                const char insertion_fn_name[]);
void print_rb_contents(void);

void setup() {
    rb = &ring_buf;
    rb_init(rb, BUF_SIZE, rb_buffer);

    while (!SerialUSB.available())
        ;

    SerialUSB.println("Beginning test.");
    SerialUSB.println();
}

void loop() {
    test_rb_push_insert(63);
    SerialUSB.println("------------------------------");
    test_rb_push_insert(64);
    SerialUSB.println("------------------------------");
    test_rb_safe_insert(63);
    SerialUSB.println("------------------------------");
    test_rb_safe_insert(64);
    SerialUSB.println("------------------------------");

    SerialUSB.println();
    SerialUSB.println("Test finished.");
    while (true)
        ;
}

void test_rb_push_insert(int num_bytes_to_insert) {
    test_rb_insertion_function(num_bytes_to_insert,
                               rb_push_insert,
                               "rb_push_insert()");
}

void test_rb_safe_insert(int num_bytes_to_insert) {
    test_rb_insertion_function(num_bytes_to_insert,
                               rb_safe_insert,
                               "rb_safe_insert()");
}

void test_rb_insertion_function(int num_bytes_to_insert,
                                int (*insertion_fn)(ring_buffer *, uint8),
                                const char insertion_fn_name[]) {
    SerialUSB.println("resetting ring buffer.");
    rb_reset(rb);
    print_rb_contents();

    SerialUSB.print(insertion_fn_name);
    SerialUSB.print("-ing ");
    SerialUSB.print(num_bytes_to_insert);
    SerialUSB.println(" bytes.");
    for (uint8 i = 1; i <= num_bytes_to_insert; i++)
        insertion_fn(rb, i);

    uint16 count = rb_full_count(rb);
    SerialUSB.print("rb_full_count(rb) = ");
    SerialUSB.println(count);

    print_rb_contents();
}

void print_rb_contents() {
    uint16 count = rb_full_count(rb);
    SerialUSB.print("ring buffer contents: ");
    for (uint16 i = 0; i < count; i++) {
        SerialUSB.print((int)rb_remove(rb));
        if (i < count - 1) SerialUSB.print(", ");
    }
    SerialUSB.println();
}

// Force init to be called *first*, i.e. before static object allocation.
// Otherwise, statically allocated objects that need libmaple may fail.
__attribute__((constructor)) void premain() {
    init();
}

int main(void) {
    setup();

    while (true) {
        loop();
    }
    return 0;
}