JongYong Park / Mbed 2 deprecated NUCLEO-F767_LIFI_4CH_os2_v2

Dependencies:   mbed

Revision:
0:488aa8153e15
Child:
1:502e5a0c14c3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/driverLifiRx.cpp	Wed Mar 20 09:24:35 2019 +0000
@@ -0,0 +1,265 @@
+#include "driverLifiRx.h"
+
+
+//typedef enum LIFI_Tx_Rx_Mode LIFI_Tx_Rx_Mode;
+//Lifi_RxTx_Mode vlc_tx_rx_mode = LIFI_Tx_Mode;
+//int vlc_tx_rx_mode_bool = 0;
+//
+//// RX 는 ADC PIN을 3.3V 로 연결한다.
+//Lifi_RxTx_Mode is_board_vlc_rx_mode_enum()
+//{
+//    float meas_r1;
+//    float meas_v1;
+//
+//    meas_r1 = adc_tx_rx_mode_select.read() ;
+//    meas_v1 = meas_r1 * 3300;
+//    if( ceil(meas_v1) > 1000 ) return LIFI_Tx_Mode;
+//    else return Lifi_Rx_Mode;
+//}
+
+// Pull up setting. RX - Connect to GND
+int Lifi_IsRxMode()
+{
+#define USE_MODE_SELECT_BY_ADC 0
+#if USE_MODE_SELECT_BY_ADC
+// 연결이 되지 않으면 기본으로 0 이다.
+// 따라서 ADC를 이용할 경우 3.3V와 연결하고나 저항을 이용해서 여러 입력값에 따른 모드를 선택할 수 있다.
+    float meas_r1;
+    float meas_v1;
+
+    meas_r1 = adc_tx_rx_mode_select.read() ;
+    meas_v1 = meas_r1 * 3300;
+    printf("meas_r1 = %f, meas_v1 = %f",meas_r1, meas_v1 );
+    if( ceil(meas_v1) > 1000 ) return LIFI_Tx_Mode;
+    else return Lifi_Rx_Mode;
+
+#else
+    int value=1;
+    value =  lifi_TxRx_ModePin.read();
+    if( value == 0 ) {
+        printf("RX mode (%d))", value);
+        return 1;
+    } else {
+        printf("TX mode (%d))", value);
+        return 0;
+    }
+#endif
+}
+
+
+// https://forum.arduino.cc/index.php?topic=185158.0
+float LifiRx_GetLuxFromPdVoltage(float volt_mili)
+{
+#if 0
+    float amps_mili = mili_volt / 10000.0;  // across 10,000 Ohms
+    float microamps = amps_mili * 1000;
+    float lux = microamps * 2.0;
+    return lux;
+#else
+    return  volt_mili / 5.0 ;
+#endif
+}
+
+// https://code.google.com/archive/p/davidegironi/downloads
+/*
+ * get lux from voltage
+ */
+double LifiRx_GetLux_Temt6000(double vol)
+{
+    //suppose we read the voltage from the emitter of temt6000, with a 10k pulldown resistor connected to gnd
+    //V[V] = I[A]*R[ohm] = I[uA] * 1[A]/1000000[킕] * 10000[ohm] = I[uA] * 1[A]/1[uA] * 0.01[ohm]
+    //1[uA] = V[V] / 0.01[ohm] = 100 * V[V/ohm]
+    //the curve from datasheet seems y=2x+0, so lux = 2 * I[uA] = 2 * 100 * V[V/ohm]
+    return 2*(100*vol);
+}
+void LifiRx_PdTunning()
+{
+    float meas_r1;
+    float meas_v1;
+    float meas_r2;
+    float meas_v2;
+    float meas_r3;
+    float meas_v3;
+    float meas_r4;
+    float meas_v4;
+
+// note myvalues for "office light" (ca. 200 lux) and direct sunlight, clear day (ca 50000lux)
+
+
+// https://www.dfrobot.com/wiki/index.php/DFRobot_Ambient_Light_Sensor_SKU:DFR0026
+//Specification
+//Supply Voltage: 3.3V to 5V
+//Illumination range : 1 Lux to 6000 Lux
+//Responsive time : 15us
+//Interface: Analog
+//Size:22x30mm
+
+
+
+    printf("\n4 LifiRx_PdTunning \n");
+    buttonInitSkipLoop();
+    while(1) {
+        if( buttonCanSkipLoop() ) return;
+        meas_r1 = adc_1.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
+        meas_v1 = meas_r1 * 3300; // Converts value in the 0V-3.3V range
+        meas_r2 = adc_2.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
+        meas_v2 = meas_r2 * 3300; // Converts value in the 0V-3.3V range
+        meas_r3 = adc_3.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
+        meas_v3 = meas_r3 * 3300; // Converts value in the 0V-3.3V range
+        meas_r4 = adc_4.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
+        meas_v4 = meas_r4 * 3300; // Converts value in the 0V-3.3V range
+        // Display values
+        printf("measure 1 = %f = %4.0f mV = %5.0f uA = %5.0f lux  \n", meas_r1, meas_v1, meas_v1/10.0, meas_v1/5.0 );
+        printf("measure 2 = %f = %4.0f mV = %5.0f uA = %5.0f lux  \n", meas_r2, meas_v2, meas_v2/10.0, meas_v2/5.0 );
+        printf("measure 3 = %f = %4.0f mV = %5.0f uA = %5.0f lux  \n", meas_r3, meas_v3, meas_v3/10.0, meas_v3/5.0 );
+        printf("measure 4 = %f = %4.0f mV = %5.0f uA = %5.0f lux  \n", meas_r4, meas_v4, meas_v4/10.0, meas_v4/5.0 );
+        printf("\033[4A");
+        wait(0.1);
+    }
+}
+
+int LifiRx_AdcRead(int num)
+{
+//floor(x) - round down
+//ceil(x) - round up
+
+    float meas_r1;
+    float meas_v1;
+    float meas_r2;
+    float meas_v2;
+    float meas_r3;
+    float meas_v3;
+    float meas_r4;
+    float meas_v4;
+    float meas_r_average;
+    float meas_v_average;
+    int ret = 0;
+    switch(num) {
+        case LIFI_CHANNEL_1:
+            meas_r1 = adc_1.read() ;
+            meas_v1 = meas_r1 * 3300;
+            ret = ceil(meas_v1);
+            break;
+        case LIFI_CHANNEL_2:
+            meas_r2 = adc_2.read() ;
+            meas_v2 = meas_r2 * 3300;
+            ret = ceil(meas_v2);
+            break;
+        case LIFI_CHANNEL_3:
+            meas_r3 = adc_3.read() ;
+            meas_v3 = meas_r3 * 3300;
+            ret = ceil(meas_v3);
+            break;
+        case LIFI_CHANNEL_4:
+            meas_r4 = adc_4.read() ;
+            meas_v4 = meas_r4 * 3300;
+            ret = ceil(meas_v4);
+            break;
+        case LIFI_CHANNEL_ALL:
+        default:
+            meas_r1 = adc_1.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
+            meas_v1 = meas_r1 * 3300; // Converts value in the 0V-3.3V range
+            meas_r2 = adc_2.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
+            meas_v2 = meas_r2 * 3300; // Converts value in the 0V-3.3V range
+            meas_r3 = adc_3.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
+            meas_v3 = meas_r3 * 3300; // Converts value in the 0V-3.3V range
+            meas_r4 = adc_4.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
+            meas_v4 = meas_r4 * 3300; // Converts value in the 0V-3.3V range
+            meas_r_average =  ( meas_r1 + meas_r2 + meas_r3 + meas_r4 ) / 4 ;
+            meas_v_average =  meas_r_average * 3300;
+            ret = ceil(meas_v_average);
+            break;
+    }
+    return ret;
+}
+
+void LifiRx_Run()
+{
+    printf("\n======== LifiRx_Run =========\n");
+#if USE_LOCAL_TICKER
+    Ticker lifi_rx_timer;
+#endif
+//    us_timestamp_t us_time;
+
+    LifiRx_Init();
+    printf("Tx Symbol Feq=%f sec / %d us \n",LIFI_RX_SYMBOL_PERIOD_SEC,LIFI_RX_SYMBOL_PERIOD_US);
+    printf("Rx Sample Feq=%f sec / %d us \n",LIFI_RX_SYMBOL_SAMPLE_PERIOD_SEC,LIFI_RX_SYMBOL_SAMPLE_PERIOD_US);
+    printf("Rx Sample/Symbol=%d times / adc edge threshod=%d mV \n",LIFI_RX_SAMPLE_PER_SYMBOL,LIFI_RX_EDGE_THRESHOLD);
+    printf("MIMO LIFI_MIMO_CHANNEL_NUM=%d  / LIFI_ACTUAL_MAX_CHANNEL_NUM=%d \n",LIFI_MIMO_CHANNEL_NUM,LIFI_ACTUAL_MAX_CHANNEL_NUM);
+    printf("Tx interval = %d sec \n",LIFI_DEMO_TX_PERIOD_SEC);
+    
+    lifi_rx_timer.attach(&LifiRx_SampleSignalEdge, LIFI_RX_SYMBOL_SAMPLE_PERIOD_SEC);
+    printf("1"); 
+//    us_time = LIFI_RX_SYMBOL_SAMPLE_PERIOD_US;
+//    lifi_rx_timer.attach_us(&LifiRx_SampleSignalEdge, us_time);
+    //////// main loop ///////
+    buttonInitSkipLoop();
+    //printf("2"); 
+    while(1) {
+        //printf("3"); 
+        if( buttonCanSkipLoop() ) return;
+        LifiRx_LoopOOK();
+        //printf("4"); 
+        wait_ms(50);
+    }
+}
+
+//////////////
+
+// https://os.mbed.com/blog/entry/Simplify-your-code-with-mbed-events/
+// create an event queue
+EventQueue queue;
+//#if USE_LIFI_RX_CHECK_QUEUE
+//EventQueue lifi_tx_check_queue(32 * EVENTS_EVENT_SIZE);
+//#endif
+
+void do_something() {
+  // this now runs in the context of eventThread, instead of in the ISR
+  //printf("!");
+  LifiRx_SampleSignalEdge();
+}
+
+
+
+void LifiRx_RunWithEventQueue()
+{
+   
+#if USE_LOCAL_TICKER
+    Ticker lifi_rx_timer;
+#endif
+
+  // create a thread that'll run the event queue's dispatch function
+  //Thread eventThread(osPriorityHigh);
+  Thread eventThread;
+  eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
+  
+//#if USE_LIFI_RX_CHECK_QUEUE  
+//  Thread lifiRxCheckThread(osPriorityNormal);
+//  lifiRxCheckThread.start(callback(&lifi_tx_check_queue, &EventQueue::dispatch_forever));
+//#endif
+    
+    printf("\n======== LifiRx_RunWithEventQueue =========\n");
+    LifiRx_Init();
+    printf("Tx Symbol Feq=%f sec / %d us \n",LIFI_RX_SYMBOL_PERIOD_SEC,LIFI_RX_SYMBOL_PERIOD_US);
+    printf("Rx Sample Feq=%f sec / %d us \n",LIFI_RX_SYMBOL_SAMPLE_PERIOD_SEC,LIFI_RX_SYMBOL_SAMPLE_PERIOD_US);
+    printf("Rx Sample/Symbol=%d times / adc edge threshod=%d mV \n",LIFI_RX_SAMPLE_PER_SYMBOL,LIFI_RX_EDGE_THRESHOLD);
+    printf("MIMO LIFI_MIMO_CHANNEL_NUM=%d  / LIFI_ACTUAL_MAX_CHANNEL_NUM=%d \n",LIFI_MIMO_CHANNEL_NUM,LIFI_ACTUAL_MAX_CHANNEL_NUM);
+    printf("Tx interval = %d sec \n",LIFI_DEMO_TX_PERIOD_SEC);
+    
+    lifi_rx_timer.attach(queue.event(&do_something), LIFI_RX_SYMBOL_SAMPLE_PERIOD_SEC);
+//    us_time = LIFI_RX_SYMBOL_SAMPLE_PERIOD_US;
+//    lifi_rx_timer.attach_us(&LifiRx_SampleSignalEdge, us_time);
+    //////// main loop ///////
+    buttonInitSkipLoop();
+    //printf("2"); 
+    while(1) {
+#if 1 //(USE_LIFI_RX_CHECK_QUEUE==0)   
+        //printf("3");      
+        if( buttonCanSkipLoop() ) return;
+        LifiRx_LoopOOK();
+        //printf("4"); 
+#endif        
+        wait_ms(50);
+    }
+}
+