tinyshell demo example

Dependencies:   mbed tinyshell MMA8451Q mbed-rtos tsi_sensor cc1101

Demonstrate how to implement commands to test all hardware avaliable in the board using shell commands.

TSI Sensor percentage

MMA8451Q Accelerometer values with RGB Led PWM.

mbed-RTOS test all features.

Revision:
2:c0591e9eb622
Parent:
1:e1b8d88fa26d
Child:
3:87a19cb69c25
--- a/main.cpp	Tue Mar 11 05:00:41 2014 +0000
+++ b/main.cpp	Tue Mar 11 05:02:06 2014 +0000
@@ -1,6 +1,6 @@
 #include "mbed.h"
 #include "rtos.h"
-#include "tinysh.h" 
+#include "tinysh.h"
 
 Serial pc(USBTX, USBRX);
 
@@ -10,17 +10,19 @@
 ////////////////////////////////////////////////////////////////
 /////// RTOS TESTING ///////////////////////////////////////////
 
-Mutex stdio_mutex; 
- 
-void notify(const char* name, int state) {
+Mutex stdio_mutex;
+
+void notify(const char* name, int state)
+{
     stdio_mutex.lock();
     printf("%s: %d\n\r", name, state);
     stdio_mutex.unlock();
 }
 
 Semaphore two_slots(2);
- 
-void test_thread(void const *name) {
+
+void test_thread(void const *name)
+{
     while (true) {
         two_slots.wait();
         printf("%s\n\r", (const char*)name);
@@ -28,53 +30,57 @@
         two_slots.release();
     }
 }
- 
+
 DigitalOut led(LED1);
- 
-void led_thread(void const *argument) {
+
+void led_thread(void const *argument)
+{
     while (true) {
         // Signal flags that are reported as event are automatically cleared.
         Thread::signal_wait(0x1);
         led = !led;
     }
 }
- 
- void led_send(){
-      Thread thread(led_thread);
-    
+
+void led_send()
+{
+    Thread thread(led_thread);
+
     while (true) {
         Thread::wait(1000);
         thread.signal_set(0x1);
     }
- } 
- 
+}
+
 
 typedef struct {
     float    voltage;   /* AD result of measured voltage */
     float    current;   /* AD result of measured current */
     uint32_t counter;   /* A counter value               */
 } message_t;
- 
+
 MemoryPool<message_t, 16> mpool;
 Queue<message_t, 16> queue;
- 
+
 /* Send Thread */
-void send_thread (void const *args) {
+void send_thread (void const *args)
+{
     uint32_t i = 0;
     while (true) {
         i++; // fake data update
         message_t *message = mpool.alloc();
-        message->voltage = (i * 0.1) * 33; 
+        message->voltage = (i * 0.1) * 33;
         message->current = (i * 0.1) * 11;
         message->counter = i;
         queue.put(message);
         Thread::wait(1000);
     }
 }
- 
-int main3 (void) {
+
+int main3 (void)
+{
     Thread thread(send_thread);
-    
+
     while (true) {
         osEvent evt = queue.get();
         if (evt.status == osEventMessage) {
@@ -82,41 +88,43 @@
             printf("\nVoltage: %.2f V\n\r"   , message->voltage);
             printf("Current: %.2f A\n\r"     , message->current);
             printf("Number of cycles: %u\n\r", message->counter);
-            
+
             mpool.free(message);
         }
     }
-} 
- 
+}
+
 
 
 
 
 /* Mail */
 typedef struct {
-  float    voltage; /* AD result of measured voltage */
-  float    current; /* AD result of measured current */
-  uint32_t counter; /* A counter value               */
+    float    voltage; /* AD result of measured voltage */
+    float    current; /* AD result of measured current */
+    uint32_t counter; /* A counter value               */
 } mail_t;
- 
+
 Mail<mail_t, 16> mail_box;
- 
-void send_thread2 (void const *args) {
+
+void send_thread2 (void const *args)
+{
     uint32_t i = 0;
     while (true) {
         i++; // fake data update
         mail_t *mail = mail_box.alloc();
-        mail->voltage = (i * 0.1) * 33; 
+        mail->voltage = (i * 0.1) * 33;
         mail->current = (i * 0.1) * 11;
         mail->counter = i;
         mail_box.put(mail);
         Thread::wait(1000);
     }
 }
- 
-int main4 (void) {
+
+int main4 (void)
+{
     Thread thread(send_thread2);
-    
+
     while (true) {
         osEvent evt = mail_box.get();
         if (evt.status == osEventMail) {
@@ -124,13 +132,13 @@
             printf("\nVoltage: %.2f V\n\r"   , mail->voltage);
             printf("Current: %.2f A\n\r"     , mail->current);
             printf("Number of cycles: %u\n\r", mail->counter);
-            
+
             mail_box.free(mail);
         }
     }
 }
 
- ////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////
@@ -140,51 +148,52 @@
 #include "MMA8451Q.h"
 
 #if   defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
-  PinName const SDA = PTE25;
-  PinName const SCL = PTE24;
+PinName const SDA = PTE25;
+PinName const SCL = PTE24;
 #elif defined (TARGET_KL05Z)
-  PinName const SDA = PTB4;
-  PinName const SCL = PTB3;
+PinName const SDA = PTB4;
+PinName const SCL = PTB3;
 #else
-  #error TARGET NOT DEFINED
+#error TARGET NOT DEFINED
 #endif
 
 #define MMA8451_I2C_ADDRESS (0x1d<<1)
 
-    PwmOut rled(LED1);
-    PwmOut gled(LED2);
-    PwmOut bled(LED3);
+PwmOut rled(LED1);
+PwmOut gled(LED2);
+PwmOut bled(LED3);
 
-   MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
+MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
 
-void kl25z_read_accel(int argc, char **argv){
-        float x, y, z;
-        
-        x = rled = 1.0 - abs(acc.getAccX());
-        y = gled = 1.0 - abs(acc.getAccY());
-        z = bled = 1.0 - abs(acc.getAccZ());
-        pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\r\n", x, y, z);
+void kl25z_read_accel(int argc, char **argv)
+{
+    float x, y, z;
+
+    x = rled = 1.0 - abs(acc.getAccX());
+    y = gled = 1.0 - abs(acc.getAccY());
+    z = bled = 1.0 - abs(acc.getAccZ());
+    pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\r\n", x, y, z);
 }
 
-tinysh_cmd_t cmd_accel={0,"accel","read ACCEL accel","[args]",kl25z_read_accel,0,0,0};
+tinysh_cmd_t cmd_accel= {0,"accel","read ACCEL accel","[args]",kl25z_read_accel,0,0,0};
 
- ////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////
 /////// HARDWARE TEST KL25 TSI       ///////////////////////////
- 
- 
+
+
 #include "tsi_sensor.h"
 /* This defines will be replaced by PinNames soon */
 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
-  #define ELEC0 9
-  #define ELEC1 10
+#define ELEC0 9
+#define ELEC1 10
 #elif defined (TARGET_KL05Z)
-  #define ELEC0 9
-  #define ELEC1 8
+#define ELEC0 9
+#define ELEC1 8
 #else
-  #error TARGET NOT DEFINED
+#error TARGET NOT DEFINED
 #endif
 
 TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
@@ -194,7 +203,7 @@
     pc.printf("tsi=%f\r\n",tsi.readPercentage());
 }
 
-tinysh_cmd_t cmd_tsi={0,"tsi","read TSI sensor value","[args]",kl25z_read_tsi,0,0,0};
+tinysh_cmd_t cmd_tsi= {0,"tsi","read TSI sensor value","[args]",kl25z_read_tsi,0,0,0};
 
 ////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////
@@ -206,13 +215,12 @@
 
 void foo_fnt(int argc, char **argv)
 {
-  printf("foo command called\r\n");
-  for(int i=0;i<argc;i++)
-    {
-      printf("argv[%d]=\"%s\"\r\n",i,argv[i]);
+    printf("foo command called\r\n");
+    for(int i=0; i<argc; i++) {
+        printf("argv[%d]=\"%s\"\r\n",i,argv[i]);
     }
 }
-tinysh_cmd_t myfoocmd={0,"foo","foo command","[args]",foo_fnt,0,0,0};
+tinysh_cmd_t myfoocmd= {0,"foo","foo command","[args]",foo_fnt,0,0,0};
 
 
 
@@ -221,23 +229,23 @@
 
 void tinysh_char_out(unsigned char c)
 {
-  pc.putc(c);
+    pc.putc(c);
 }
 
-void tinyshell_thread(void const *args) {
-  pc.baud(115200);
-  pc.printf("kl25z tiny shell build %s %s\r\n",__DATE__,__TIME__);
-  tinysh_set_prompt("$ ");
-  tinysh_add_command(&myfoocmd);
-  tinysh_add_command(&cmd_tsi);
-  tinysh_add_command(&cmd_accel);
-  
-  
-  
-  
-  while(true)
-    {
-      tinysh_char_in( pc.getc() );
+void tinyshell_thread(void const *args)
+{
+    pc.baud(115200);
+    pc.printf("kl25z tiny shell build %s %s\r\n",__DATE__,__TIME__);
+    tinysh_set_prompt("$ ");
+    tinysh_add_command(&myfoocmd);
+    tinysh_add_command(&cmd_tsi);
+    tinysh_add_command(&cmd_accel);
+
+
+
+
+    while(true) {
+        tinysh_char_in( pc.getc() );
     }
 
 }
@@ -248,7 +256,7 @@
 ////////////////////////////////////////////////////////////////
 
 
- 
+
 
 
 
@@ -256,10 +264,11 @@
 
 
 
-int main() {
-    
+int main()
+{
+
     //
     Thread thread_shell(tinyshell_thread);
-    
+
     Thread::wait(osWaitForever);
 }