![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
a
Dependencies: mbed
Fork of GIMBAL_SAMPLE4_PPM by
PPM/PPMOut.cpp@1:dd6e70abeb8e, 2018-05-25 (annotated)
- Committer:
- halusis
- Date:
- Fri May 25 08:09:17 2018 +0000
- Revision:
- 1:dd6e70abeb8e
- Parent:
- 0:f0b20f502059
a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
halusis | 0:f0b20f502059 | 1 | #include "mbed.h" |
halusis | 0:f0b20f502059 | 2 | #include "PPMOut.h" |
halusis | 0:f0b20f502059 | 3 | |
halusis | 0:f0b20f502059 | 4 | PpmOut::PpmOut(PinName pin, uint8_t channel_number): ppm(pin) { |
halusis | 0:f0b20f502059 | 5 | if(channel_number > MAX_CHANNELS){ |
halusis | 0:f0b20f502059 | 6 | this->channel_number = MAX_CHANNELS; |
halusis | 0:f0b20f502059 | 7 | } |
halusis | 0:f0b20f502059 | 8 | this->channel_number = channel_number; |
halusis | 0:f0b20f502059 | 9 | resetChannels(); |
halusis | 0:f0b20f502059 | 10 | pulse_out = 1; |
halusis | 0:f0b20f502059 | 11 | ppm = pulse_out; |
halusis | 0:f0b20f502059 | 12 | current_dot = 0; |
halusis | 0:f0b20f502059 | 13 | timeout.attach_us(this, &PpmOut::attimeout, FRAME_LEN); |
halusis | 0:f0b20f502059 | 14 | } |
halusis | 0:f0b20f502059 | 15 | |
halusis | 0:f0b20f502059 | 16 | void PpmOut::setChannel(int channel_no, uint16_t value) { |
halusis | 0:f0b20f502059 | 17 | //__disable_irq(); // Disable Interrupts |
halusis | 0:f0b20f502059 | 18 | if(channel_no > channel_number-1){ |
halusis | 0:f0b20f502059 | 19 | return; |
halusis | 0:f0b20f502059 | 20 | } |
halusis | 0:f0b20f502059 | 21 | if(value > MAX_CHANNEL_VALUE){ |
halusis | 0:f0b20f502059 | 22 | value = MAX_CHANNEL_VALUE; |
halusis | 0:f0b20f502059 | 23 | } |
halusis | 0:f0b20f502059 | 24 | dots[channel_no*2] = CHANNEL_SYNC; |
halusis | 0:f0b20f502059 | 25 | dots[channel_no*2+1] = CHANNEL_PAD_SYNC + value; |
halusis | 0:f0b20f502059 | 26 | |
halusis | 0:f0b20f502059 | 27 | setFrameSync(); |
halusis | 0:f0b20f502059 | 28 | //__enable_irq(); // Enable Interrupts |
halusis | 0:f0b20f502059 | 29 | } |
halusis | 0:f0b20f502059 | 30 | |
halusis | 0:f0b20f502059 | 31 | void PpmOut::setFrameSync(){ |
halusis | 0:f0b20f502059 | 32 | uint16_t sum_channels = 0; |
halusis | 0:f0b20f502059 | 33 | for(uint8_t channel = 0; channel < channel_number; channel++) { |
halusis | 0:f0b20f502059 | 34 | sum_channels += dots[channel*2+1]; |
halusis | 0:f0b20f502059 | 35 | } |
halusis | 0:f0b20f502059 | 36 | sum_channels += channel_number*CHANNEL_SYNC; |
halusis | 0:f0b20f502059 | 37 | dots[channel_number*2] = CHANNEL_SYNC; |
halusis | 0:f0b20f502059 | 38 | dots[channel_number*2+1] = FRAME_LEN - CHANNEL_SYNC - sum_channels; |
halusis | 0:f0b20f502059 | 39 | } |
halusis | 0:f0b20f502059 | 40 | |
halusis | 0:f0b20f502059 | 41 | void PpmOut::attimeout() { |
halusis | 0:f0b20f502059 | 42 | pulse_out = !pulse_out; |
halusis | 0:f0b20f502059 | 43 | ppm = pulse_out; |
halusis | 0:f0b20f502059 | 44 | |
halusis | 0:f0b20f502059 | 45 | timeout.attach_us(this, &PpmOut::attimeout, dots[current_dot]); |
halusis | 0:f0b20f502059 | 46 | current_dot++; |
halusis | 0:f0b20f502059 | 47 | |
halusis | 0:f0b20f502059 | 48 | if(current_dot == channel_number*2+2) { // 2 for FRAME_SYNC |
halusis | 0:f0b20f502059 | 49 | current_dot = 0; |
halusis | 0:f0b20f502059 | 50 | } |
halusis | 0:f0b20f502059 | 51 | } |
halusis | 0:f0b20f502059 | 52 | |
halusis | 0:f0b20f502059 | 53 | void PpmOut::resetChannels() { |
halusis | 0:f0b20f502059 | 54 | int8_t channel; |
halusis | 0:f0b20f502059 | 55 | |
halusis | 0:f0b20f502059 | 56 | current_dot = 0; |
halusis | 0:f0b20f502059 | 57 | memset(dots, 0x00, DOTS); |
halusis | 0:f0b20f502059 | 58 | for(channel = 0; channel < channel_number; channel++) { |
halusis | 0:f0b20f502059 | 59 | dots[channel*2] = CHANNEL_SYNC; |
halusis | 0:f0b20f502059 | 60 | dots[channel*2+1] = CHANNEL_PAD_SYNC; |
halusis | 0:f0b20f502059 | 61 | } |
halusis | 0:f0b20f502059 | 62 | setFrameSync(); |
halusis | 0:f0b20f502059 | 63 | } |