You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

led_matrix.c 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright 2013,2014 Kai Ryu <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <avr/io.h>
  15. #include <avr/interrupt.h>
  16. #include <util/delay.h>
  17. #include "led_matrix.h"
  18. #define LED_MATRIX_TIMER_TOP F_CPU/(256*64)/LED_MATRIX_ROWS
  19. static led_matrix_element_t led_matrix[LED_MATRIX_ROWS][LED_MATRIX_COLS];
  20. void led_matrix_init(void)
  21. {
  22. led_matrix_unselect_rows();
  23. led_matrix_write_cols(0);
  24. /* Timer1 setup */
  25. /* CTC mode */
  26. TCCR1B |= (1<<WGM12);
  27. /* Clock selelct: clk/1 */
  28. TCCR1B |= (1<<CS10);
  29. /* Set TOP value */
  30. uint8_t sreg = SREG;
  31. cli();
  32. OCR1AH = (LED_MATRIX_TIMER_TOP >> 8) & 0xFF;
  33. OCR1AL = LED_MATRIX_TIMER_TOP & 0xFF;
  34. SREG = sreg;
  35. }
  36. void led_matrix_enable(void)
  37. {
  38. /* Enable Compare Match Interrupt */
  39. TIMSK1 |= _BV(OCIE1A);
  40. }
  41. void led_matrix_disable(void)
  42. {
  43. /* Disable Compare Match Interrupt */
  44. TIMSK1 &= ~_BV(OCIE1A);
  45. }
  46. inline
  47. led_matrix_row_t led_matrix_make_cols(uint8_t row, uint8_t pwm)
  48. {
  49. led_matrix_row_t cols = 0;
  50. for (uint8_t i = 0; i < LED_MATRIX_COLS; i++) {
  51. cols |= ((led_matrix[row][i].value < pwm ? 1 : 0) << i);
  52. }
  53. return cols;
  54. }
  55. inline
  56. void led_matrix_set_value(uint8_t row, uint8_t col, uint8_t value)
  57. {
  58. led_matrix[row][col].value = value;
  59. }
  60. inline
  61. void led_matrix_set_delta(uint8_t row, uint8_t col, int8_t delta)
  62. {
  63. led_matrix[row][col].delta = delta;
  64. }
  65. ISR(TIMER1_COMPA_vect)
  66. {
  67. static uint8_t row = 0;
  68. static uint8_t pwm = 0;
  69. row = (row + 1) % LED_MATRIX_ROWS;
  70. pwm++;
  71. led_matrix_select_row(row);
  72. _delay_us(10);
  73. led_matrix_write_cols(led_matrix_make_cols(row, pwm));
  74. _delay_us(10);
  75. led_matrix_unselect_rows();
  76. }