Subversion Repositories freemyipod

Rev

Rev 558 | Rev 652 | Go to most recent revision | Details | Compare with Previous | 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;
559 theseven 6
bool toggle[65];
558 theseven 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:
559 theseven 17
            toggle[pos] = true;
558 theseven 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
 
559 theseven 44
static void renderline(void* framebuf, int width, int fontwidth,
45
                       int fontheight, int* state, int line)
558 theseven 46
{
47
    int i;
48
    for (i = 0; i < 16; i++)
49
        renderchar(framebuf, 2 + fontwidth * (i + i / 4), 2 + fontheight * (2 + line), width,
50
                    pos == i + line * 16 ? 0xffffffff : 0xff000000,
51
                    pos == i + line * 16 ? 0xff000000 : 0xffffffff, 
52
                    *("01X" + state[i + line * 16]));
53
}
54
 
55
static void main()
56
{
57
    int i, j;
58
    char buf[9];
559 theseven 59
    int state[64];
60
    for (i = 0; i < 65; i++) toggle[i] = false;
558 theseven 61
    for (i = 0; i < 64; i++) state[i] = clockgate_get_state(i);
62
    uint32_t orig[2] = {0xffffffff, 0xffffffff};
63
    uint32_t now[2];
64
    for (i = 0; i < 2; i++)
65
        for (j = 0; j < 32; j++)
66
            if (state[i * 32 + j])
67
                orig[i] &= ~(1 << j);
68
    cprintf(3, "Initial state: %08X %08X\n", orig[0], orig[1]);
69
    int fontwidth = get_font_width();
70
    int fontheight = get_font_height();
71
    int width = 19 * fontwidth + 4;
72
    int height = 6 * fontheight + 4;
73
    int xoffs = (lcd_get_width() - width) / 2;
74
    int yoffs = (lcd_get_height() - height) / 2;
75
    int framebufsize = width * height * 3;
76
    void* framebuf = malloc(framebufsize);
77
    if (!framebuf) panicf(PANIC_KILLTHREAD, "Could not allocate framebuffer!");
78
    memset(framebuf, 0xff, framebufsize);
79
    memset(framebuf, 0, width * 3);
80
    memset(framebuf + (height - 1) * width * 3, 0, width * 3);
81
    for (i = 0; i < height; i++)
82
    {
83
        char* ptr = (char*)framebuf + (i * width - 1) * 3;
84
        for (j = 0; j < 6; j++) *ptr++ = 0;
85
    }
86
    wakeup_init(&eventwakeup);
87
    wakeup_signal(&eventwakeup);
88
    struct button_hook_entry* hook = button_register_handler(handler, NULL);
89
    if (!hook) panicf(PANIC_KILLTHREAD, "Could not register button hook!");
559 theseven 90
    while (!toggle[64])
558 theseven 91
    {
559 theseven 92
        wakeup_wait(&eventwakeup, 500000);
558 theseven 93
        now[0] = 0xffffffff;
94
        now[1] = 0xffffffff;
95
        for (i = 0; i < 2; i++)
96
            for (j = 0; j < 32; j++)
97
            {
98
                state[i * 32 + j] = clockgate_get_state(i * 32 + j);
559 theseven 99
                if (toggle[i * 32 + j])
100
                {
101
                    toggle[i * 32 + j] = false;
102
                    bool newstate = !state[i * 32 + j];
103
                    clockgate_enable(i * 32 + j, newstate);
104
                    state[i * 32 + j] = clockgate_get_state(i * 32 + j);
105
                    if (state[i * 32 + j] != newstate) state[i * 32 + j] = 2;
106
                }
558 theseven 107
                if (state[i * 32 + j]) now[i] &= ~(1 << j);
108
            }
559 theseven 109
        for (i = 0; i < 4; i++) renderline(framebuf, width, fontwidth, fontheight, state, i);
558 theseven 110
        renderchar(framebuf, 2 + fontwidth * 18, 2, width, pos == 64 ? 0xffffffff : 0xff000000,
111
                   pos == 64 ? 0xff000000 : 0xffffffff, 'X');
112
        snprintf(buf, sizeof(buf), "%08X", orig[0]);
113
        rendertext(framebuf, 2, 2, width, 0xff000000, 0xffffffff, buf);
114
        snprintf(buf, sizeof(buf), "%08X", orig[1]);
115
        rendertext(framebuf, 2 + 9 * fontwidth, 2, width, 0xff000000, 0xffffffff, buf);
116
        snprintf(buf, sizeof(buf), "%08X", now[0]);
117
        rendertext(framebuf, 2, 2 + fontheight, width, 0xff000000, 0xffffffff, buf);
118
        snprintf(buf, sizeof(buf), "%08X", now[1]);
119
        rendertext(framebuf, 2 + 9 * fontwidth, 2 + fontheight, width,
120
                   0xff000000, 0xffffffff, buf);
121
        displaylcd(xoffs, yoffs, width, height, framebuf, 0, 0, width);
122
    }
123
    button_unregister_handler(hook);
124
    cprintf(3, "Final state: %08X %08X\n", now[0], now[1]);
125
}
126
 
127
 
128
EMCORE_APP_HEADER("Clock gate hunter", main, 127)