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.

vibration.c 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. Copyright 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 "timer.h"
  16. #include "vibration.h"
  17. static uint8_t vibration_state = 0;
  18. static uint16_t vibration_start;
  19. static uint16_t vibration_duration;
  20. void vibration_task(void)
  21. {
  22. if (vibration_state) {
  23. if (timer_elapsed(vibration_start) > vibration_duration) {
  24. vibration_state = 0;
  25. vibration_disable();
  26. }
  27. }
  28. }
  29. void vibration_init(void)
  30. {
  31. #ifndef EXPERIMENTAL
  32. DDRD |= (1<<PD6);
  33. PORTD &= ~(1<<PD6);
  34. #else
  35. DDRD |= (1<<PD5);
  36. PORTD &= ~(1<<PD5);
  37. #endif
  38. }
  39. void vibration_enable(void)
  40. {
  41. #ifndef EXPERIMENTAL
  42. PORTD |= (1<<PD6);
  43. #else
  44. PORTD |= (1<<PD5);
  45. #endif
  46. }
  47. void vibration_disable(void)
  48. {
  49. #ifndef EXPERIMENTAL
  50. PORTD &= ~(1<<PD6);
  51. #else
  52. PORTD &= ~(1<<PD5);
  53. #endif
  54. }
  55. void vibration(uint16_t duration)
  56. {
  57. vibration_state = 1;
  58. vibration_start = timer_read();
  59. vibration_duration = duration;
  60. vibration_enable();
  61. }