Subversion Repositories freemyipod

Rev

Rev 559 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
558 theseven 1
#include "emcoreapp.h"
2
 
3
 
4
struct wakeup eventwakeup;
5
int pos = 64;
6
int state[65];
7
 
8
void handler(void* user, enum button_event eventtype, int which, int value)
9
{
10
    bool action = false;
11
    switch (eventtype)
12
    {
13
    case BUTTON_PRESS:
14
        switch (which)
15
        {
16
        case 0:
17
            state[pos] = !state[pos];
18
            action = true;
19
            break;
20
        case 1:
21
            pos++;
22
            action = true;
23
            break;
24
        case 2:
25
            pos--;
26
            action = true;
27
            break;
28
        }
29
        break;
30
    case WHEEL_FORWARD:
31
        pos++;
32
        action = true;
33
        break;
34
    case WHEEL_BACKWARD:
35
        pos--;
36
        action = true;
37
        break;
38
    }
39
    while (pos < 0) pos += 65;
40
    while (pos > 65) pos -= 65;
41
    if (action) wakeup_signal(&eventwakeup);
42
}
43
 
44
static void renderline(void* framebuf, int width, int fontwidth, int fontheight, int line)
45
{
46
    int i;
47
    for (i = 0; i < 16; i++)
48
        renderchar(framebuf, 2 + fontwidth * (i + i / 4), 2 + fontheight * (2 + line), width,
49
                    pos == i + line * 16 ? 0xffffffff : 0xff000000,
50
                    pos == i + line * 16 ? 0xff000000 : 0xffffffff, 
51
                    *("01X" + state[i + line * 16]));
52
}
53
 
54
static void main()
55
{
56
    int i, j;
57
    char buf[9];
58
    for (i = 0; i < 64; i++) state[i] = clockgate_get_state(i);
59
    state[i] = false;
60
    uint32_t orig[2] = {0xffffffff, 0xffffffff};
61
    uint32_t now[2];
62
    for (i = 0; i < 2; i++)
63
        for (j = 0; j < 32; j++)
64
            if (state[i * 32 + j])
65
                orig[i] &= ~(1 << j);
66
    cprintf(3, "Initial state: %08X %08X\n", orig[0], orig[1]);
67
    int fontwidth = get_font_width();
68
    int fontheight = get_font_height();
69
    int width = 19 * fontwidth + 4;
70
    int height = 6 * fontheight + 4;
71
    int xoffs = (lcd_get_width() - width) / 2;
72
    int yoffs = (lcd_get_height() - height) / 2;
73
    int framebufsize = width * height * 3;
74
    void* framebuf = malloc(framebufsize);
75
    if (!framebuf) panicf(PANIC_KILLTHREAD, "Could not allocate framebuffer!");
76
    memset(framebuf, 0xff, framebufsize);
77
    memset(framebuf, 0, width * 3);
78
    memset(framebuf + (height - 1) * width * 3, 0, width * 3);
79
    for (i = 0; i < height; i++)
80
    {
81
        char* ptr = (char*)framebuf + (i * width - 1) * 3;
82
        for (j = 0; j < 6; j++) *ptr++ = 0;
83
    }
84
    wakeup_init(&eventwakeup);
85
    wakeup_signal(&eventwakeup);
86
    struct button_hook_entry* hook = button_register_handler(handler, NULL);
87
    if (!hook) panicf(PANIC_KILLTHREAD, "Could not register button hook!");
88
    while (true)
89
    {
90
        wakeup_wait(&eventwakeup, TIMEOUT_BLOCK);
91
        now[0] = 0xffffffff;
92
        now[1] = 0xffffffff;
93
        for (i = 0; i < 2; i++)
94
            for (j = 0; j < 32; j++)
95
            {
96
                bool oldstate = state[i * 32 + j];
97
                clockgate_enable(i * 32 + j, oldstate);
98
                state[i * 32 + j] = clockgate_get_state(i * 32 + j);
99
                if (state[i * 32 + j] != oldstate) state[i * 32 + j] = 2;
100
                if (state[i * 32 + j]) now[i] &= ~(1 << j);
101
            }
102
        for (i = 0; i < 4; i++) renderline(framebuf, width, fontwidth, fontheight, i);
103
        renderchar(framebuf, 2 + fontwidth * 18, 2, width, pos == 64 ? 0xffffffff : 0xff000000,
104
                   pos == 64 ? 0xff000000 : 0xffffffff, 'X');
105
        snprintf(buf, sizeof(buf), "%08X", orig[0]);
106
        rendertext(framebuf, 2, 2, width, 0xff000000, 0xffffffff, buf);
107
        snprintf(buf, sizeof(buf), "%08X", orig[1]);
108
        rendertext(framebuf, 2 + 9 * fontwidth, 2, width, 0xff000000, 0xffffffff, buf);
109
        snprintf(buf, sizeof(buf), "%08X", now[0]);
110
        rendertext(framebuf, 2, 2 + fontheight, width, 0xff000000, 0xffffffff, buf);
111
        snprintf(buf, sizeof(buf), "%08X", now[1]);
112
        rendertext(framebuf, 2 + 9 * fontwidth, 2 + fontheight, width,
113
                   0xff000000, 0xffffffff, buf);
114
        displaylcd(xoffs, yoffs, width, height, framebuf, 0, 0, width);
115
        if (state[64]) break;
116
    }
117
    button_unregister_handler(hook);
118
    cprintf(3, "Final state: %08X %08X\n", now[0], now[1]);
119
}
120
 
121
 
122
EMCORE_APP_HEADER("Clock gate hunter", main, 127)