1 /* 2 * Keyboard scanning task. 3 * 4 * Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "board.h" 21 #include "tasks.h" 22 #include "task_gpio.h" 23 #include "jzlcd.h" 24 #include "lcd.h" 25 26 extern vidinfo_t panel_info; 27 28 void plot_value(uint16_t xmin, uint16_t xmax, uint16_t ymin, uint16_t ymax, uint8_t value) 29 { 30 uint16_t x, y; 31 uint32_t pixel; 32 33 for (y = ymin; (y < panel_info.jz_fb->h) && (y < ymax); y++) 34 { 35 pixel = (value >> (((y - ymin) * 8) / (ymax - ymin))) % 2 ? 0xffffffff : 0; 36 37 for (x = xmin; (x < panel_info.jz_fb->w) && (x < xmax); x++) 38 { 39 set_pixel(x, y, pixel); 40 } 41 } 42 } 43 44 void init_keyscan() 45 { 46 uint8_t pin; 47 48 for (pin = 0; pin < GPIO_KEYIN_COUNT; pin++) 49 { 50 task_gpio_as_input(GPIO_KEYIN_ROW[pin]); 51 task_gpio_enable_pull(GPIO_KEYIN_ROW[pin]); 52 } 53 54 for (pin = 0; pin < GPIO_KEYOUT_COUNT; pin++) 55 { 56 task_gpio_as_input(GPIO_KEYOUT_COL[pin]); 57 } 58 59 task_gpio_as_input(gpio_global_pin(GPIO_PORT_POWER, GPIO_POWER)); 60 } 61 62 /* Tasks. */ 63 64 void keyscan(uint8_t task) 65 { 66 uint8_t column, row, value; 67 68 init_keyscan(); 69 70 while (1) 71 { 72 /* Select each column and read the key states. */ 73 74 for (column = 0; column < GPIO_KEYOUT_COUNT; column++) 75 { 76 task_gpio_as_output(GPIO_KEYOUT_COL[column]); 77 task_gpio_clear_pin(GPIO_KEYOUT_COL[column]); 78 79 /* Employ a delay to avoid catching the last column's reading. */ 80 81 for (unsigned int delay = 0; delay < 1000; delay++) 82 83 value = 0; 84 85 for (row = 0; row < GPIO_KEYIN_COUNT; row++) 86 { 87 value = (value << 1) | (task_gpio_get_pin(GPIO_KEYIN_ROW[row]) ? 0 : 1); 88 } 89 90 /* Perform an operation indicating the status. */ 91 92 plot_value(column * 10, column * 10 + 10, 0, 10 * GPIO_KEYIN_COUNT, value); 93 94 task_gpio_as_input(GPIO_KEYOUT_COL[column]); 95 } 96 97 plot_value(100, 110, 0, 80, task_gpio_get_pin(gpio_global_pin(GPIO_PORT_POWER, GPIO_POWER)) ? 0xff : 0); 98 } 99 } 100 101 void start_keyscan(uint8_t task) 102 { 103 uint32_t args[] = {task}; 104 105 task_gpio_init(task); 106 107 /* Invoke the task. */ 108 109 start_task(task, (void (*)()) keyscan, args, 1); 110 }