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 | mainCode=bitset(mainCode,29,1); |
---|

11 | %throttle |
---|

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

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

14 | endfor |
---|

15 | |
---|

16 | |
---|

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

18 | |
---|

19 | %channel selection first half |
---|

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

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

22 | endif |
---|

23 | |
---|

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

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

26 | endfor |
---|

27 | |
---|

28 | %channel selection second half |
---|

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

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

31 | endif |
---|

32 | |
---|

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

34 | |
---|

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

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

37 | endfor |
---|

38 | |
---|

39 | % CheckSum |
---|

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

41 | endfor |
---|

42 | |
---|

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

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

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

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

47 | endfor |
---|

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

49 | code=[]; |
---|

50 | **for** n=2:29 |
---|

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

52 | endfor |
---|