Pasted as C++ [Remove this paste ]
Description: No description
URL: http://bcas.tv/paste/results/1kqYIx42.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#if defined(SPEKTRUM)
static bool spekFrameComplete = false;
 
struct x_dat 
{
  unsigned int x_data    : 9;		
  unsigned int x_chan_lo : 2;		
  unsigned int chan      : 4;		
  unsigned int x_chan_hi : 1;		
} __attribute__ ((__packed__));
 
#if (SPEKTRUM == 1024)
struct dat 
{
  unsigned int data      : 10;		
  unsigned int chan      : 4;		
  unsigned int dummy     : 2;		
} __attribute__ ((__packed__));
#endif
 
#if (SPEKTRUM == 2048)
struct dat 
{
  unsigned int data      : 11;		
  unsigned int chan      : 4;		
  unsigned int dummy     : 1;		
} __attribute__ ((__packed__));
#endif
 
typedef union 
{		
  uint8_t      in[SPEK_FRAME_SIZE];
  struct dat   chans[SPEK_FRAME_SIZE/2];		
  struct x_dat x_chans[SPEK_FRAME_SIZE/2];		
} spekMsg;
 
static spekMsg spekFrame;
 
uint8_t endianness[16] = {1,0,3,2,5,4,7,6,9,8,11,10,13,12,15,14};
 
// Receive ISR callback
static void spektrumDataReceive(uint8_t c)
{
    uint32_t spekTime;
    static uint32_t spekTimeLast, spekTimeInterval;
    static uint8_t  spekFramePosition;
 
    spekTime = micros();
    spekTimeInterval = spekTime - spekTimeLast;
    spekTimeLast = spekTime;
    if (spekTimeInterval > 5000)
        spekFramePosition = 0;
    spekFrame.in[endianness[spekFramePosition]] = c;
    if (spekFramePosition == SPEK_FRAME_SIZE - 1) 
    {
        spekFrameComplete = true;
        failsafeCnt = 0;   // clear FailSafe counter
    } else {
        spekFramePosition++;
    }
}
 
static uint8_t xChanAdd = 0;
 
void readSpektrum() 
{
    if (spekFrameComplete)
    {
      for (uint8_t b = 1; b < SPEK_FRAME_SIZE/2; b ++)
      {
        uint8_t spekChannel = spekFrame.chans[b].chan;
        if (spekChannel < RC_CHANS) 
          rcValue[spekChannel] = 988 + (spekFrame.chans[b].data SPEK_DATA_SHIFT);
        #if (SPEKTRUM == 2048)
        if(spekChannel == 0) xChanAdd = 4; 
        if(spekChannel == 1) xChanAdd = 0; 
        if (spekChannel == X_CHAN) 
        {
          uint8_t xChannel = spekFrame.x_chans[b].x_chan_lo + xChanAdd;
          uint16_t value = spekFrame.x_chans[b].x_data;
          xValue[xChannel] =  value; 
        }
        #endif
      }
      spekFrameComplete = false;
    }
}
#endif