Version of personalized IMPACT LoRa

Fork of LMiC-10secs by Alcatel-Lucent IoT Development

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers oslmic.cpp Source File

oslmic.cpp

00001 /*******************************************************************************
00002  * Copyright (c) 2014-2015 IBM Corporation.
00003  * All rights reserved. This program and the accompanying materials
00004  * are made available under the terms of the Eclipse Public License v1.0
00005  * which accompanies this distribution, and is available at
00006  * http://www.eclipse.org/legal/epl-v10.html
00007  *
00008  * Contributors:
00009  *    IBM Zurich Research Lab - initial API, implementation and documentation
00010  *******************************************************************************/
00011 
00012 #include "lmic.h"
00013 #include "x_nucleo_iks01a1.h"
00014 #include "debug.h"
00015 
00016 /* Instantiate the expansion board */
00017 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);
00018 
00019 /* Retrieve the composing elements of the expansion board */
00020 static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
00021 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
00022 static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
00023 static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
00024 static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
00025 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
00026 static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
00027 
00028 // RUNTIME STATE
00029 static struct {
00030     osjob_t* scheduledjobs;
00031     osjob_t* runnablejobs;
00032 } OS;
00033 
00034 void os_init () {
00035     memset(&OS, 0x00, sizeof(OS));
00036     hal_init();
00037     radio_init();
00038     LMIC_init();
00039 }
00040 
00041 ostime_t os_getTime () {
00042     return hal_ticks();
00043 }
00044 
00045 static u1_t unlinkjob (osjob_t** pnext, osjob_t* job) {
00046     for( ; *pnext; pnext = &((*pnext)->next)) {
00047         if(*pnext == job) { // unlink
00048             *pnext = job->next;
00049             return 1;
00050         }
00051     }
00052     return 0;
00053 }
00054 
00055 // clear scheduled job
00056 void os_clearCallback (osjob_t* job) {
00057     hal_disableIRQs();
00058     unlinkjob(&OS.scheduledjobs, job) || unlinkjob(&OS.runnablejobs, job);
00059     hal_enableIRQs();
00060 }
00061 
00062 // schedule immediately runnable job
00063 void os_setCallback (osjob_t* job, osjobcb_t cb) {
00064     osjob_t** pnext;
00065     hal_disableIRQs();
00066     // remove if job was already queued
00067     os_clearCallback(job);
00068     // fill-in job
00069     job->func = cb;
00070     job->next = NULL;
00071     // add to end of run queue
00072     for(pnext=&OS.runnablejobs; *pnext; pnext=&((*pnext)->next));
00073     *pnext = job;
00074     hal_enableIRQs();
00075 }
00076 
00077 // schedule timed job
00078 void os_setTimedCallback (osjob_t* job, ostime_t time, osjobcb_t cb) {
00079     osjob_t** pnext;
00080     hal_disableIRQs();
00081     // remove if job was already queued
00082     os_clearCallback(job);
00083     // fill-in job
00084     job->deadline = time;
00085     job->func = cb;
00086     job->next = NULL;
00087     // insert into schedule
00088     for(pnext=&OS.scheduledjobs; *pnext; pnext=&((*pnext)->next)) {
00089         //debug_str(".");
00090         if((*pnext)->deadline - time > 0) { // (cmp diff, not abs!)
00091             // enqueue before next element and stop
00092             //debug_str("Stop To Wait...\r\n");
00093             job->next = *pnext;
00094             break;
00095         }
00096     }
00097     *pnext = job;
00098     hal_enableIRQs();
00099 }
00100 
00101 /* Helper function for printing floats & doubles */
00102 static char *printDouble(char* str, double v, int decimalDigits=2)
00103 {
00104   int i = 1;
00105   int intPart, fractPart;
00106   int len;
00107   char *ptr;
00108 
00109   /* prepare decimal digits multiplicator */
00110   for (;decimalDigits!=0; i*=10, decimalDigits--);
00111 
00112   /* calculate integer & fractinal parts */
00113   intPart = (int)v;
00114   fractPart = (int)((v-(double)(int)v)*i);
00115 
00116   /* fill in integer part */
00117   sprintf(str, "%i.", intPart);
00118 
00119   /* prepare fill in of fractional part */
00120   len = strlen(str);
00121   ptr = &str[len];
00122 
00123   /* fill in leading fractional zeros */
00124   for (i/=10;i>1; i/=10, ptr++) {
00125     if(fractPart >= i) break;
00126     *ptr = '0';
00127   }
00128 
00129   /* fill in (rest of) fractional part */
00130   sprintf(ptr, "%i", fractPart);
00131 
00132   return str;
00133 }
00134 
00135 // execute jobs from timer and from run queue
00136 void os_runloop () {
00137 /*      uint8_t id;
00138       float value1, value2;
00139       char buffer1[32], buffer2[32];
00140       int32_t axes[3];
00141       
00142       debug_str("--- Starting new run ---\r\n");
00143     
00144       humidity_sensor->ReadID(&id);
00145       debug_str("HTS221  humidity & temperature    = ");
00146       debug_uint(id);
00147       debug_str("\r\n");
00148       pressure_sensor->ReadID(&id);
00149       debug_str("LPS25H  pressure & temperature    = ");
00150       debug_uint(id);
00151       debug_str("\r\n");
00152       magnetometer->ReadID(&id);
00153       debug_str("LIS3MDL magnetometer              = ");
00154       debug_uint(id);
00155       debug_str("\r\n");
00156       gyroscope->ReadID(&id);
00157       debug_str("LSM6DS0 accelerometer & gyroscope = ");
00158       debug_uint(id);
00159       debug_str("\r\n");*/
00160       
00161 //      wait(3);
00162 
00163       while(1) {
00164 /*            debug_str("\r\n");
00165         
00166             temp_sensor1->GetTemperature(&value1);
00167             humidity_sensor->GetHumidity(&value2);
00168             debug_str("HTS221: [temp] ");
00169             debug_str(printDouble(buffer1, value1));
00170             debug_str("°C,   [hum] ");
00171             debug_str(printDouble(buffer2, value2));
00172             debug_str("%\r\n");
00173             //pc.printf("HTS221: [temp] %7s°C,   [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
00174             
00175             temp_sensor2->GetFahrenheit(&value1);
00176             pressure_sensor->GetPressure(&value2);
00177             debug_str("LPS25H: [temp] ");
00178             debug_str(printDouble(buffer1, value1));
00179             debug_str("°F, [press] ");
00180             debug_str(printDouble(buffer2, value2));
00181             debug_str("mbar\r\n");
00182             //pc.printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
00183         
00184             debug_str("---\r\n");
00185         
00186             magnetometer->Get_M_Axes(axes);
00187             debug_str("LIS3MDL [mag/mgauss]:  ");
00188             debug_uint(axes[0]);
00189             debug_str(", ");
00190             debug_uint(axes[1]);
00191             debug_str(", ");
00192             debug_uint(axes[2]);
00193             debug_str("\r\n");
00194             //pc.printf("LIS3MDL [mag/mgauss]:  %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00195         
00196             accelerometer->Get_X_Axes(axes);
00197             debug_str("LSM6DS0 [acc/mg]:      ");
00198             debug_uint(axes[0]);
00199             debug_str(", ");
00200             debug_uint(axes[1]);
00201             debug_str(", ");
00202             debug_uint(axes[2]);
00203             debug_str("\r\n");
00204             //pc.printf("LSM6DS0 [acc/mg]:      %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00205         
00206             gyroscope->Get_G_Axes(axes);
00207             debug_str("LSM6DS0 [gyro/mdps]:   ");
00208             debug_uint(axes[0]);
00209             debug_str(", ");
00210             debug_uint(axes[1]);
00211             debug_str(", ");
00212             debug_uint(axes[2]);
00213             debug_str("\r\n");
00214             //pc.printf("LSM6DS0 [gyro/mdps]:   %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);*/
00215 
00216             osjob_t* j = NULL;
00217             hal_disableIRQs();
00218             // check for runnable jobs
00219             if(OS.runnablejobs) {
00220                 j = OS.runnablejobs;
00221                 OS.runnablejobs = j->next;
00222             } else if(OS.scheduledjobs && hal_checkTimer(OS.scheduledjobs->deadline)) { // check for expired timed jobs
00223                 j = OS.scheduledjobs;
00224                 OS.scheduledjobs = j->next;
00225             } else { // nothing pending
00226                 hal_sleep(); // wake by irq (timer already restarted)
00227             }
00228             hal_enableIRQs();
00229             if(j) { // run job callback
00230                 j->func(j);
00231             }
00232       }
00233 }