fork of seeed studio 4-digit display for st nucleo board

Dependencies:   Data_Clock_Pair Seeed_Chainable_LED Seeed_Four_Digit_Disp Seeed_IR_Temp_Sensor Seeed_Led_Bar

Fork of Seeed_Grove_4_Digit_Display_Clock by Seeed

Revision:
4:d540dccad60a
Parent:
3:c4427ce4d171
Child:
5:d28207a2d2a6
--- a/main.cpp	Mon Apr 10 20:40:05 2017 -0600
+++ b/main.cpp	Tue Apr 11 08:47:25 2017 -0600
@@ -1,9 +1,12 @@
-#include "mbed.h"
-
+#include <algorithm>
+#include <cmath>
+#include "mbed.h"
+
 Serial pc(SERIAL_TX, SERIAL_RX);
 DigitalOut clk(PE_9);
 DigitalOut dat(PE_11);
-DigitalOut   led(LED1);
+DigitalOut   led(LED1);
+AnalogIn knob(PA_3);
 
 // Avoid name conflict
 #define LED_GLB_CMDMODE 0x00  // Work on 8-bit mode
@@ -13,32 +16,32 @@
 // Send 16 bits of data
 void sendData(unsigned int data)
 {
-  for (int i = 0; i < 16; i++)
-  {
-      wait_ms(1);
-    unsigned int state = (data & 0x8000) ? 1 : 0;
-    dat = state;
+    for (int i = 0; i < 16; i++)
+    {
+        wait_us(100);
+        unsigned int state = (data & 0x8000) ? 1 : 0;
+        dat = state;
 
-    wait_ms(1);
-    clk = !clk;
+        wait_us(100);
+        clk = !clk;
 
-    data <<= 1;
-  }
+        data <<= 1;
+    }
 }
 
 // Send the latch command
 void latchData()
 {
-dat = 0;
-  wait_ms(10);
+    dat = 0;
+    wait_ms(1);
 
-  for (int i = 0; i < 4; i++)
-  {
-      wait_ms(1);
-    dat=1;
-    wait_ms(1);
-    dat=0;
-  }
+    for (int i = 0; i < 4; i++)
+    {
+        wait_us(100);
+        dat = 1;
+        wait_us(100);
+        dat = 0;
+    }
 }
 
 // each element in the state will hold the brightness level
@@ -49,38 +52,52 @@
 void setData(int __state[])
 {
 
-  sendData(LED_GLB_CMDMODE);
-  for (int i = 0; i < 10; i++)
-  {
-	  // Go forward on __state
-      sendData(__state[i]);
-  }
+    sendData(LED_GLB_CMDMODE);
+    for (int i = 0; i < 10; i++)
+    {
+        // Go forward on __state
+        sendData(__state[i]);
+    }
 
-  // Two extra empty bits for padding the command to the correct length
-  sendData(0x00);
-  sendData(0x00);
+    // Two extra empty bits for padding the command to the correct length
+    sendData(0x00);
+    sendData(0x00);
 
-  latchData();
+    latchData();
 }
 
 
 
-int main() {
+int main()
+{
     pc.printf("\n\nstarting algorithm\n\n");
     int led_bar[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
     int not_led_bar[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
+    int knob_led_bar[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
     setData(led_bar);
-    /* display.setBrightness(7); */
-    /* display.write(100); */
-    /* display.write(5); */
-    /* display.write(hour * 100 + minute);
 */
-    /* ticker.attach(tick, 0.5);
 */
-    while(1) {
+    while (1)
+    {
+        float led_frac = knob.read();
+        pc.printf("knob is %f\n", led_frac);
+        int led_percent = floor(led_frac * 100);
+        int tens = floor(led_frac * 10);
+        pc.printf("percent is %i\n", led_percent);
+        int ones = led_percent % 10;
+        ones = floor(ones * 10 / 8);
+        if (ones > 8) ones = 8;
+        int ones_led = ones * 16;
+        pc.printf("tens is %i\n", tens);
+        for (int i = 0; i < 10; ++i) knob_led_bar[i] = 0x00;
+        for (int i = 0; i < tens; ++i)
+            knob_led_bar[i] = 0xff;
+        knob_led_bar[tens] = ones_led;
         setData(led_bar);
-        wait(0.5);
+        wait_ms(100);
+        setData(knob_led_bar);
+        wait_ms(100);
         setData(not_led_bar);
         pc.printf("iteration\n");
-        led = !led;
-        wait(2);
-    }
-}
+        led = !led;
+        wait_ms(100);
+    }
+}