summaryrefslogtreecommitdiff
path: root/pix_driver.c
blob: 10d3d211204e0e7b5b7f222414c2f4c8284c34ab (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/gpio.h>

MODULE_LICENSE("GPL");

/* GPIO */

#define A1  17 // 0
#define A2  18 // 1
#define A3  27 // 2
#define OE  22 // 3
#define LE  23 // 4
#define SDI 24 // 5
#define CLK 25 // 6

#define LINES 8
#define PER_LINE 12

static u8 pix_screen[LINES][PER_LINE] = {
    {
        0b00000000,0b00000000,0b00000000,0b00000000,
        0b00000000,0b00000000,0b00000000,0b00000000,
        0b00000000,0b00000000,0b00000000,0b00000000,
    },
    {
        0b00000000,0b00000000,0b00000000,0b00000000,
        0b00000000,0b00000000,0b00000000,0b00000000,
        0b00000000,0b00000000,0b00000000,0b00000000,
    },
    {
        0b00000000,0b00000000,0b00000000,0b00000000,
        0b00000000,0b00000000,0b00000000,0b00000000,
        0b00000000,0b00000000,0b00000000,0b00000000,
    },
    {
        0b00000000,0b00000000,0b00000000,0b00000000,
        0b00000000,0b00000000,0b00000000,0b00000000,
        0b00000000,0b00000000,0b00000000,0b00000000,
    },
    {
        0b00000000,0b00000000,0b00000000,0b00000000,
        0b00000000,0b00000000,0b00000000,0b00000000,
        0b00000000,0b00000000,0b00000000,0b00000000,
    },
    {
        0b00000000,0b00000000,0b00000000,0b00000000,
        0b00000000,0b00000000,0b00000000,0b00000000,
        0b00000000,0b00000000,0b00000000,0b00000000,
    },
    {
        0b00000000,0b00000000,0b00000000,0b00000000,
        0b00000000,0b00000000,0b00000000,0b00000000,
        0b00000000,0b00000000,0b00000000,0b00000000,
    },
    {
        0b00000000,0b00000000,0b00000000,0b00000000,
        0b00000000,0b00000000,0b00000000,0b00000000,
        0b00000000,0b00000000,0b00000000,0b00000000,
    },
};

void pix_line(u8 row){
    gpio_set_value(A1, !(row & 0b00000001));
    gpio_set_value(A2, !(row & 0b00000010));
    gpio_set_value(A3, !(row & 0b00000100));
}

void pix_dot(int x, int y, int r, int g, int b) {
    uint8_t l,p,t;

    if(y % 2){
        x = (x + 16);
    }
    y = (y / 2) - ((y / 2) % 1);

    l = x / 8;
    p = 7 - x % 8;
    t = pix_screen[y][l];

    //printf("line:%i, l:%i, p:%i, t:%i\n", y,l,p,t);

    if(r) {
        t |= 1 << p;
    }else{
        t &= ~(1 << p);
    }
    pix_screen[y][l] = t;

    l = x / 8 + 4;
    p = 7 - x % 8;
    t = pix_screen[y][l];
    if(g) {
        t |= 1 << p;
    }else{
        t &= ~(1 << p);
    }
    pix_screen[y][l] = t;

    l = x / 8 + 8;
    p = 7 - x % 8;
    t = pix_screen[y][l];
    if(b) {
        t |= 1 << p;
    }else{
        t &= ~(1 << p);
    }
    pix_screen[y][l] = t;
}

void pix_gpio_init(void){
    printk(KERN_INFO "PIX: starting gpio...");
    gpio_request(A1, "A1");
    gpio_request(A2, "A2");
    gpio_request(A3, "A3");

    gpio_request(OE, "OE");
    gpio_request(LE, "LE");
    gpio_request(SDI, "SDI");
    gpio_request(CLK, "CLK");

    gpio_direction_output(A1, 0);
    gpio_direction_output(A2, 0);
    gpio_direction_output(A3, 0);

    gpio_direction_output(OE, 1);
    gpio_direction_output(LE, 0);
    gpio_direction_output(SDI, 0);
    gpio_direction_output(CLK, 0);
    printk(KERN_INFO "PIX: starting gpio done.");
}

void pix_gpio_exit(void){
    printk(KERN_INFO "PIX: stopping gpio...");
    gpio_free(A1);
    gpio_free(A2);
    gpio_free(A3);

    gpio_free(OE);
    gpio_free(LE);
    gpio_free(SDI);
    gpio_free(CLK);
    printk(KERN_INFO "PIX: stopping gpio done.");
}



/* SYSFS */

static struct kobject *pix_kobject;

static ssize_t set_pix(struct kobject *kobj, struct kobj_attribute *attr, const char *buff, size_t count) {
    u8 x = 0;
    u8 y = 0;
    u8 r = 0;
    u8 g = 0;
    u8 b = 0;
    sscanf(buff, "%hhd %hhd %hhd %hhd %hhd", &x, &y, &r, &g, &b);

    pix_dot(x,y,r,g,b);
    // printk(KERN_INFO "PIX: %d %d %d %d %d", x,y,r,g,b);

    return count;
}

static struct kobj_attribute pix_attribute =__ATTR(dot, (S_IWUSR | S_IRUGO), NULL, set_pix);

void pix_sysfs_init(void){
    printk(KERN_INFO "PIX: starting sysfs...");
    pix_kobject = kobject_create_and_add("pix", NULL);
    if (sysfs_create_file(pix_kobject, &pix_attribute.attr)) {
        pr_debug("failed to create pix sysfs!\n");
    }
    printk(KERN_INFO "PIX: starting sysfs done.");
}

void pix_sysfs_exit(void){
    printk(KERN_INFO "PIX: stopping sysfs...");
    kobject_put(pix_kobject);
    printk(KERN_INFO "PIX: stopping sysfs done.");
}


/* THREAD */

#define THREAD_PRIORITY 45
#define THREAD_NAME "pix"

struct task_struct *task;

int pix_thread(void *data){
    u8 line, pos, bit;
    struct task_struct *TSK;
    struct sched_param PARAM = { .sched_priority = MAX_RT_PRIO - 50 };
    //struct sched_param PARAM = { .sched_priority = DEFAULT_PRIO };
    TSK = current;

    PARAM.sched_priority = THREAD_PRIORITY;
    sched_setscheduler(TSK, SCHED_FIFO, &PARAM);

    while(1) {
        for(line = 0; line < LINES; line++) {
            pix_line(line);
            for(pos = 0; pos < PER_LINE; pos++) {
                for (bit = 0; bit < 8; bit++)  {
                    gpio_set_value(SDI, !!(pix_screen[line][pos] & (1 << (7 - bit))));
                    gpio_set_value(CLK, 1);
                    gpio_set_value(CLK, 0);
                }
            }
            gpio_set_value(LE, 1);
            gpio_set_value(LE, 0);
            gpio_set_value(OE, 0);
            usleep_range(2000, 2000);
            gpio_set_value(OE, 1);
        }
        if (kthread_should_stop()) break;
    }
    return 0;
}

void pix_thread_init(void){
    printk(KERN_INFO "PIX: starting thread...");
    task = kthread_run(pix_thread, NULL, THREAD_NAME);
    printk(KERN_INFO "PIX: starting thread done.");
}

void pix_thread_exit(void){
    printk(KERN_INFO "PIX: stopping thread...");
    kthread_stop(task);
    printk(KERN_INFO "PIX: stopping thread done.");
}

/* MODULE */

static int __init pix_init(void){
    printk(KERN_INFO "PIX: staring...");
    pix_gpio_init();
    pix_thread_init();
    pix_sysfs_init();
    printk(KERN_INFO "PIX: staring done.");
    return 0;
}

static void __exit pix_exit(void){
    printk(KERN_INFO "PIX: stopping...");
    pix_sysfs_exit();
    pix_thread_exit();
    pix_gpio_exit();
    printk(KERN_INFO "PIX: stopping done.");
}

module_init(pix_init);
module_exit(pix_exit);