/firmware/src/Motherboard/boards/mb24/ButtonArray.cc

http://github.com/makerbot/G3Firmware · C++ · 76 lines · 60 code · 14 blank · 2 comment · 14 complexity · 8355c80f5b7f2ae1f2bc2b36a276db52 MD5 · raw file

  1. #include "ButtonArray.hh"
  2. static uint8_t previousL;
  3. static uint8_t previousC;
  4. void ButtonArray::init() {
  5. previousL = 0;
  6. previousC = 0;
  7. // Set all of the known buttons to inputs (see above note)
  8. DDRL = DDRL & 0x1;
  9. DDRC = DDRC & 0xF9;
  10. PORTL = PORTL & 0x1;
  11. PORTC = PORTC & 0xF9;
  12. }
  13. void ButtonArray::scanButtons() {
  14. // Don't bother scanning if we already have a button.
  15. if (buttonPressWaiting) {
  16. return;
  17. }
  18. uint8_t newL = PINL;// & 0xFE;
  19. uint8_t newC = PINC;// & 0x06;
  20. if (newL != previousL) {
  21. uint8_t diff = newL ^ previousL;
  22. for(uint8_t i = 1; i < 8; i++) {
  23. if (diff&(1<<i)) {
  24. if (!(newL&(1<<i))) {
  25. if (!buttonPressWaiting) {
  26. buttonPress = i;
  27. buttonPressWaiting = true;
  28. }
  29. }
  30. }
  31. }
  32. }
  33. if (newC != previousC) {
  34. uint8_t diff = newC ^ previousC;
  35. for(uint8_t i = 1; i < 3; i++) {
  36. if (diff&(1<<i)) {
  37. if (!(newC&(1<<i))) {
  38. if (!buttonPressWaiting) {
  39. buttonPress = i+10;
  40. buttonPressWaiting = true;
  41. }
  42. }
  43. }
  44. }
  45. }
  46. previousL = newL;
  47. previousC = newC;
  48. }
  49. bool ButtonArray::getButton(ButtonName& button) {
  50. bool buttonValid;
  51. uint8_t buttonNumber;
  52. ATOMIC_BLOCK(ATOMIC_FORCEON)
  53. {
  54. buttonValid = buttonPressWaiting;
  55. buttonNumber = buttonPress;
  56. buttonPressWaiting = false;
  57. }
  58. if (buttonValid) {
  59. button = (ButtonName)(buttonNumber);
  60. }
  61. return buttonValid;
  62. }