Wireless / Mbed 2 deprecated spi_buttons_leds

Dependencies:   mbed nRF24L01P

Revision:
1:cedd9360f5c7
Parent:
0:c2ab34dc9018
Child:
3:74f042e3ec10
--- a/main.cpp	Tue Dec 04 11:09:22 2018 +0000
+++ b/main.cpp	Tue Dec 04 13:02:47 2018 +0000
@@ -4,65 +4,58 @@
 SPI spi(D11, D12, D13);
 DigitalOut cs(D10, 1);
 
-void green() {
-    cs = 1;
-    cs = 0;
-    spi.write(0x46);
-    spi.write(0x09);
-    spi.write(0x15);
-    cs = 1;
-}
-
-void red() {
-    cs = 1;
-    cs = 0;
-    spi.write(0x46);
-    spi.write(0x09);
-    spi.write(0x2A);
-    cs = 1;
-}
-
-void read() {
-    cs = 0;
-    
-    spi.write(0x47);  
-    spi.write(0x09);
-    int data = spi.write(0x01);
-    cs = 1;
-    
-    printf("Data: %d\n\r",(data));
-    
-    //if((data & 0x00) == 0x80){
-//        green();
-//    } else {
-//        red();
-//    }
-}
-
-int main() {    
-    // alles aan: 01000110 00000000 00000000
-    // alles uit: 01000110 00000000 11111111
-    // Chip must be deselected
-    printf("Initializing. \n\r");
-    cs = 1;
- 
-    // Select the device by seting chip select low
+void init() {
     cs = 0;
  
-    // Send 0x8f, the command to read the WHOAMI register
-    spi.write(0x46);
-    spi.write(0x00);
+    spi.write(0x46); // Select IODIR
+    spi.write(0x00); // 0000 = Set all to output
     spi.write(0x00);
   
     // Deselect the device
     cs = 1;
+}
+
+void setLed(int i) {
+    cs = 0;
+    spi.write(0x46);
+    spi.write(0x09);
+    
+    int mask = 1;
+    int writeValue = 255;
+    writeValue ^= mask << i;
+    spi.write(writeValue);
+    cs = 1;
+}
+
+int readButton() {
+    cs = 0;
+    spi.write(0x47);
+    spi.write(0x09);
+    int misoOutput = spi.write(0x00);
+    cs = 1;
+    
+    misoOutput = misoOutput & 192;
+    printf("output miso: %d", misoOutput);
+    if (misoOutput == 128) { // S1 pressed
+        return 1;    
+    } else if (misoOutput == 64) { // S2 pressed
+        return 5;
+    }
+    return 0;
+}
+
+int main() {    
+    cs = 1;
+    printf("Initializing. \n\r");
+    init();
+    printf("Setting Led. \n\r");
+    
+    // Select the device by seting chip select low
+    int led = 0;
     printf("Starting \n\r");
     while (true) {
-//        green();
-        read();
-//        wait(1);
-//        red();
-//        read();
-        wait(0.25);
+        setLed(led);
+        led = (led + readButton()) % 6;
+        wait(0.1);
     }
 }
\ No newline at end of file