upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/unit/test_touch.c
blob: 13f04b5ffa622ebfc96081e608be81851a50b378 (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
#include "test_framework.h"
#include "../../main/touch.h"
#include <string.h>

int main(void)
{
    touch_point_t pt;
    uint8_t data[8];

    printf("=== test_touch ===\n");

    memset(data, 0, sizeof(data));
    touch_parse_raw(data, &pt);
    ASSERT(!pt.touched, "All-zero data = no touch");

    data[0] = 1;
    data[1] = 1;
    data[2] = 0;
    data[3] = 0;
    touch_parse_raw(data, &pt);
    ASSERT(!pt.touched, "data[0]=1 = no touch (gesture byte nonzero)");

    data[0] = 0;
    data[1] = 0;
    touch_parse_raw(data, &pt);
    ASSERT(!pt.touched, "data[1]=0 = no touch (touch count zero)");

    data[0] = 0;
    data[1] = 1;
    data[2] = 0x00;
    data[3] = 0x64;
    data[4] = 0x00;
    data[5] = 0xC8;
    data[6] = 0;
    data[7] = 0;
    touch_parse_raw(data, &pt);
    ASSERT(pt.touched, "Valid touch: touched=true");
    ASSERT_EQ_INT(100, (int)pt.x, "Valid touch: x=100");
    ASSERT_EQ_INT(200, (int)pt.y, "Valid touch: y=200");

    data[0] = 0;
    data[1] = 1;
    data[2] = 0x0F;
    data[3] = 0xFF;
    data[4] = 0x0F;
    data[5] = 0xFF;
    touch_parse_raw(data, &pt);
    ASSERT(pt.touched, "Max raw coords: touched=true");
    ASSERT_EQ_INT(TOUCH_MAX_X, (int)pt.x, "Max raw coords clamped to 319");
    ASSERT_EQ_INT(TOUCH_MAX_Y, (int)pt.y, "Max raw coords clamped to 479");

    data[0] = 0;
    data[1] = 1;
    data[2] = 0x05;
    data[3] = 0x00;
    data[4] = 0x08;
    data[5] = 0x00;
    touch_parse_raw(data, &pt);
    ASSERT(pt.touched, "12-bit coords: touched=true");
    ASSERT_EQ_INT(TOUCH_MAX_X, (int)pt.x, "12-bit x: (0x05 << 8) | 0x00 = 1280, clamped to 319");

    data[0] = 0;
    data[1] = 2;
    touch_parse_raw(data, &pt);
    ASSERT(!pt.touched, "data[1]=2 = too many touches, reject");

    touch_parse_raw(NULL, &pt);
    ASSERT(!pt.touched, "NULL data = no touch");

    data[0] = 0;
    data[1] = 1;
    data[2] = 0x00;
    data[3] = 0x00;
    data[4] = 0x00;
    data[5] = 0x00;
    touch_parse_raw(data, &pt);
    ASSERT(pt.touched, "Origin (0,0): touched=true");
    ASSERT_EQ_INT(0, (int)pt.x, "Origin: x=0");
    ASSERT_EQ_INT(0, (int)pt.y, "Origin: y=0");

    data[0] = 0;
    data[1] = 1;
    data[2] = 0x01;
    data[3] = 0x3F;
    data[4] = 0x01;
    data[5] = 0xDF;
    touch_parse_raw(data, &pt);
    ASSERT(pt.touched, "Mid-screen: touched=true");
    ASSERT_EQ_INT(319, (int)pt.x, "Mid-screen: x=0x13F=319");
    ASSERT_EQ_INT(479, (int)pt.y, "Mid-screen: y=0x1DF=479");

    TEST_SUMMARY();
}