Created
November 18, 2017 21:49
-
-
Save FLYBYME/1b38efa3889f75a6046cfeecdc6e9163 to your computer and use it in GitHub Desktop.
decode dsm packet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define BUFFER_SIZE 14 | |
#define MAX_CHANS 8 | |
int ledPin = 13; | |
int count = 0; | |
int chan[MAX_CHANS]; | |
bool state = false; | |
bool sync = false; | |
byte chanBuffer[BUFFER_SIZE]; | |
uint8_t chanPos; | |
void setup() { | |
// initialize both serial ports: | |
Serial.begin(115200); | |
pinMode(ledPin, OUTPUT); | |
} | |
int decode(uint16_t dsm) { | |
dsm &= 0x7ff; | |
return ((((int)dsm - 1024) * 1000) / 1700) + 1500; | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
while (Serial.available() && (chanPos < BUFFER_SIZE)) { | |
byte bt = Serial.read(); | |
if (bt == 0xAB && !sync && chanPos == 0) { | |
sync = true; | |
//Serial.println("start of sync msg"); | |
} | |
if (sync) { | |
chanBuffer[chanPos++] = bt; | |
digitalWrite(ledPin, state = !state); | |
} | |
} | |
if (chanPos == BUFFER_SIZE) { | |
for (int i = 0; i < MAX_CHANS; ++i) { | |
uint8_t spekChannel = i * 2; | |
byte bh = chanBuffer[spekChannel]; | |
byte bl = chanBuffer[spekChannel + 1]; | |
// chan[spekChannel] = decode(bh | bl); | |
chan[spekChannel] = bl; | |
} | |
for (int i = 0; i < MAX_CHANS; ++i) { | |
Serial.print(" "); | |
Serial.print(i); | |
Serial.print(":"); | |
Serial.print(chan[i]); | |
} | |
Serial.println(); | |
//Serial.println("end of sync msg"); | |
sync = false; | |
chanPos = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment