Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
driverLifiRx.cpp@21:82b5759f5047, 2019-04-09 (annotated)
- Committer:
- JongYongPark
- Date:
- Tue Apr 09 09:11:24 2019 +0000
- Revision:
- 21:82b5759f5047
- Parent:
- 5:5fe30df87772
working version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JongYongPark | 0:488aa8153e15 | 1 | #include "driverLifiRx.h" |
JongYongPark | 0:488aa8153e15 | 2 | |
JongYongPark | 0:488aa8153e15 | 3 | |
JongYongPark | 0:488aa8153e15 | 4 | //typedef enum LIFI_Tx_Rx_Mode LIFI_Tx_Rx_Mode; |
JongYongPark | 0:488aa8153e15 | 5 | //Lifi_RxTx_Mode vlc_tx_rx_mode = LIFI_Tx_Mode; |
JongYongPark | 0:488aa8153e15 | 6 | //int vlc_tx_rx_mode_bool = 0; |
JongYongPark | 0:488aa8153e15 | 7 | // |
JongYongPark | 0:488aa8153e15 | 8 | //// RX 는 ADC PIN을 3.3V 로 연결한다. |
JongYongPark | 0:488aa8153e15 | 9 | //Lifi_RxTx_Mode is_board_vlc_rx_mode_enum() |
JongYongPark | 0:488aa8153e15 | 10 | //{ |
JongYongPark | 0:488aa8153e15 | 11 | // float meas_r1; |
JongYongPark | 0:488aa8153e15 | 12 | // float meas_v1; |
JongYongPark | 0:488aa8153e15 | 13 | // |
JongYongPark | 0:488aa8153e15 | 14 | // meas_r1 = adc_tx_rx_mode_select.read() ; |
JongYongPark | 0:488aa8153e15 | 15 | // meas_v1 = meas_r1 * 3300; |
JongYongPark | 0:488aa8153e15 | 16 | // if( ceil(meas_v1) > 1000 ) return LIFI_Tx_Mode; |
JongYongPark | 0:488aa8153e15 | 17 | // else return Lifi_Rx_Mode; |
JongYongPark | 0:488aa8153e15 | 18 | //} |
JongYongPark | 0:488aa8153e15 | 19 | |
JongYongPark | 0:488aa8153e15 | 20 | // Pull up setting. RX - Connect to GND |
JongYongPark | 0:488aa8153e15 | 21 | int Lifi_IsRxMode() |
JongYongPark | 0:488aa8153e15 | 22 | { |
JongYongPark | 0:488aa8153e15 | 23 | #define USE_MODE_SELECT_BY_ADC 0 |
JongYongPark | 0:488aa8153e15 | 24 | #if USE_MODE_SELECT_BY_ADC |
JongYongPark | 0:488aa8153e15 | 25 | // 연결이 되지 않으면 기본으로 0 이다. |
JongYongPark | 0:488aa8153e15 | 26 | // 따라서 ADC를 이용할 경우 3.3V와 연결하고나 저항을 이용해서 여러 입력값에 따른 모드를 선택할 수 있다. |
JongYongPark | 0:488aa8153e15 | 27 | float meas_r1; |
JongYongPark | 0:488aa8153e15 | 28 | float meas_v1; |
JongYongPark | 0:488aa8153e15 | 29 | |
JongYongPark | 0:488aa8153e15 | 30 | meas_r1 = adc_tx_rx_mode_select.read() ; |
JongYongPark | 0:488aa8153e15 | 31 | meas_v1 = meas_r1 * 3300; |
JongYongPark | 0:488aa8153e15 | 32 | printf("meas_r1 = %f, meas_v1 = %f",meas_r1, meas_v1 ); |
JongYongPark | 0:488aa8153e15 | 33 | if( ceil(meas_v1) > 1000 ) return LIFI_Tx_Mode; |
JongYongPark | 0:488aa8153e15 | 34 | else return Lifi_Rx_Mode; |
JongYongPark | 0:488aa8153e15 | 35 | |
JongYongPark | 0:488aa8153e15 | 36 | #else |
JongYongPark | 0:488aa8153e15 | 37 | int value=1; |
JongYongPark | 0:488aa8153e15 | 38 | value = lifi_TxRx_ModePin.read(); |
JongYongPark | 0:488aa8153e15 | 39 | if( value == 0 ) { |
JongYongPark | 0:488aa8153e15 | 40 | printf("RX mode (%d))", value); |
JongYongPark | 0:488aa8153e15 | 41 | return 1; |
JongYongPark | 0:488aa8153e15 | 42 | } else { |
JongYongPark | 0:488aa8153e15 | 43 | printf("TX mode (%d))", value); |
JongYongPark | 0:488aa8153e15 | 44 | return 0; |
JongYongPark | 0:488aa8153e15 | 45 | } |
JongYongPark | 0:488aa8153e15 | 46 | #endif |
JongYongPark | 0:488aa8153e15 | 47 | } |
JongYongPark | 0:488aa8153e15 | 48 | |
JongYongPark | 21:82b5759f5047 | 49 | int LifiRx_GetDebugLed(){ |
JongYongPark | 21:82b5759f5047 | 50 | return led_rx_debug ; |
JongYongPark | 21:82b5759f5047 | 51 | } |
JongYongPark | 0:488aa8153e15 | 52 | |
JongYongPark | 0:488aa8153e15 | 53 | // https://forum.arduino.cc/index.php?topic=185158.0 |
JongYongPark | 0:488aa8153e15 | 54 | float LifiRx_GetLuxFromPdVoltage(float volt_mili) |
JongYongPark | 0:488aa8153e15 | 55 | { |
JongYongPark | 0:488aa8153e15 | 56 | #if 0 |
JongYongPark | 0:488aa8153e15 | 57 | float amps_mili = mili_volt / 10000.0; // across 10,000 Ohms |
JongYongPark | 0:488aa8153e15 | 58 | float microamps = amps_mili * 1000; |
JongYongPark | 0:488aa8153e15 | 59 | float lux = microamps * 2.0; |
JongYongPark | 0:488aa8153e15 | 60 | return lux; |
JongYongPark | 0:488aa8153e15 | 61 | #else |
JongYongPark | 0:488aa8153e15 | 62 | return volt_mili / 5.0 ; |
JongYongPark | 0:488aa8153e15 | 63 | #endif |
JongYongPark | 0:488aa8153e15 | 64 | } |
JongYongPark | 0:488aa8153e15 | 65 | |
JongYongPark | 0:488aa8153e15 | 66 | // https://code.google.com/archive/p/davidegironi/downloads |
JongYongPark | 0:488aa8153e15 | 67 | /* |
JongYongPark | 0:488aa8153e15 | 68 | * get lux from voltage |
JongYongPark | 0:488aa8153e15 | 69 | */ |
JongYongPark | 0:488aa8153e15 | 70 | double LifiRx_GetLux_Temt6000(double vol) |
JongYongPark | 0:488aa8153e15 | 71 | { |
JongYongPark | 0:488aa8153e15 | 72 | //suppose we read the voltage from the emitter of temt6000, with a 10k pulldown resistor connected to gnd |
JongYongPark | 0:488aa8153e15 | 73 | //V[V] = I[A]*R[ohm] = I[uA] * 1[A]/1000000[킕] * 10000[ohm] = I[uA] * 1[A]/1[uA] * 0.01[ohm] |
JongYongPark | 0:488aa8153e15 | 74 | //1[uA] = V[V] / 0.01[ohm] = 100 * V[V/ohm] |
JongYongPark | 0:488aa8153e15 | 75 | //the curve from datasheet seems y=2x+0, so lux = 2 * I[uA] = 2 * 100 * V[V/ohm] |
JongYongPark | 0:488aa8153e15 | 76 | return 2*(100*vol); |
JongYongPark | 0:488aa8153e15 | 77 | } |
JongYongPark | 3:ae1f53883bc0 | 78 | |
JongYongPark | 3:ae1f53883bc0 | 79 | float meas_v1_max=0; |
JongYongPark | 3:ae1f53883bc0 | 80 | float meas_v1_min=3300; |
JongYongPark | 3:ae1f53883bc0 | 81 | float meas_v2_max=0; |
JongYongPark | 3:ae1f53883bc0 | 82 | float meas_v2_min=3300; |
JongYongPark | 3:ae1f53883bc0 | 83 | float meas_v3_max=0; |
JongYongPark | 3:ae1f53883bc0 | 84 | float meas_v3_min=3300; |
JongYongPark | 3:ae1f53883bc0 | 85 | float meas_v4_max=0; |
JongYongPark | 3:ae1f53883bc0 | 86 | float meas_v4_min=3300; |
JongYongPark | 3:ae1f53883bc0 | 87 | |
JongYongPark | 0:488aa8153e15 | 88 | void LifiRx_PdTunning() |
JongYongPark | 0:488aa8153e15 | 89 | { |
JongYongPark | 0:488aa8153e15 | 90 | float meas_r1; |
JongYongPark | 0:488aa8153e15 | 91 | float meas_v1; |
JongYongPark | 0:488aa8153e15 | 92 | float meas_r2; |
JongYongPark | 0:488aa8153e15 | 93 | float meas_v2; |
JongYongPark | 0:488aa8153e15 | 94 | float meas_r3; |
JongYongPark | 0:488aa8153e15 | 95 | float meas_v3; |
JongYongPark | 0:488aa8153e15 | 96 | float meas_r4; |
JongYongPark | 0:488aa8153e15 | 97 | float meas_v4; |
JongYongPark | 0:488aa8153e15 | 98 | |
JongYongPark | 3:ae1f53883bc0 | 99 | meas_v1_max=0; |
JongYongPark | 3:ae1f53883bc0 | 100 | meas_v1_min=3300; |
JongYongPark | 3:ae1f53883bc0 | 101 | meas_v2_max=0; |
JongYongPark | 3:ae1f53883bc0 | 102 | meas_v2_min=3300; |
JongYongPark | 3:ae1f53883bc0 | 103 | meas_v3_max=0; |
JongYongPark | 3:ae1f53883bc0 | 104 | meas_v3_min=3300; |
JongYongPark | 3:ae1f53883bc0 | 105 | meas_v4_max=0; |
JongYongPark | 3:ae1f53883bc0 | 106 | meas_v4_min=3300; |
JongYongPark | 3:ae1f53883bc0 | 107 | |
JongYongPark | 0:488aa8153e15 | 108 | // note myvalues for "office light" (ca. 200 lux) and direct sunlight, clear day (ca 50000lux) |
JongYongPark | 0:488aa8153e15 | 109 | |
JongYongPark | 0:488aa8153e15 | 110 | |
JongYongPark | 0:488aa8153e15 | 111 | // https://www.dfrobot.com/wiki/index.php/DFRobot_Ambient_Light_Sensor_SKU:DFR0026 |
JongYongPark | 0:488aa8153e15 | 112 | //Specification |
JongYongPark | 0:488aa8153e15 | 113 | //Supply Voltage: 3.3V to 5V |
JongYongPark | 0:488aa8153e15 | 114 | //Illumination range : 1 Lux to 6000 Lux |
JongYongPark | 0:488aa8153e15 | 115 | //Responsive time : 15us |
JongYongPark | 0:488aa8153e15 | 116 | //Interface: Analog |
JongYongPark | 0:488aa8153e15 | 117 | //Size:22x30mm |
JongYongPark | 0:488aa8153e15 | 118 | |
JongYongPark | 0:488aa8153e15 | 119 | |
JongYongPark | 0:488aa8153e15 | 120 | |
JongYongPark | 0:488aa8153e15 | 121 | printf("\n4 LifiRx_PdTunning \n"); |
JongYongPark | 0:488aa8153e15 | 122 | buttonInitSkipLoop(); |
JongYongPark | 0:488aa8153e15 | 123 | while(1) { |
JongYongPark | 0:488aa8153e15 | 124 | if( buttonCanSkipLoop() ) return; |
JongYongPark | 0:488aa8153e15 | 125 | meas_r1 = adc_1.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range) |
JongYongPark | 0:488aa8153e15 | 126 | meas_v1 = meas_r1 * 3300; // Converts value in the 0V-3.3V range |
JongYongPark | 0:488aa8153e15 | 127 | meas_r2 = adc_2.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range) |
JongYongPark | 0:488aa8153e15 | 128 | meas_v2 = meas_r2 * 3300; // Converts value in the 0V-3.3V range |
JongYongPark | 0:488aa8153e15 | 129 | meas_r3 = adc_3.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range) |
JongYongPark | 0:488aa8153e15 | 130 | meas_v3 = meas_r3 * 3300; // Converts value in the 0V-3.3V range |
JongYongPark | 0:488aa8153e15 | 131 | meas_r4 = adc_4.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range) |
JongYongPark | 0:488aa8153e15 | 132 | meas_v4 = meas_r4 * 3300; // Converts value in the 0V-3.3V range |
JongYongPark | 3:ae1f53883bc0 | 133 | |
JongYongPark | 3:ae1f53883bc0 | 134 | if( meas_v1_min > meas_v1 ) meas_v1_min = meas_v1; |
JongYongPark | 3:ae1f53883bc0 | 135 | if( meas_v1_max < meas_v1 ) meas_v1_max = meas_v1; |
JongYongPark | 3:ae1f53883bc0 | 136 | if( meas_v2_min > meas_v2 ) meas_v2_min = meas_v2; |
JongYongPark | 3:ae1f53883bc0 | 137 | if( meas_v2_max < meas_v2 ) meas_v2_max = meas_v2; |
JongYongPark | 3:ae1f53883bc0 | 138 | if( meas_v3_min > meas_v3 ) meas_v3_min = meas_v3; |
JongYongPark | 3:ae1f53883bc0 | 139 | if( meas_v3_max < meas_v3 ) meas_v3_max = meas_v3; |
JongYongPark | 3:ae1f53883bc0 | 140 | if( meas_v4_min > meas_v4 ) meas_v4_min = meas_v4; |
JongYongPark | 3:ae1f53883bc0 | 141 | if( meas_v4_max < meas_v4 ) meas_v4_max = meas_v4; |
JongYongPark | 3:ae1f53883bc0 | 142 | |
JongYongPark | 0:488aa8153e15 | 143 | // Display values |
JongYongPark | 3:ae1f53883bc0 | 144 | printf("measure 1 = %f = %4.0f mV = %5.0f uA = %5.0f lux delta = %5.0f \n", meas_r1, meas_v1, meas_v1/10.0, meas_v1/5.0, meas_v1_max - meas_v1_min); |
JongYongPark | 3:ae1f53883bc0 | 145 | printf("measure 2 = %f = %4.0f mV = %5.0f uA = %5.0f lux delta = %5.0f \n", meas_r2, meas_v2, meas_v2/10.0, meas_v2/5.0, meas_v2_max - meas_v2_min); |
JongYongPark | 3:ae1f53883bc0 | 146 | printf("measure 3 = %f = %4.0f mV = %5.0f uA = %5.0f lux delta = %5.0f \n", meas_r3, meas_v3, meas_v3/10.0, meas_v3/5.0, meas_v3_max - meas_v3_min); |
JongYongPark | 3:ae1f53883bc0 | 147 | printf("measure 4 = %f = %4.0f mV = %5.0f uA = %5.0f lux delta = %5.0f \n", meas_r4, meas_v4, meas_v4/10.0, meas_v4/5.0, meas_v4_max - meas_v4_min); |
JongYongPark | 0:488aa8153e15 | 148 | printf("\033[4A"); |
JongYongPark | 0:488aa8153e15 | 149 | wait(0.1); |
JongYongPark | 0:488aa8153e15 | 150 | } |
JongYongPark | 0:488aa8153e15 | 151 | } |
JongYongPark | 0:488aa8153e15 | 152 | |
JongYongPark | 3:ae1f53883bc0 | 153 | |
JongYongPark | 0:488aa8153e15 | 154 | int LifiRx_AdcRead(int num) |
JongYongPark | 0:488aa8153e15 | 155 | { |
JongYongPark | 0:488aa8153e15 | 156 | //floor(x) - round down |
JongYongPark | 0:488aa8153e15 | 157 | //ceil(x) - round up |
JongYongPark | 0:488aa8153e15 | 158 | |
JongYongPark | 0:488aa8153e15 | 159 | float meas_r1; |
JongYongPark | 0:488aa8153e15 | 160 | float meas_v1; |
JongYongPark | 0:488aa8153e15 | 161 | float meas_r2; |
JongYongPark | 0:488aa8153e15 | 162 | float meas_v2; |
JongYongPark | 0:488aa8153e15 | 163 | float meas_r3; |
JongYongPark | 0:488aa8153e15 | 164 | float meas_v3; |
JongYongPark | 0:488aa8153e15 | 165 | float meas_r4; |
JongYongPark | 0:488aa8153e15 | 166 | float meas_v4; |
JongYongPark | 0:488aa8153e15 | 167 | float meas_r_average; |
JongYongPark | 0:488aa8153e15 | 168 | float meas_v_average; |
JongYongPark | 0:488aa8153e15 | 169 | int ret = 0; |
JongYongPark | 0:488aa8153e15 | 170 | switch(num) { |
JongYongPark | 0:488aa8153e15 | 171 | case LIFI_CHANNEL_1: |
JongYongPark | 0:488aa8153e15 | 172 | meas_r1 = adc_1.read() ; |
JongYongPark | 0:488aa8153e15 | 173 | meas_v1 = meas_r1 * 3300; |
JongYongPark | 0:488aa8153e15 | 174 | ret = ceil(meas_v1); |
JongYongPark | 0:488aa8153e15 | 175 | break; |
JongYongPark | 0:488aa8153e15 | 176 | case LIFI_CHANNEL_2: |
JongYongPark | 0:488aa8153e15 | 177 | meas_r2 = adc_2.read() ; |
JongYongPark | 0:488aa8153e15 | 178 | meas_v2 = meas_r2 * 3300; |
JongYongPark | 0:488aa8153e15 | 179 | ret = ceil(meas_v2); |
JongYongPark | 0:488aa8153e15 | 180 | break; |
JongYongPark | 0:488aa8153e15 | 181 | case LIFI_CHANNEL_3: |
JongYongPark | 0:488aa8153e15 | 182 | meas_r3 = adc_3.read() ; |
JongYongPark | 0:488aa8153e15 | 183 | meas_v3 = meas_r3 * 3300; |
JongYongPark | 0:488aa8153e15 | 184 | ret = ceil(meas_v3); |
JongYongPark | 0:488aa8153e15 | 185 | break; |
JongYongPark | 0:488aa8153e15 | 186 | case LIFI_CHANNEL_4: |
JongYongPark | 0:488aa8153e15 | 187 | meas_r4 = adc_4.read() ; |
JongYongPark | 0:488aa8153e15 | 188 | meas_v4 = meas_r4 * 3300; |
JongYongPark | 0:488aa8153e15 | 189 | ret = ceil(meas_v4); |
JongYongPark | 0:488aa8153e15 | 190 | break; |
JongYongPark | 0:488aa8153e15 | 191 | case LIFI_CHANNEL_ALL: |
JongYongPark | 0:488aa8153e15 | 192 | default: |
JongYongPark | 0:488aa8153e15 | 193 | meas_r1 = adc_1.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range) |
JongYongPark | 0:488aa8153e15 | 194 | meas_v1 = meas_r1 * 3300; // Converts value in the 0V-3.3V range |
JongYongPark | 0:488aa8153e15 | 195 | meas_r2 = adc_2.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range) |
JongYongPark | 0:488aa8153e15 | 196 | meas_v2 = meas_r2 * 3300; // Converts value in the 0V-3.3V range |
JongYongPark | 0:488aa8153e15 | 197 | meas_r3 = adc_3.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range) |
JongYongPark | 0:488aa8153e15 | 198 | meas_v3 = meas_r3 * 3300; // Converts value in the 0V-3.3V range |
JongYongPark | 0:488aa8153e15 | 199 | meas_r4 = adc_4.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range) |
JongYongPark | 0:488aa8153e15 | 200 | meas_v4 = meas_r4 * 3300; // Converts value in the 0V-3.3V range |
JongYongPark | 0:488aa8153e15 | 201 | meas_r_average = ( meas_r1 + meas_r2 + meas_r3 + meas_r4 ) / 4 ; |
JongYongPark | 0:488aa8153e15 | 202 | meas_v_average = meas_r_average * 3300; |
JongYongPark | 0:488aa8153e15 | 203 | ret = ceil(meas_v_average); |
JongYongPark | 0:488aa8153e15 | 204 | break; |
JongYongPark | 0:488aa8153e15 | 205 | } |
JongYongPark | 0:488aa8153e15 | 206 | return ret; |
JongYongPark | 0:488aa8153e15 | 207 | } |
JongYongPark | 0:488aa8153e15 | 208 | |
JongYongPark | 0:488aa8153e15 | 209 | void LifiRx_Run() |
JongYongPark | 0:488aa8153e15 | 210 | { |
JongYongPark | 0:488aa8153e15 | 211 | printf("\n======== LifiRx_Run =========\n"); |
JongYongPark | 0:488aa8153e15 | 212 | #if USE_LOCAL_TICKER |
JongYongPark | 0:488aa8153e15 | 213 | Ticker lifi_rx_timer; |
JongYongPark | 0:488aa8153e15 | 214 | #endif |
JongYongPark | 0:488aa8153e15 | 215 | // us_timestamp_t us_time; |
JongYongPark | 0:488aa8153e15 | 216 | |
JongYongPark | 0:488aa8153e15 | 217 | LifiRx_Init(); |
JongYongPark | 0:488aa8153e15 | 218 | printf("Tx Symbol Feq=%f sec / %d us \n",LIFI_RX_SYMBOL_PERIOD_SEC,LIFI_RX_SYMBOL_PERIOD_US); |
JongYongPark | 0:488aa8153e15 | 219 | printf("Rx Sample Feq=%f sec / %d us \n",LIFI_RX_SYMBOL_SAMPLE_PERIOD_SEC,LIFI_RX_SYMBOL_SAMPLE_PERIOD_US); |
JongYongPark | 0:488aa8153e15 | 220 | printf("Rx Sample/Symbol=%d times / adc edge threshod=%d mV \n",LIFI_RX_SAMPLE_PER_SYMBOL,LIFI_RX_EDGE_THRESHOLD); |
JongYongPark | 0:488aa8153e15 | 221 | printf("MIMO LIFI_MIMO_CHANNEL_NUM=%d / LIFI_ACTUAL_MAX_CHANNEL_NUM=%d \n",LIFI_MIMO_CHANNEL_NUM,LIFI_ACTUAL_MAX_CHANNEL_NUM); |
JongYongPark | 0:488aa8153e15 | 222 | printf("Tx interval = %d sec \n",LIFI_DEMO_TX_PERIOD_SEC); |
JongYongPark | 0:488aa8153e15 | 223 | |
JongYongPark | 0:488aa8153e15 | 224 | lifi_rx_timer.attach(&LifiRx_SampleSignalEdge, LIFI_RX_SYMBOL_SAMPLE_PERIOD_SEC); |
JongYongPark | 0:488aa8153e15 | 225 | printf("1"); |
JongYongPark | 0:488aa8153e15 | 226 | // us_time = LIFI_RX_SYMBOL_SAMPLE_PERIOD_US; |
JongYongPark | 0:488aa8153e15 | 227 | // lifi_rx_timer.attach_us(&LifiRx_SampleSignalEdge, us_time); |
JongYongPark | 0:488aa8153e15 | 228 | //////// main loop /////// |
JongYongPark | 0:488aa8153e15 | 229 | buttonInitSkipLoop(); |
JongYongPark | 0:488aa8153e15 | 230 | //printf("2"); |
JongYongPark | 0:488aa8153e15 | 231 | while(1) { |
JongYongPark | 0:488aa8153e15 | 232 | //printf("3"); |
JongYongPark | 0:488aa8153e15 | 233 | if( buttonCanSkipLoop() ) return; |
JongYongPark | 0:488aa8153e15 | 234 | LifiRx_LoopOOK(); |
JongYongPark | 0:488aa8153e15 | 235 | //printf("4"); |
JongYongPark | 3:ae1f53883bc0 | 236 | //wait_ms(50); |
JongYongPark | 3:ae1f53883bc0 | 237 | |
JongYongPark | 0:488aa8153e15 | 238 | } |
JongYongPark | 0:488aa8153e15 | 239 | } |
JongYongPark | 0:488aa8153e15 | 240 | |
JongYongPark | 0:488aa8153e15 | 241 | ////////////// |
JongYongPark | 0:488aa8153e15 | 242 | |
JongYongPark | 3:ae1f53883bc0 | 243 | #if USE_MBED_OS5 |
JongYongPark | 0:488aa8153e15 | 244 | // https://os.mbed.com/blog/entry/Simplify-your-code-with-mbed-events/ |
JongYongPark | 0:488aa8153e15 | 245 | // create an event queue |
JongYongPark | 3:ae1f53883bc0 | 246 | #if USE_LIFI_RX_CHECK_QUEUE |
JongYongPark | 0:488aa8153e15 | 247 | EventQueue queue; |
JongYongPark | 1:502e5a0c14c3 | 248 | EventQueue lifi_tx_check_queue(32 * EVENTS_EVENT_SIZE); |
JongYongPark | 1:502e5a0c14c3 | 249 | #endif |
JongYongPark | 0:488aa8153e15 | 250 | |
JongYongPark | 0:488aa8153e15 | 251 | void do_something() { |
JongYongPark | 0:488aa8153e15 | 252 | // this now runs in the context of eventThread, instead of in the ISR |
JongYongPark | 0:488aa8153e15 | 253 | //printf("!"); |
JongYongPark | 0:488aa8153e15 | 254 | LifiRx_SampleSignalEdge(); |
JongYongPark | 0:488aa8153e15 | 255 | } |
JongYongPark | 0:488aa8153e15 | 256 | |
JongYongPark | 0:488aa8153e15 | 257 | |
JongYongPark | 0:488aa8153e15 | 258 | |
JongYongPark | 0:488aa8153e15 | 259 | void LifiRx_RunWithEventQueue() |
JongYongPark | 0:488aa8153e15 | 260 | { |
JongYongPark | 0:488aa8153e15 | 261 | |
JongYongPark | 0:488aa8153e15 | 262 | #if USE_LOCAL_TICKER |
JongYongPark | 0:488aa8153e15 | 263 | Ticker lifi_rx_timer; |
JongYongPark | 0:488aa8153e15 | 264 | #endif |
JongYongPark | 0:488aa8153e15 | 265 | |
JongYongPark | 0:488aa8153e15 | 266 | // create a thread that'll run the event queue's dispatch function |
JongYongPark | 1:502e5a0c14c3 | 267 | Thread eventThread(osPriorityHigh); |
JongYongPark | 1:502e5a0c14c3 | 268 | //Thread eventThread; |
JongYongPark | 0:488aa8153e15 | 269 | eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); |
JongYongPark | 0:488aa8153e15 | 270 | |
JongYongPark | 3:ae1f53883bc0 | 271 | #if (USE_MBED_OS5 && USE_LIFI_RX_CHECK_QUEUE) |
JongYongPark | 1:502e5a0c14c3 | 272 | Thread lifiRxCheckThread(osPriorityNormal); |
JongYongPark | 1:502e5a0c14c3 | 273 | lifiRxCheckThread.start(callback(&lifi_tx_check_queue, &EventQueue::dispatch_forever)); |
JongYongPark | 1:502e5a0c14c3 | 274 | #endif |
JongYongPark | 0:488aa8153e15 | 275 | |
JongYongPark | 0:488aa8153e15 | 276 | printf("\n======== LifiRx_RunWithEventQueue =========\n"); |
JongYongPark | 0:488aa8153e15 | 277 | LifiRx_Init(); |
JongYongPark | 0:488aa8153e15 | 278 | printf("Tx Symbol Feq=%f sec / %d us \n",LIFI_RX_SYMBOL_PERIOD_SEC,LIFI_RX_SYMBOL_PERIOD_US); |
JongYongPark | 0:488aa8153e15 | 279 | printf("Rx Sample Feq=%f sec / %d us \n",LIFI_RX_SYMBOL_SAMPLE_PERIOD_SEC,LIFI_RX_SYMBOL_SAMPLE_PERIOD_US); |
JongYongPark | 0:488aa8153e15 | 280 | printf("Rx Sample/Symbol=%d times / adc edge threshod=%d mV \n",LIFI_RX_SAMPLE_PER_SYMBOL,LIFI_RX_EDGE_THRESHOLD); |
JongYongPark | 0:488aa8153e15 | 281 | printf("MIMO LIFI_MIMO_CHANNEL_NUM=%d / LIFI_ACTUAL_MAX_CHANNEL_NUM=%d \n",LIFI_MIMO_CHANNEL_NUM,LIFI_ACTUAL_MAX_CHANNEL_NUM); |
JongYongPark | 0:488aa8153e15 | 282 | printf("Tx interval = %d sec \n",LIFI_DEMO_TX_PERIOD_SEC); |
JongYongPark | 0:488aa8153e15 | 283 | |
JongYongPark | 0:488aa8153e15 | 284 | lifi_rx_timer.attach(queue.event(&do_something), LIFI_RX_SYMBOL_SAMPLE_PERIOD_SEC); |
JongYongPark | 0:488aa8153e15 | 285 | // us_time = LIFI_RX_SYMBOL_SAMPLE_PERIOD_US; |
JongYongPark | 0:488aa8153e15 | 286 | // lifi_rx_timer.attach_us(&LifiRx_SampleSignalEdge, us_time); |
JongYongPark | 0:488aa8153e15 | 287 | //////// main loop /////// |
JongYongPark | 0:488aa8153e15 | 288 | buttonInitSkipLoop(); |
JongYongPark | 0:488aa8153e15 | 289 | //printf("2"); |
JongYongPark | 0:488aa8153e15 | 290 | while(1) { |
JongYongPark | 1:502e5a0c14c3 | 291 | #if (USE_LIFI_RX_CHECK_QUEUE==0) |
JongYongPark | 0:488aa8153e15 | 292 | //printf("3"); |
JongYongPark | 0:488aa8153e15 | 293 | if( buttonCanSkipLoop() ) return; |
JongYongPark | 0:488aa8153e15 | 294 | LifiRx_LoopOOK(); |
JongYongPark | 0:488aa8153e15 | 295 | //printf("4"); |
JongYongPark | 0:488aa8153e15 | 296 | #endif |
JongYongPark | 0:488aa8153e15 | 297 | wait_ms(50); |
JongYongPark | 2:e1fd30fce2f8 | 298 | if( buttonCanSkipLoop() ) return; |
JongYongPark | 0:488aa8153e15 | 299 | } |
JongYongPark | 0:488aa8153e15 | 300 | } |
JongYongPark | 0:488aa8153e15 | 301 | |
JongYongPark | 3:ae1f53883bc0 | 302 | #endif |