logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

shift_register.c (2444B)


  1. /* Copyright 2023 ArthurCyy <https://github.com/ArthurCyy>
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "shift_register.h"
  17. #include <string.h>
  18. static void shift_out(void);
  19. static uint8_t shift_values[SHR_SERIES_NUM] = {0};
  20. void shift_init(void) {
  21. #ifdef SHR_OE_PIN
  22. gpio_set_pin_output(SHR_OE_PIN);
  23. gpio_write_pin_high(SHR_OE_PIN);
  24. #endif
  25. gpio_set_pin_output(SHR_DATA_PIN);
  26. gpio_set_pin_output(SHR_LATCH_PIN);
  27. gpio_set_pin_output(SHR_CLOCK_PIN);
  28. }
  29. void shift_enable(void) {
  30. #ifdef SHR_OE_PIN
  31. gpio_write_pin_low(SHR_OE_PIN);
  32. #endif
  33. gpio_write_pin_low(SHR_DATA_PIN);
  34. gpio_write_pin_low(SHR_LATCH_PIN);
  35. gpio_write_pin_low(SHR_CLOCK_PIN);
  36. }
  37. void shift_disable(void) {
  38. #ifdef SHR_OE_PIN
  39. gpio_write_pin_high(SHR_OE_PIN);
  40. #endif
  41. gpio_write_pin_low(SHR_DATA_PIN);
  42. gpio_write_pin_low(SHR_LATCH_PIN);
  43. gpio_write_pin_low(SHR_CLOCK_PIN);
  44. }
  45. void shift_writePin(pin_t pin, int level) {
  46. uint8_t group = (pin - H0) >> 3;
  47. uint8_t bit = 0x01 << ((pin - H0)&0x07);
  48. if(group >= SHR_SERIES_NUM)
  49. return;
  50. if(level)
  51. shift_values[group] |= bit;
  52. else
  53. shift_values[group] &= ~bit;
  54. shift_out();
  55. }
  56. void shift_writeGroup(int group, uint8_t value) {
  57. if(group >= SHR_SERIES_NUM)
  58. return;
  59. shift_values[group] = value;
  60. shift_out();
  61. }
  62. void shift_writeAll(int level) {
  63. memset(shift_values, level ? 0xFF : 0, sizeof(shift_values));
  64. shift_out();
  65. }
  66. static void shift_out(void) {
  67. uint8_t n = SHR_SERIES_NUM;
  68. gpio_write_pin_low(SHR_LATCH_PIN);
  69. while(n--){
  70. for (uint8_t i = 0; i < 8; i++) {
  71. gpio_write_pin_low(SHR_CLOCK_PIN);
  72. gpio_write_pin(SHR_DATA_PIN, shift_values[n] & (0x80 >> i));
  73. gpio_write_pin_high(SHR_CLOCK_PIN);
  74. }
  75. }
  76. gpio_write_pin_high(SHR_LATCH_PIN);
  77. }