Ble for smart sOlutions

Dependencies:   Adafruit_WS2801

Revision:
9:92d861703f96
Parent:
7:9cda1b0f25ae
Child:
10:d845189d146e
diff -r 369b80cef5ae -r 92d861703f96 source/InterestService.cpp
--- a/source/InterestService.cpp	Tue May 21 13:57:27 2019 +0200
+++ b/source/InterestService.cpp	Tue Jun 11 11:16:55 2019 +0200
@@ -2,11 +2,12 @@
 // Created by kris on 7-5-19.
 //
 
+#include <wait_api.h>
 #include "InterestService.h"
 #pragma pack(push,1)
 struct packed_A {
     uint32_t magic;
-    int value[20];
+    int value[5];
 };
 #pragma pack(pop)
 
@@ -24,11 +25,11 @@
     printf("[PERIPHERAL] Adding interest service.. \r\n");
     GattCharacteristic *charTable[] = { &interestCharacteristic };
 
-    GattService colorService(
+    GattService interestService(
             CustomUUIDs::UUID_INTEREST_SERVICE,
             charTable,
             sizeof(charTable) / sizeof(GattCharacteristic *));
-    ble.gattServer().addService(colorService);
+    ble.gattServer().addService(interestService);
     ble.gattServer().onDataWritten(this, &InterestService::onDataWritten);
     readFromFs();
 
@@ -40,7 +41,7 @@
     uint16_t handle = writeParams->handle;
     printf("You wrote some data... But for which char? \r\n");
     printf("%d == %d", handle, interestCharacteristic.getValueHandle());//colorCharacteristic.getValueHandle());
-//    if(handle == interestCharacteristic.getValueHandle()){
+    if(handle == interestCharacteristic.getValueHandle()){
         printf("You wrote interests!");
         printf("The value should be an array of 20 integers.");
 //            color = writeParams->data;
@@ -50,12 +51,13 @@
             index++;
             printf("Index: %d, val %08X \r\n", index, interests[i]);
         }
+        printf("[PERIPHERAL]\t Writing that to the FS..\r\n");
         writeToFs();
-//    }
+    }
 }
 
 int InterestService::readFromFs() {
-    enum { MAGIC_VALUE = 0xDEADBEEF };
+    enum { MAGIC_VALUE = 0xC0FFEE20 };
     mbed::FlashIAP flash_device;
     flash_device.init();
     int sector_size = flash_device.get_sector_size(flash_device.get_flash_start() + flash_device.get_flash_size() - 1UL);
@@ -65,8 +67,8 @@
     printf("Page size: %d \r\n", page_size);
     printf("Address: %d \r\n", address);
 
-    int buffer[24];
-    flash_device.read(buffer, address, 24);
+    int buffer[5];
+    flash_device.read(buffer, address, 5);
     packed_A const *ptr = (packed_A const *) buffer;
 
     if(ptr->magic == MAGIC_VALUE) {
@@ -76,8 +78,11 @@
                ptr->value[3],
                ptr->value[4]);
 
-        for (int i=0; i < 5;  i++) {
-            interests[i] = ptr->value[i] << 24 | (ptr->value[i+1] & 0xff) << 16 | (ptr->value[i+2] & 0xff) << 8| (ptr->value[i+3] & 0xff);
+        for (int i=0; i < 20;  i++) {
+            interests[i] = int((unsigned char)(ptr->value[i]  &0xff) << 24 |
+                               (unsigned char)(ptr->value[i+1]&0xff) << 16 |
+                               (unsigned char)(ptr->value[i+2]&0xff) << 8 |
+                               (unsigned char)(ptr->value[i+3]&0xff));//ptr->value[i] << 24 | (ptr->value[i+1] & 0xff) << 16 | (ptr->value[i+2] & 0xff) << 8| (ptr->value[i+3] & 0xff);
         }
 
     } else {
@@ -85,7 +90,7 @@
         packed_A newmemory;
         newmemory.magic = MAGIC_VALUE;
         for (int i=0; i < 20;  i++) {
-            newmemory.value[i] = 0;
+            newmemory.value[i] = 2;
         }
         flash_device.erase(address, flash_device.get_sector_size(address));
         flash_device.program(&newmemory, address, sizeof(newmemory));
@@ -96,20 +101,30 @@
 
 
 int InterestService::writeToFs() {
-    enum { MAGIC_VALUE = 0xDEADBEEF };
+    enum { MAGIC_VALUE = 0xC0FFEE20 };
     mbed::FlashIAP flash_device;
     flash_device.init();
+    printf("[PERIPHERAL]\t Inited FS.. \r\n");
     packed_A newmemory;
     int sector_size = flash_device.get_sector_size(flash_device.get_flash_start() + flash_device.get_flash_size() - 1UL);
     int page_size = flash_device.get_page_size();
     int address = (flash_device.get_flash_start() + flash_device.get_flash_size()) - (sector_size);
+    printf("[PERIPHERAL]\t Ready?");
 
     newmemory.magic = MAGIC_VALUE;
-    for (int i=0; i < 20;  i++) {
+    for (int i=0; i < 5;  i++) {
+        printf("[PERIPHERAL]\t U wrote: %d", interests[i]);
         newmemory.value[i] = interests[i];
     }
 
+    printf("[PERIPHERAL]\t %d", sizeof(newmemory));
+    printf("[PERIPHERAL]\t %d", address);
+    printf("[PERIPHERAL]\t %d", sizeof(interests));
+
+
     flash_device.erase(address, flash_device.get_sector_size(address));
+    wait(0.2);
+//    return 0;
     flash_device.program(&newmemory, address, sizeof(newmemory));
     printf("Flashed");
     return 0;
@@ -120,6 +135,5 @@
 }
 
 InterestService::~InterestService() {
-
-    printf("[PERIPHERAL]\t Destructing InterestService");
+    printf("[PERIPHERAL]\t Destructing InterestService\r\n");
 }