Control drones with a proper joystick
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

config.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * EEPROM configuration storage utility
  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. #include <EEPROM.h>
  10. #include "cppm.h"
  11. #include "events.h"
  12. #include "config.h"
  13. //#define DEBUG_OUTPUT Serial
  14. static void toEEPROM(const ConfigData& data) {
  15. for (uint8_t i = 0; i < CONFIG_VERSION_LENGTH; i++) {
  16. EEPROM.write(i, CONFIG_VERSION[i]);
  17. }
  18. const uint8_t* buffer = (const uint8_t*)((const void*)&data);
  19. uint8_t sum = 0;
  20. for (uint8_t i = 0; i < CONFIG_DATA_LENGTH; i++) {
  21. EEPROM.write(CONFIG_VERSION_LENGTH + i, buffer[i]);
  22. sum ^= buffer[i];
  23. }
  24. EEPROM.write(CONFIG_VERSION_LENGTH + CONFIG_DATA_LENGTH, sum);
  25. #ifdef DEBUG_OUTPUT
  26. DEBUG_OUTPUT.println("EEPROM write success!");
  27. #endif
  28. }
  29. // Return 0 on valid data, otherwise header or checksum wrong
  30. static uint8_t fromEEPROM(ConfigData& data) {
  31. for (uint8_t i = 0; i < CONFIG_VERSION_LENGTH; i++) {
  32. if (EEPROM.read(i) != CONFIG_VERSION[i]) {
  33. #ifdef DEBUG_OUTPUT
  34. DEBUG_OUTPUT.println("EEPROM header wrong!");
  35. #endif
  36. return 1;
  37. }
  38. }
  39. uint8_t* buffer = (uint8_t*)((void*)&data);
  40. uint8_t sum = 0;
  41. for (uint8_t i = 0; i < CONFIG_DATA_LENGTH; i++) {
  42. buffer[i] = EEPROM.read(CONFIG_VERSION_LENGTH + i);
  43. sum ^= buffer[i];
  44. }
  45. uint8_t storedSum = EEPROM.read(CONFIG_VERSION_LENGTH + CONFIG_DATA_LENGTH);
  46. if (storedSum != sum) {
  47. #ifdef DEBUG_OUTPUT
  48. DEBUG_OUTPUT.println("EEPROM checksum wrong!");
  49. #endif
  50. return 2;
  51. }
  52. #ifdef DEBUG_OUTPUT
  53. DEBUG_OUTPUT.println("EEPROM read success!");
  54. #endif
  55. return 0;
  56. }
  57. void eepromRead() {
  58. ConfigData data;
  59. if (fromEEPROM(data) != 0) {
  60. data.channels = DEFAULT_CHANNELS;
  61. data.frameLength = DEFAULT_FRAME_LENGTH;
  62. data.pulseLength = DEFAULT_PULSE_LENGTH;
  63. data.inverted = DEFAULT_INVERT_STATE;
  64. data.cppmPin = CPPM_OUTPUT_PIN_DEFAULT;
  65. for (uint8_t i = 0; i < CHANNELS_MAX; i++) {
  66. data.invert[i] = 0;
  67. data.minimum[i] = CHANNEL_MINIMUM_VALUE;
  68. data.maximum[i] = CHANNEL_MAXIMUM_VALUE;
  69. data.trim[i] = 0;
  70. }
  71. // Should be correct for every device
  72. data.invert[CHANNEL_THROTTLE] = 1;
  73. data.invert[CHANNEL_PITCH] = 1;
  74. /*
  75. * Default values to match my personal setup.
  76. * Can be changed using the on-screen menu.
  77. */
  78. data.minimum[CHANNEL_THROTTLE] = 1010;
  79. data.maximum[CHANNEL_THROTTLE] = 1950;
  80. data.minimum[CHANNEL_ROLL] = 1050;
  81. data.maximum[CHANNEL_ROLL] = 1950;
  82. data.minimum[CHANNEL_PITCH] = 1080;
  83. data.maximum[CHANNEL_PITCH] = 1890;
  84. data.minimum[CHANNEL_AUX1] = 990;
  85. data.maximum[CHANNEL_AUX1] = 2100;
  86. data.minimum[CHANNEL_AUX2] = 990;
  87. data.maximum[CHANNEL_AUX2] = 1990;
  88. toEEPROM(data);
  89. }
  90. CPPM::instance().setChannels(data.channels);
  91. CPPM::instance().setFrameLength(data.frameLength);
  92. CPPM::instance().setPulseLength(data.pulseLength);
  93. CPPM::instance().setInvert(data.inverted);
  94. CPPM::instance().setOutput(data.cppmPin);
  95. for (uint8_t i = 0; i < CHANNELS_MAX; i++) {
  96. joyCPPM.setInvert(i, data.invert[i]);
  97. joyCPPM.setMinimum(i, data.minimum[i]);
  98. joyCPPM.setMaximum(i, data.maximum[i]);
  99. joyCPPM.setTrim(i, data.trim[i]);
  100. }
  101. }
  102. void eepromWrite() {
  103. ConfigData data;
  104. data.channels = CPPM::instance().getChannels();
  105. data.frameLength = CPPM::instance().getFrameLength();
  106. data.pulseLength = CPPM::instance().getPulseLength();
  107. data.inverted = CPPM::instance().getInvert();
  108. data.cppmPin = CPPM::instance().getOutput();
  109. for (uint8_t i = 0; i < CHANNELS_MAX; i++) {
  110. data.invert[i] = joyCPPM.getInvert(i);
  111. data.minimum[i] = joyCPPM.getMinimum(i);
  112. data.maximum[i] = joyCPPM.getMaximum(i);
  113. data.trim[i] = joyCPPM.getTrim(i);
  114. }
  115. toEEPROM(data);
  116. }