Mercurial > templog
comparison py/touch/touch.c @ 455:e12d6d677320
Try some other ways with touch sensing. Linux scheduling isn't fast enough
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 03 Jan 2013 20:11:54 +0800 |
parents | dce9f7841696 |
children |
comparison
equal
deleted
inserted
replaced
454:dce9f7841696 | 455:e12d6d677320 |
---|---|
10 | 10 |
11 #define BCM2708_PERI_BASE 0x20000000 | 11 #define BCM2708_PERI_BASE 0x20000000 |
12 #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */ | 12 #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */ |
13 | 13 |
14 | 14 |
15 // for usleep | |
16 #define _BSD_SOURCE | |
17 | |
18 // clock_gettime | |
19 #define _POSIX_C_SOURCE 199309L | |
20 | |
15 #include <stdio.h> | 21 #include <stdio.h> |
16 #include <string.h> | 22 #include <string.h> |
17 #include <stdlib.h> | 23 #include <stdlib.h> |
24 #include <stdint.h> | |
18 #include <dirent.h> | 25 #include <dirent.h> |
19 #include <fcntl.h> | 26 #include <fcntl.h> |
20 #include <assert.h> | 27 #include <assert.h> |
21 #include <sys/mman.h> | 28 #include <sys/mman.h> |
22 #include <sys/types.h> | 29 #include <sys/types.h> |
23 #include <sys/stat.h> | 30 #include <sys/stat.h> |
31 #include <time.h> | |
32 #include <sched.h> | |
24 | 33 |
25 #include <unistd.h> | 34 #include <unistd.h> |
26 | 35 |
27 #define PAGE_SIZE (4*1024) | 36 #define PAGE_SIZE (4*1024) |
28 #define BLOCK_SIZE (4*1024) | 37 #define BLOCK_SIZE (4*1024) |
61 #define TOUCH_IN 24 | 70 #define TOUCH_IN 24 |
62 #define TOUCH_OUT 25 | 71 #define TOUCH_OUT 25 |
63 | 72 |
64 void setup_io(); | 73 void setup_io(); |
65 | 74 |
75 static int cmp_int(const void *a, const void *b) | |
76 { | |
77 const int *ia = a; | |
78 const int *ib = b; | |
79 return (*ia - *ib); | |
80 } | |
81 | |
82 static int clock_diff(struct timespec *t1, struct timespec *t2) | |
83 { | |
84 uint64_t v1 = t1->tv_sec * 1000000000 + t1->tv_nsec; | |
85 uint64_t v2 = t2->tv_sec * 1000000000 + t2->tv_nsec; | |
86 return v2-v1; | |
87 } | |
88 | |
66 int main(int argc, char **argv) | 89 int main(int argc, char **argv) |
67 { | 90 { |
68 int g,rep; | 91 // Set up gpi pointer for direct register access |
69 | 92 setup_io(); |
70 // Set up gpi pointer for direct register access | |
71 setup_io(); | |
72 | |
73 // Switch GPIO 7..11 to output mode | |
74 | |
75 /************************************************************************\ | |
76 * You are about to change the GPIO settings of your computer. * | |
77 * Mess this up and it will stop working! * | |
78 * It might be a good idea to 'sync' before running this program * | |
79 * so at least you still have your code changes written to the SD-card! * | |
80 \************************************************************************/ | |
81 | |
82 | 93 |
83 INP_GPIO(TOUCH_IN); | 94 INP_GPIO(TOUCH_IN); |
84 INP_GPIO(TOUCH_OUT); | 95 INP_GPIO(TOUCH_OUT); |
85 OUT_GPIO(TOUCH_OUT); | 96 OUT_GPIO(TOUCH_OUT); |
86 | 97 |
87 int num = 1000; | 98 const int num = 200; |
88 int sum = 0; | 99 const int margin = 30; |
100 int print = 0; | |
101 if (argc > 1) | |
102 { | |
103 print = 1; | |
104 } | |
89 | 105 |
90 while (1) | 106 while (1) |
91 { | 107 { |
92 int n; | 108 int vals[num]; |
93 sum = 0; | 109 int nsecs[num]; |
94 for (n = 0; n < num; n++) | 110 for (int n = 0; n < num; n++) |
95 { | 111 { |
96 GPIO_CLR(TOUCH_OUT); | 112 GPIO_CLR(TOUCH_OUT); |
97 usleep(1000); | 113 struct timespec t = {.tv_nsec=20}; |
114 nanosleep(&t, NULL); | |
115 //sched_yield(); | |
116 //usleep(1); | |
117 if (GPIO_GET(TOUCH_IN)) | |
118 { | |
119 printf("short "); | |
120 } | |
98 GPIO_SET(TOUCH_OUT); | 121 GPIO_SET(TOUCH_OUT); |
122 int val = 0; | |
99 while (GPIO_GET(TOUCH_IN) == 0) | 123 while (GPIO_GET(TOUCH_IN) == 0) |
100 { | 124 { |
101 sum++; | 125 val++; |
102 } | 126 } |
127 | |
128 vals[n] = val; | |
103 } | 129 } |
104 printf("total %f\n", (float)sum / num); | 130 qsort(vals, num, sizeof(*vals), cmp_int); |
105 } | 131 int sum = 0, count = 0; |
132 for (int n = 0; n < num; n++) | |
133 { | |
134 if (print) | |
135 { | |
136 printf("%3d ", vals[n]); | |
137 if (n == num-1 || n % 10 == 9) | |
138 { | |
139 printf("\n"); | |
140 } | |
141 if (n == margin || n == num-margin) | |
142 { | |
143 printf("#"); | |
144 } | |
145 } | |
146 if (n >= margin && n < (num-margin)) | |
147 { | |
148 sum += vals[n]; | |
149 count ++; | |
150 } | |
151 } | |
152 printf("total %f from %d\n", (float)sum / count, count); | |
153 usleep(500000); | |
154 } | |
106 return 0; | 155 return 0; |
107 | 156 |
108 } // main | 157 } // main |
109 | 158 |
110 | 159 |
111 // | 160 // |
112 // Set up a memory regions to access GPIO | 161 // Set up a memory regions to access GPIO |
113 // | 162 // |
114 void setup_io() | 163 void setup_io() |
115 { | 164 { |
116 /* open /dev/mem */ | 165 /* open /dev/mem */ |
117 if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { | 166 if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { |
118 printf("can't open /dev/mem \n"); | 167 printf("can't open /dev/mem \n"); |
119 exit (-1); | 168 exit (-1); |
120 } | 169 } |
121 | 170 |
122 /* mmap GPIO */ | 171 /* mmap GPIO */ |
123 | 172 |
124 // Allocate MAP block | 173 // Allocate MAP block |
125 if ((gpio_mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) { | 174 if ((gpio_mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) { |
126 printf("allocation error \n"); | 175 printf("allocation error \n"); |
127 exit (-1); | 176 exit (-1); |
128 } | 177 } |
129 | 178 |
130 // Make sure pointer is on 4K boundary | 179 // Make sure pointer is on 4K boundary |
131 if ((unsigned long)gpio_mem % PAGE_SIZE) | 180 if ((unsigned long)gpio_mem % PAGE_SIZE) |
132 gpio_mem += PAGE_SIZE - ((unsigned long)gpio_mem % PAGE_SIZE); | 181 gpio_mem += PAGE_SIZE - ((unsigned long)gpio_mem % PAGE_SIZE); |
133 | 182 |
134 // Now map it | 183 // Now map it |
135 gpio_map = (unsigned char *)mmap( | 184 gpio_map = mmap( |
136 (caddr_t)gpio_mem, | 185 (caddr_t)gpio_mem, |
137 BLOCK_SIZE, | 186 BLOCK_SIZE, |
138 PROT_READ|PROT_WRITE, | 187 PROT_READ|PROT_WRITE, |
139 MAP_SHARED|MAP_FIXED, | 188 MAP_SHARED|MAP_FIXED, |
140 mem_fd, | 189 mem_fd, |
141 GPIO_BASE | 190 GPIO_BASE |
142 ); | 191 ); |
143 | 192 |
144 if ((long)gpio_map < 0) { | 193 if ((long)gpio_map < 0) { |
145 printf("mmap error %d\n", (int)gpio_map); | 194 printf("mmap error %d\n", (int)gpio_map); |
146 exit (-1); | 195 exit (-1); |
147 } | 196 } |
148 | 197 |
149 // Always use volatile pointer! | 198 // Always use volatile pointer! |
150 gpio = (volatile unsigned *)gpio_map; | 199 gpio = (volatile unsigned *)gpio_map; |
151 | 200 |
152 | 201 |
153 } // setup_io | 202 } // setup_io |