Simple single-color 8x8x8 LED Cube with AVRs
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.

eq.c 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * eq.c
  3. *
  4. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  5. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  6. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  7. *
  8. * This file is part of LED-Cube.
  9. *
  10. * LED-Cube is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * LED-Cube is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #include <avr/io.h>
  24. #include <stdint.h>
  25. #include <util/delay.h>
  26. #include "eq.h"
  27. #include "adc.h"
  28. #define Trs 72
  29. #define Ts 19
  30. #define To 36
  31. uint8_t result[7] = {128, 128, 128, 128, 128, 128, 128};
  32. #define RESETPORT PORTC
  33. #define STROBEPORT PORTC
  34. #define RESET PC3
  35. #define STROBE PC2
  36. #define RESETON RESETPORT |= (1 << RESET)
  37. #define RESETOFF RESETPORT &= ~(1 << RESET)
  38. #define STROBEON STROBEPORT |= (1 << STROBE)
  39. #define STROBEOFF STROBEPORT &= ~(1 << STROBE)
  40. void equalizerInit(void);
  41. uint8_t *equalizerGet(void);
  42. void calcMultiplicator(uint8_t *d);
  43. void filterNoise(uint8_t *data);
  44. uint8_t getOffset(void);
  45. void equalizerInit(void) {
  46. RESETON;
  47. STROBEOFF;
  48. }
  49. uint8_t *equalizerGet(void) {
  50. uint8_t i;
  51. RESETOFF;
  52. _delay_us(Trs);
  53. for (i = 0; i < 7; i++) {
  54. STROBEON;
  55. _delay_us(Ts);
  56. STROBEOFF;
  57. _delay_us(To); // Wait for result
  58. adcStartConversion(0);
  59. result[i] = adcGetByte();
  60. }
  61. RESETON;
  62. asm volatile ("nop"); // Ensure minimal reset pulse width
  63. asm volatile ("nop"); // 2 NOPs at 16MHz are enough...
  64. filterNoise(result);
  65. calcMultiplicator(result);
  66. return result;
  67. }
  68. void calcMultiplicator(uint8_t *d) {
  69. uint8_t i;
  70. uint16_t multiplicator = ((uint16_t)getOffset() + 100) / 10;
  71. for (i = 0; i < 7; i++) {
  72. d[i] = (d[i] * multiplicator) / 10;
  73. }
  74. }
  75. void filterNoise(uint8_t *data) {
  76. uint8_t i;
  77. for (i = 0; i < 7; i++) {
  78. if (data[i] <= 40) {
  79. data[i] = 0;
  80. }
  81. }
  82. }
  83. uint8_t getOffset(void) {
  84. adcStartConversion(0x01);
  85. return adcGetByte();
  86. }