Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #include <string.h>
  2.  
  3. #define IBUS_BUFFSIZE 32 // Max iBus packet size (2 byte header, 14 channels x 2 bytes, 2 byte checksum)
  4. #define IBUS_MAXCHANNELS 10
  5. static uint8_t ibusIndex = 0;
  6. static uint8_t ibus[IBUS_BUFFSIZE] = {0};
  7. static uint16_t rcValue[IBUS_MAXCHANNELS];
  8.  
  9. void setup()
  10. {
  11. Serial.begin(115200);
  12.  
  13. digitalWrite(13, HIGH); //Flash on rx update
  14.  
  15. Serial.println(“setup done!”);
  16. }
  17.  
  18. void loop()
  19. {
  20. readRx();
  21. UpdateOutput();
  22. }
  23.  
  24. void UpdateOutput()
  25. {
  26. }
  27. void readRx()
  28. {
  29. uint8_t i;
  30.  
  31. if (Serial.available())
  32. {
  33. uint8_t val = Serial.read();
  34. // Look for 0x2040 as start of packet
  35. if (ibusIndex == 0 && val != 0x20) {
  36. return;
  37. }
  38. if (ibusIndex == 1 && val != 0x40) {
  39. ibusIndex = 0;
  40. return;
  41. }
  42.  
  43. if (ibusIndex < IBUS_BUFFSIZE) ibus[ibusIndex] = val;
  44. ibusIndex++;
  45.  
  46. if (ibusIndex == IBUS_BUFFSIZE)
  47. {
  48. digitalWrite(13,LOW);
  49. }
  50.  
  51. ibusIndex = 0;
  52. rcValue[0] = (ibus[ 3] << 8) + ibus[ 2];
  53. rcValue[1] = (ibus[ 5] << 8) + ibus[ 4];
  54. rcValue[2] = (ibus[ 7] << 8) + ibus[ 6];
  55. rcValue[3] = (ibus[ 9] << 8) + ibus[ 8];
  56. rcValue[4] = (ibus[11] << 8) + ibus[10];
  57. rcValue[5] = (ibus[13] << 8) + ibus[12];
  58. rcValue[6] = (ibus[15] << 8) + ibus[14];
  59. rcValue[7] = (ibus[17] << 8) + ibus[16];
  60. rcValue[8] = (ibus[19] << 8) + ibus[18];
  61. rcValue[9] = (ibus[21] << 8) + ibus[20];
  62. digitalWrite(13,HIGH);
  63. return;
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement