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.vl_row) && (y < ymax); y++) 34 { 35 pixel = (value >> (((y - ymin) * 8) / (ymax - ymin))) % 2 ? 0xffffffff : 0; 36 37 for (x = xmin; (x < panel_info.vl_col) && (x < xmax); x++) 38 { 39 set_pixel(x, y, pixel); 40 } 41 } 42 } 43 44 void init_keyscan() 45 { 46 int column; 47 48 for (column = 0; column < GPIO_KEYOUT_COUNT; column++) 49 { 50 task_gpio_set_pin(GPIO_KEYOUT_BASE + column); 51 } 52 } 53 54 /* Tasks. */ 55 56 void keyscan(uint8_t task) 57 { 58 int column, row; 59 uint8_t value; 60 61 init_keyscan(); 62 63 while (1) 64 { 65 /* Select each column and read the key states. */ 66 67 for (column = 0; column < GPIO_KEYOUT_COUNT; column++) 68 { 69 task_gpio_clear_pin(GPIO_KEYOUT_BASE + column); 70 71 value = 0; 72 73 for (row = 0; row < GPIO_KEYIN_COUNT; row++) 74 { 75 value = (value << 1) | !task_gpio_get_pin(GPIO_KEYIN_BASE + row); 76 } 77 78 /* Perform an operation indicating the status. */ 79 80 plot_value(column * 10, column * 10 + 10, 0, 10 * GPIO_KEYIN_COUNT, value); 81 82 task_gpio_set_pin(GPIO_KEYOUT_BASE + column); 83 } 84 } 85 } 86 87 void start_keyscan(uint8_t task) 88 { 89 uint32_t args[] = {task}; 90 91 task_gpio_init(task); 92 93 /* Invoke the task. */ 94 95 start_task(task, (void (*)()) keyscan, args, 1); 96 }