working version of song control with initialization from sd card

Dependencies:   MFRC522 NRF2401P SDFileSystem SPI_TFT_ILI9341 TFT_fonts mbed

Fork of Song_Control by Malcolm McCulloch

Files at this revision

API Documentation at this revision

Comitter:
epgmdm
Date:
Sat Jan 23 00:14:37 2016 +0000
Child:
1:c2232b1eaf31
Commit message:
Basic structure - initialise

Changed in this revision

SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
locker.cpp Show annotated file Show diff for this revision Revisions of this file
locker.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
utils.cpp Show annotated file Show diff for this revision Revisions of this file
utils.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Sat Jan 23 00:14:37 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/neilt6/code/SDFileSystem/#d10a519c0910
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/locker.cpp	Sat Jan 23 00:14:37 2016 +0000
@@ -0,0 +1,43 @@
+/**
+* All the code associated to run the mbed as a locker.
+* Link with hub
+* Link with battery
+*/
+#include "mbed.h"
+#include "utils.h"
+#define debug 
+
+// Flags
+// Variables
+// tx nRF2401
+
+long long addrHub=0xBBBBBBBBBB;
+int channel = 76;
+
+// Initializaton
+/**
+* Initialise for a locker
+*/
+void initialiseLocker(FILE *fp){
+#ifdef debug
+    printf("Initialise Locker\n\r");
+#endif
+
+    // Read in hub address and channel
+    if (fscanf (fp,"%x %*c %*s",&channel )!=1) writeError("Locker config: cannot read channel");
+    if (fscanf (fp,"%llx %*c %*s",&addrHub )!=1) writeError("Locker config: cannot read hub address");
+
+#ifdef debug
+    printf("  Channel:%x, Hub Address %llx \n\r",channel, addrHub);
+#endif
+
+
+}
+// Interupt routines
+
+
+// Loop through slow routines
+
+void loopLocker(){
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/locker.h	Sat Jan 23 00:14:37 2016 +0000
@@ -0,0 +1,5 @@
+/**
+* All the code associated to run the mbed as a locker.
+*/
+void initialiseLocker(FILE *fp);
+void loopLocker();
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Jan 23 00:14:37 2016 +0000
@@ -0,0 +1,97 @@
+/**
+* This is a multi-purpose mbed. Duiring initialisation it reads the config file and then configures the mbed appropriately.
+* L Locker
+* H main Hub
+* B Battery
+*/
+#include "mbed.h"
+#include "SDFileSystem.h"
+#include "utils.h"
+#include "locker.h"
+#define debug 
+
+// Defines the type of mbed
+char type;
+unsigned int version = 1453505344; // Linux epoch time
+unsigned int sdVersion;
+int id;
+
+// Sd card
+SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
+
+
+/**
+* Reads config file and configures the mbed
+*/
+void initialise()
+{
+    char * name = "/sd/config.ini";
+    FILE *fp;
+
+    // Initialise the SD card
+    spiSD();
+    sd.large_frames(false);
+    sd.mount();
+    wait(0.1);
+#ifdef debug
+    printf("  SD MOUNTED Card type: %d  Capacity: %.1fMB\n\r", sd.card_type(),(sd.disk_sectors() * 512) / 1048576.0);
+#endif
+
+
+    //Configuration
+#ifdef debug
+    printf("Reading config file...\n\r");
+#endif
+    fp = fopen(name, "r");
+    if (fp == NULL) {
+        writeError ("Config file cannot be read");
+    }
+    if (fscanf (fp,"%c %*c %*s",&type )!=1) writeError("Config: cannot read type");
+    if (fscanf (fp,"%d %*c %*s",&sdVersion )!=1) writeError("Config: cannot read version");
+    if (fscanf (fp,"%d %*c %*s",&id )!=1) writeError("Config: cannot read id");
+#ifdef debug
+    printf("  Type:%c, Version %u, ID %d\n\r",type,sdVersion,id);
+#endif
+    switch (type){
+    case('B'): { // Battery
+            //initialiseBattery(fp);
+            break;
+        }
+    case('H'): { // Hub
+            //initialiseHub(fp);
+            break;
+        }
+    case('L'): { // Locker
+            initialiseLocker(fp);
+            break;
+        }
+    default:{
+    writeError ("Type declared in config not available.");
+    }
+        
+    }
+    fclose (fp);
+}
+
+/**
+* main loop. Set up timers and interupts then wait
+*/
+int main()
+{
+    struct tm * timeInf = localtime(&version);
+    printf("\n\r SOLAR NANO GRID CONTROLLER. Version %04d-%02d-%02d %02d:%02d \n\r", timeInf->tm_year+1900,timeInf->tm_mon+1,timeInf->tm_mday,timeInf->tm_hour, timeInf->tm_sec);
+    //
+    // Read config and initialise
+    //
+#ifdef debug
+    printf("\n\rInitialise \n\r");
+#endif
+    initialise();
+#ifdef debug
+    printf("\n\r> START LOOP\n\r");
+#endif
+    while (true) {
+        // do slower routines
+
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Jan 23 00:14:37 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/6f327212ef96
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/utils.cpp	Sat Jan 23 00:14:37 2016 +0000
@@ -0,0 +1,46 @@
+/**
+* Useful utils
+*/
+#include "mbed.h"
+#include "pinmap.h"
+#include "utils.h"
+
+DigitalOut ledError(LED_RED,ledOFF);
+/**
+* turns SPI on for Nrf
+*/
+void spiNrf()
+{
+    //sd.deselect();
+    pin_function(PTE1, 1);  //pin function 1 is GPIO
+    pin_function(PTD7, 7); //Set SD_MISO as SPI, this is the same as the last number in those tables
+}
+
+/**
+* Turns SPI on for SD card
+*/
+void spiSD()
+{
+    //sd.select();
+    pin_function(PTE1 , 7);  //Set SD_MISO as SPI, this is the same as the last number in those tables
+    pin_function(PTD7, 1); //pin function 1 is GPIO
+}
+
+/**
+* Flashes the red led and writes error message to stdout for ever.
+*/
+void writeError( char *msg ){
+ 
+    while (1){
+        ledError = ledON;
+        printf("ERROR: %s\n\r",msg);
+        wait (0.1);
+        ledError = ledOFF;
+        wait (0.5);
+          ledError = ledON;
+        wait (0.2);
+        ledError = ledOFF;
+        wait (3.9);
+    }
+    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/utils.h	Sat Jan 23 00:14:37 2016 +0000
@@ -0,0 +1,20 @@
+/**
+* useful utils.
+*/
+#define ledON 0
+#define ledOFF 1
+
+/**
+* turns SPI on for Nrf
+*/
+void spiNrf();
+
+/**
+* Turns SPI on for SD card
+*/
+void spiSD();
+
+/**
+* Flashes the red led and writes error message to stdout for ever.
+*/
+void writeError( char *msg );
\ No newline at end of file