diff options
author | Erich Eckner <git@eckner.net> | 2018-10-22 14:19:25 +0200 |
---|---|---|
committer | Erich Eckner <git@eckner.net> | 2018-10-22 14:19:25 +0200 |
commit | ae75b15e6241232c2adb3ae3b53819d3a4dd638d (patch) | |
tree | 4a4c738c2bfa8fff0ac2dbe0a90444120bb566c5 /multiplexer.c | |
parent | 9cbc55a81c76dd3bfb480b9f280f9958ce6ef592 (diff) | |
download | anzeige-ae75b15e6241232c2adb3ae3b53819d3a4dd638d.tar.xz |
split of logical parts into separate files
Diffstat (limited to 'multiplexer.c')
-rw-r--r-- | multiplexer.c | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/multiplexer.c b/multiplexer.c new file mode 100644 index 0000000..5f82ee5 --- /dev/null +++ b/multiplexer.c @@ -0,0 +1,128 @@ +#include <stdio.h> +#include <stdlib.h> +#include <fcntl.h> +#include <sys/mman.h> +#include <unistd.h> +#include <errno.h> +#include <string.h> +#include <pthread.h> + +#include "multiplexer.h" + +/** TODO +void printButton(int g) +{ + if (GET_GPIO(g)) // !=0 <-> bit is 1 <- port is HIGH=3.3V + printf("Button pressed!\n"); + else // port is LOW=0V + printf("Button released!\n"); +} **/ + + +// +// Set up a memory regions to access GPIO +// +void multiplexer_setup_root() +{ +#ifndef SKIP_GPIO + /* open /dev/mem */ + if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { + perror("can't open /dev/mem"); + exit(-1); + } + + /* mmap GPIO */ + gpio_map = mmap( + NULL, //Any adddress in our space will do + BLOCK_SIZE, //Map length + PROT_READ|PROT_WRITE,// Enable reading & writting to mapped memory + MAP_SHARED, //Shared with other processes + mem_fd, //File to map + GPIO_BASE //Offset to GPIO peripheral + ); + + close(mem_fd); //No need to keep mem_fd open after mmap + + if (gpio_map == MAP_FAILED) { + perror("mmap error"); + exit(-1); + } + + // Always use volatile pointer! + gpio = (volatile unsigned *)gpio_map; +#endif +} // multiplexer_setup_root + +// +// Shift the bits provided in content into the display +// +void *put_on_display(void *param) +{ + t_display_data *display_data = param; + int line, column; + + while (display_data -> keep_running) { + +#ifdef SKIP_GPIO + usleep(100000); + printf("=\n"); +#endif + display_data -> is_buf = display_data -> should_buf; + for (line=0; line<7; line++) { +#ifdef SKIP_GPIO + for (column=0; column<40; column++) { + if ((*(display_data -> buf[display_data -> is_buf] + column)>>line) & 0x01) + printf("X"); + else + printf("."); +#else + GPIO_CLR = 1<<GATE_PIN; // Licht an + for (column=0; column<8; column++) { + GPIO_CLR = 1<<SER_CLK_PIN; + GPIO_ALTER(column == line) = 1<<SER_DAT_PIN; + GPIO_SET = 1<<SER_CLK_PIN; + } + for (column=39; column>=0; column--) { + GPIO_CLR = 1<<SER_CLK_PIN; + GPIO_ALTER((*(display_data -> buf[display_data -> is_buf] + column)>>line) & 0x01) = 1<<SER_DAT_PIN; + GPIO_SET = 1<<SER_CLK_PIN; +#endif + } + usleep(1000); +#ifdef SKIP_GPIO + printf("\n"); +#else + GPIO_SET = 1<<GATE_PIN; // Licht aus + GPIO_CLR = 1<<PAR_CLK_PIN; + GPIO_SET = 1<<PAR_CLK_PIN; +#endif + } + } + return NULL; +} // put_on_display + +pthread_t multiplexer_setup_non_root(t_display_data *display_data) +{ + pthread_t thread_id; + +#ifndef SKIP_GPIO + // must use INP_GPIO before we can use OUT_GPIO + INP_GPIO(SER_DAT_PIN); + OUT_GPIO(SER_DAT_PIN); + INP_GPIO(SER_CLK_PIN); + OUT_GPIO(SER_CLK_PIN); + INP_GPIO(GATE_PIN); + OUT_GPIO(GATE_PIN); + INP_GPIO(PAR_CLK_PIN); + OUT_GPIO(PAR_CLK_PIN); + INP_GPIO(SENSE_PIN); +#endif + + for (int i=0; i<3; i++) + memset(display_data -> buf[i],0,40); + display_data -> is_buf = 0; + display_data -> should_buf = 0; + display_data -> keep_running = 1; + pthread_create(&thread_id, NULL, put_on_display, display_data); + return thread_id; +} // multiplexer_setup_non_root |