1 | %Copyright: Puzzlebox Productions LLC |
---|

2 | %This code is released under GPL v2. |
---|

3 | %Nov. 29th, 2012 |
---|

4 | |
---|

5 | function code=throttleYawPitch(throttle,yaw,pitch,_channel) |
---|

6 | |
---|

7 | checkSum=0; |
---|

8 | mainCode=0; |
---|

9 | |
---|

10 | %throttle |
---|

11 | **for** n=1:7 |
---|

12 | mainCode=bitset(mainCode,21+n,bitget(throttle,n));%getting the first 7 digits to mainCode |
---|

13 | endfor |
---|

14 | |
---|

15 | |
---|

16 | mainCode=bitset(mainCode,21,1); %meaning unclear, possibly left button. |
---|

17 | |
---|

18 | %channel selection first half |
---|

19 | **if** (_channel==**'C'**) mainCode=bitset(mainCode,20,1); |
---|

20 | **else** mainCode=bitset(mainCode,20,0); %this digit equals 0 **in** channel A or B |
---|

21 | endif |
---|

22 | |
---|

23 | **for** n=1:7 |
---|

24 | mainCode=bitset(mainCode,12+n,bitget(yaw,n));%yaw |
---|

25 | endfor |
---|

26 | |
---|

27 | %channel selection second half |
---|

28 | **if** (_channel==**'A'**) mainCode=bitset(mainCode,12,1); |
---|

29 | **else** mainCode=bitset(mainCode,12,0); %**if** channel B or C, this digit equals 0; |
---|

30 | endif |
---|

31 | |
---|

32 | mainCode=bitset(mainCode,11,0);% meaning unclear, possibly right button. |
---|

33 | |
---|

34 | **for** n=1:6 |
---|

35 | mainCode=bitset(mainCode,4+n,bitget(pitch,n));%pitch |
---|

36 | endfor |
---|

37 | |
---|

38 | % CheckSum |
---|

39 | **for** n=1:6 checkSum += bitand(bitshift(mainCode,-(4*n)),15);%sum up every 4 digits **in** the code |
---|

40 | endfor |
---|

41 | |
---|

42 | checkSum=bitand(checkSum,15); %get the last 4 bits of the sum |
---|

43 | checkSum=bitand(bitcmp(checkSum,4)+1,15);%16-sum is the formula of this helicopter |
---|

44 | **for** n=1:4 |
---|

45 | mainCode=bitset(mainCode,n,bitget(checkSum,n)); %get the last 4 digit of CheckSum |
---|

46 | endfor |
---|

47 | codeString=dec2bin(mainCode); |
---|

48 | code=[]; |
---|

49 | **for** n=1:28 |
---|

50 | code=[code str2num(codeString(n))]; |
---|

51 | endfor |
---|