Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.46 KB | None | 0 0
  1. /*
  2. Copyright 2012 Jun Wako
  3. Generated by planckkeyboard.com (2014 Jack Humbert)
  4.  
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. */
  18.  
  19. /*
  20.  * scan matrix
  21.  */
  22. #include <stdint.h>
  23. #include <stdbool.h>
  24. #include <avr/io.h>
  25. #include <util/delay.h>
  26. #include "print.h"
  27. #include "debug.h"
  28. #include "util.h"
  29. #include "matrix.h"
  30.  
  31.  
  32. #ifndef DEBOUNCE
  33. #   define DEBOUNCE 10
  34. #endif
  35. static uint8_t debouncing = DEBOUNCE;
  36.  
  37. /* matrix state(1:on, 0:off) */
  38. static matrix_row_t matrix[MATRIX_ROWS];
  39. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  40.  
  41. static matrix_row_t read_cols(void);
  42. static void init_cols(void);
  43. static void unselect_rows(void);
  44. static void select_row(uint8_t row);
  45.  
  46. inline
  47. uint8_t matrix_rows(void)
  48. {
  49.     return MATRIX_ROWS;
  50. }
  51.  
  52. inline
  53. uint8_t matrix_cols(void)
  54. {
  55.     return MATRIX_COLS;
  56. }
  57.  
  58. void matrix_init(void)
  59. {
  60.     // initialize row and col
  61.     unselect_rows();
  62.     init_cols();
  63.  
  64.     // initialize matrix state: all keys off
  65.     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  66.         matrix[i] = 0;
  67.         matrix_debouncing[i] = 0;
  68.     }
  69. }
  70.  
  71. uint8_t matrix_scan(void)
  72. {
  73.     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  74.         select_row(i);
  75.         _delay_us(30);  // without this wait read unstable value.
  76.         matrix_row_t cols = read_cols();
  77.         if (matrix_debouncing[i] != cols) {
  78.             matrix_debouncing[i] = cols;
  79.             if (debouncing) {
  80.                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
  81.             }
  82.             debouncing = DEBOUNCE;
  83.         }
  84.         unselect_rows();
  85.     }
  86.  
  87.     if (debouncing) {
  88.         if (--debouncing) {
  89.             _delay_ms(1);
  90.         } else {
  91.             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  92.                 matrix[i] = matrix_debouncing[i];
  93.             }
  94.         }
  95.     }
  96.  
  97.     return 1;
  98. }
  99.  
  100. bool matrix_is_modified(void)
  101. {
  102.     if (debouncing) return false;
  103.     return true;
  104. }
  105.  
  106. inline
  107. bool matrix_is_on(uint8_t row, uint8_t col)
  108. {
  109.     return (matrix[row] & ((matrix_row_t)1<col));
  110. }
  111.  
  112. inline
  113. matrix_row_t matrix_get_row(uint8_t row)
  114. {
  115.     return matrix[row];
  116. }
  117.  
  118. void matrix_print(void)
  119. {
  120.     print("\nr/c 0123456789ABCDEF\n");
  121.     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  122.         phex(row); print(": ");
  123.         pbin_reverse16(matrix_get_row(row));
  124.         print("\n");
  125.     }
  126. }
  127.  
  128. uint8_t matrix_key_count(void)
  129. {
  130.     uint8_t count = 0;
  131.     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  132.         count += bitpop16(matrix[i]);
  133.     }
  134.     return count;
  135. }
  136.  
  137. static void init_cols(void)
  138. {
  139.     DDRB &= ~(1<<7 | 1<<3 | 1<<2 | 1<<1 | 1<<0);
  140.     PORTB |= (1<<7 | 1<<3 | 1<<2 | 1<<1 | 1<<0);
  141.     DDRC &= ~(1<<7 | 1<<6);
  142.     PORTC |= (1<<7 | 1<<6);
  143.     DDRD &= ~(1<<5 | 1<<3 | 1<<2 | 1<<1 | 1<<0);
  144.     PORTD |= (1<<5 | 1<<3 | 1<<2 | 1<<1 | 1<<0);
  145.    
  146. }
  147.  
  148. static matrix_row_t read_cols(void)
  149. {
  150.   return (PINB&(1<<0) ? 0 : (1<<0)) |
  151.          (PINB&(1<<1) ? 0 : (1<<1)) |
  152.          (PINB&(1<<2) ? 0 : (1<<2)) |
  153.          (PINB&(1<<3) ? 0 : (1<<3)) |
  154.          (PINB&(1<<7) ? 0 : (1<<4)) |
  155.          (PIND&(1<<0) ? 0 : (1<<5)) |
  156.          (PIND&(1<<1) ? 0 : (1<<6)) |
  157.          (PIND&(1<<2) ? 0 : (1<<7)) |
  158.          (PIND&(1<<3) ? 0 : (1<<8)) |
  159.          (PINC&(1<<6) ? 0 : (1<<9)) |
  160.          (PINC&(1<<7) ? 0 : (1<<10)) |
  161.          (PIND&(1<<5) ? 0 : (1<<11));
  162.          
  163. }
  164.  
  165. static void unselect_rows(void)
  166. {
  167.     DDRF &= ~(1<<1 | 1<<4 | 1<<5 | 1<<6);
  168.     PORTF |= (1<<1 | 1<<4 | 1<<5 | 1<<6);
  169.    
  170. }
  171.  
  172. static void select_row(uint8_t row)
  173. {
  174.     switch (row) {
  175.         case 0:
  176.             DDRF  |= (1<<1);
  177.             PORTF &= ~(1<<1);
  178.             break;
  179.         case 1:
  180.             DDRF  |= (1<<4);
  181.             PORTF &= ~(1<<4);
  182.             break;
  183.         case 2:
  184.             DDRF  |= (1<<5);
  185.             PORTF &= ~(1<<5);
  186.             break;
  187.         case 3:
  188.             DDRF  |= (1<<6);
  189.             PORTF &= ~(1<<6);
  190.             break;
  191.        
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement