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