Control drones with a proper joystick
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cppm.h 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Combined-PPM signal generator
  3. * Copyright 2016 by Thomas Buck <xythobuz@xythobuz.de>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation, version 2.
  8. */
  9. #ifndef __CPPM_H__
  10. #define __CPPM_H__
  11. #include <stdint.h>
  12. #include <avr/interrupt.h>
  13. #define CPPM_OUTPUT_PIN 4
  14. #define CHANNEL_DEFAULT_VALUE 1500
  15. #define CHANNELS_MAX 12
  16. ISR(TIMER1_COMPA_vect);
  17. class CPPM {
  18. public:
  19. inline static CPPM& instance() {
  20. if (!inst) {
  21. inst = new CPPM();
  22. }
  23. return *inst;
  24. }
  25. void init(void);
  26. void copy(uint16_t* d);
  27. inline uint16_t getChannels() { return channels; }
  28. inline void setChannels(uint16_t c) {
  29. if (c > CHANNELS_MAX)
  30. c = CHANNELS_MAX;
  31. channels = c;
  32. }
  33. inline uint16_t getFrameLength() { return frameLength; }
  34. inline void setFrameLength(uint16_t fl) { frameLength = fl; }
  35. inline uint16_t getPulseLength() { return pulseLength; }
  36. inline void setPulseLength(uint16_t pl) { pulseLength = pl; }
  37. inline uint8_t getInvert() { return !onState; }
  38. inline void setInvert(uint8_t i) { onState = !i; }
  39. private:
  40. CPPM() : channels(6), frameLength(20000), pulseLength(300), onState(1) { }
  41. CPPM(CPPM&) { }
  42. volatile uint16_t channels;
  43. volatile uint16_t frameLength; // PPM frame length in microseconds (1ms = 1000µs)
  44. volatile uint16_t pulseLength;
  45. volatile uint8_t onState; // polarity of the pulses: 1 is positive, 0 is negative
  46. volatile uint16_t data[CHANNELS_MAX];
  47. volatile uint8_t state;
  48. volatile uint8_t currentChannel;
  49. volatile uint16_t calcRest;
  50. static CPPM* inst;
  51. friend void TIMER1_COMPA_vect(void);
  52. };
  53. #endif // __CPPM_H__