for helge just for testing

Dependencies:   Freetronics_16x2_LCD mbed-rtos mbed

Fork of Freetronics_16x2_LCD by Shields

Revision:
4:c0b34f33643f
Parent:
2:4e0ed7dc6420
--- a/main.cpp	Fri Feb 13 09:48:28 2015 +0000
+++ b/main.cpp	Sun Aug 14 09:53:28 2016 +0000
@@ -18,25 +18,117 @@
 
 #include "mbed.h"
 #include "freetronicsLCDShield.h"
+#include "rtos.h"
 
-freetronicsLCDShield lcd(D8, D9, D4, D5, D6, D7, D3, A0);
+#include "keys.h"
+
+float clock_s() { return us_ticker_read() / 1000000.0f; }
+uint64_t clock_ms() { return us_ticker_read() / 1000; }
+uint64_t clock_us() { return us_ticker_read(); }
 
-int main() {
+//freetronicsLCDShield::freetronicsLCDShield (PinName rs, PinName e, PinName d0, PinName d1, PinName d2, PinName d3, PinName bl, PinName a0) 
+//freetronicsLCDShield lcd(D12, D11, D5, D4, D3, D2, D9, A0);
+freetronicsLCDShield lcd(D8, D9, D4, D5, D6, D7, D10, A0);
+
+DigitalOut led2(LED2);
+Serial pc(SERIAL_TX, SERIAL_RX);
+
+void lcdThread (void const *arg)
+{
+    char text[16];
     // turn on the back light (it's off by default)
     lcd.setBackLight(true);
-    
     // print the first line and wait 3 sec
-    lcd.printf("mbed application");
+    write_lcd(0,0,"schorschs");
     wait(3);
     
     // print the counter prefix; the number will be printed in the while loop
-    lcd.setCursorPosition(1, 0);
-    lcd.printf("counter");
-    
+    write_lcd(1, 0,"cnt:");
     int i=1;
     while (i++) {
-        lcd.setCursorPosition(1, 8);
-        lcd.printf("%d", i);
-        wait(0.001);
+        sprintf (text, "%4d", (i/100) % 10000);
+        write_lcd (1, 4, text);
+        Thread::wait(1);
+    }
+}
+/*
+ * thread safe positioning and writing
+ */
+Mutex lcdMtx;
+void write_lcd (uint8_t x, uint8_t y, char *txt)
+{
+    lcdMtx.lock(0);
+    lcd.setCursorPosition(x, y);
+    lcd.printf("%s", txt);
+    lcdMtx.unlock();
+}
+    
+/*
+ * LED blinkt zufaellig
+ */
+void ledThread (void const *arg)
+{
+    uint8_t ison = 0;
+    while (1) {
+        
+        Thread::wait(1);
+        if ((clock_ms() % 100) == 1)
+            ison ^= 1;
+        led2 = ison;
+        //pc.printf("LED%d\n",  cnt++);
     }
 }
+
+#define VOLTAGE_KEY_0 0.0f       //right
+#define VOLTAGE_KEY_1 0.125f     //up
+#define VOLTAGE_KEY_2 0.277f     //dn
+#define VOLTAGE_KEY_3 0.411f     //left
+#define VOLTAGE_KEY_4 0.578f     //select
+#define VOLTAGE_KEY_IDLE 0.760f
+static void keypressed_callback (key_t *k)
+{
+    if (NULL == k)
+        return;
+    pc.printf ("%s pressed\n", k->name);
+    write_lcd (1, 10, k->name);
+}
+static void keyreleased_callback (key_t *k)
+{
+    if (NULL == k)
+        return;
+    pc.printf ("%s released\n", k->name);
+    write_lcd (1, 10, "      ");
+}
+
+static void init_keys (void)
+{
+    keys_init(
+        A0, 20, (float [6]){
+        VOLTAGE_KEY_0,
+        VOLTAGE_KEY_1,
+        VOLTAGE_KEY_2,
+        VOLTAGE_KEY_3,
+        VOLTAGE_KEY_4,
+        VOLTAGE_KEY_IDLE
+        } 
+    );
+    register_key_callback (0, KEYCB_PRESS, keypressed_callback);
+    register_key_callback (1, KEYCB_PRESS, keypressed_callback);
+    register_key_callback (2, KEYCB_PRESS, keypressed_callback);
+    register_key_callback (3, KEYCB_PRESS, keypressed_callback);
+    register_key_callback (4, KEYCB_PRESS, keypressed_callback);
+    register_key_callback (0, KEYCB_RELEASE, keyreleased_callback);
+    register_key_callback (1, KEYCB_RELEASE, keyreleased_callback);
+    register_key_callback (2, KEYCB_RELEASE, keyreleased_callback);
+    register_key_callback (3, KEYCB_RELEASE, keyreleased_callback);
+    register_key_callback (4, KEYCB_RELEASE, keyreleased_callback);
+}
+int main() {
+    init_keys();
+
+    Thread lcdT(lcdThread);
+    Thread ledT(ledThread);
+
+    lcdT.join();
+    ledT.join();
+}