Miguel Gallegos / Mbed OS Prueba_5

Dependencies:   TextLCD

Revision:
2:e4b49e7686c7
Parent:
1:9a5f06e7969e
diff -r 9a5f06e7969e -r e4b49e7686c7 main.cpp
--- a/main.cpp	Thu Feb 14 10:37:28 2019 +0000
+++ b/main.cpp	Tue May 07 05:44:08 2019 +0000
@@ -1,219 +1,176 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+
 #include "mbed.h"
 #include <stdio.h>
 #include <errno.h>
-
-// Block devices
-//#include "SPIFBlockDevice.h"
-//#include "DataFlashBlockDevice.h"
 #include "SDBlockDevice.h"
-//#include "HeapBlockDevice.h"
-
-// File systems
+#include "FATFileSystem.h"
+#include "TextLCD.h"
 
-//#include "LittleFileSystem.h"
-#include "FATFileSystem.h"
+float anselect;
+TextLCD lcd(D8, D9, D4, D5, D6, D7); // rs, e, d4-d7
+AnalogIn av_sensor(A1);
+AnalogIn av_select(A0);
+ 
+SDBlockDevice blockDevice (D11, D12, D13, D10);  // mosi, miso, sck, cs
+// File system declaration
+char fsistem[]="fs";
+char archivo1[] = "/fs/dato1.txt";
+char archivo2[] = "/fs/dato2.txt";
+char archivo3[] = "/fs/dato3.txt";
+char prueba[] = "/fs/prueba.txt";
 
-// Physical block device, can be any device that supports the BlockDevice API
-SDBlockDevice   blockDevice(p5, p6, p7, p8);  // mosi, miso, sck, cs
+const int tmuestra = 1500;           //tamaño de la muestra(tamaño del vector);
+float muestra[tmuestra];        //arreglo de la muestra en float;
+
+FATFileSystem   fileSystem(fsistem);
+int escribeSD(int n, float grab[]);
 
-// File system declaration
-FATFileSystem   fileSystem("fs");
+void inicio();                  //Abre o da el formato y crea los archiívos "w+"
+void grabacion();              //carga datos analogicos en muestra[tmuestra];
+void escriteSD(int n, float grab);//n es para seleccionar los archivos
+int iterpre();                      //interpreta los botones del shild LCD
 
-// Entry point for the example
 int main()
 {
-    printf("--- Mbed OS filesystem example ---\n");
-
-    // Try to mount the filesystem
-    printf("Mounting the filesystem... ");
-    fflush(stdout);
-
-    int err = fileSystem.mount(&blockDevice);
-    printf("%s\n", (err ? "Fail :(" : "OK"));
-    if (err) {
-        // Reformat if we can't mount the filesystem
-        // this should only happen on the first boot
-        printf("No filesystem found, formatting... ");
-        fflush(stdout);
-        err = fileSystem.reformat(&blockDevice);
-        printf("%s\n", (err ? "Fail :(" : "OK"));
-        if (err) {
-            error("error: %s (%d)\n", strerror(-err), err);
+        while(1){
+        //inicio();
+        int n = iterpre();
+        int e = 0;
+        lcd.locate(9,1);
+        lcd.printf("%d",n);
+        switch(n)
+        {
+            
+            case 0:
+                //lcd.locate(0,0);
+                //lcd.printf("esperando... ");
+                break;
+            case 1:
+                //lcd.locate(0,0);
+                //lcd.printf(" tomando muestra 1 ");
+                //grabacion();
+                //lcd.locate(0,0);
+                //lcd.printf(" grabando  muestra 1 ");
+                //escribeSD(1,muestra);
+                //lcd.cls();
+                break;
+                
+            case 2:
+                //grabacion();
+                //escribeSD(2,muestra);
+                break;
+            case 3:
+                //grabacion();
+                //escribeSD(3,muestra);
+                break;
+            case 4:
+                //grabacion();
+                //escribeSD(4,muestra);
+                break;
         }
     }
-
-    // Open the numbers file
-    printf("Opening \"/fs/numbers.txt\"... ");
-    fflush(stdout);
-
-    FILE*   f = fopen("/fs/numbers.txt", "r+");
-    printf("%s\n", (!f ? "Fail :(" : "OK"));
-    if (!f) {
-        // Create the numbers file if it doesn't exist
-        printf("No file found, creating a new file... ");
+}
+void grabacion(){
+    for (int i = 0; i < tmuestra; i++) {
+        muestra[i] = av_sensor.read();
+        printf("\r tomando muestra %f... \n ", muestra[i]);
         fflush(stdout);
-        f = fopen("/fs/numbers.txt", "w+");
-        printf("%s\n", (!f ? "Fail :(" : "OK"));
-        if (!f) {
-            error("error: %s (%d)\n", strerror(errno), -errno);
-        }
-
-        for (int i = 0; i < 10; i++) {
-            printf("\rWriting numbers (%d/%d)... ", i, 10);
-            fflush(stdout);
-            err = fprintf(f, "    %d\n", i);
-            if (err < 0) {
-                printf("Fail :(\n");
-                error("error: %s (%d)\n", strerror(errno), -errno);
-            }
-        }
-
-        printf("\rWriting numbers (%d/%d)... OK\n", 10, 10);
-
-        printf("Seeking file... ");
+    }
+    return;
+}
+void inicio(){
+    printf("Montanto el sistema de archivos... ");
+    fflush(stdout);
+    int err = fileSystem.reformat(&blockDevice);                                //formatéa si existe un error
+    printf("%s\n", (err ? "Fail :(" : "OK"));
+    FILE * f = fopen(archivo1, "w+");
+    err = fclose(f);
+    FILE * g = fopen(archivo2, "w+");
+    err = fclose(g);
+    FILE * h = fopen(archivo3, "w+");
+    err = fclose(h);
+    FILE * i = fopen(prueba, "w+");
+    err = fclose(i);
+    return;
+}
+int escribeSD(int n, float grab[]){
+    printf("Montanto el sistema de archivos... ");
+    fflush(stdout);
+    char ubica[]= "";
+    
+    switch(n)
+    {
+        case 0:
+            //ubica = prueba;
+            break;
+        case 1:
+            strcat(archivo1,ubica);
+            break;
+        case 2:
+            strcat(archivo2,ubica);
+            break;
+        case 3:
+            strcat(archivo3,ubica);
+            break;
+        case 4:
+            strcat(prueba,ubica);
+            break;
+    }
+    int err = fileSystem.mount(&blockDevice);                                   //monta el sistema de archivo                                   
+    FILE*   f = fopen(ubica, "r+");                                             //abre la ubicacion
+    for (int i = 0; i < tmuestra; i++) {
+        printf("\rWriting numbers (%d/%d)... ", i, tmuestra);
         fflush(stdout);
-        err = fseek(f, 0, SEEK_SET);
-        printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
+        err = fprintf(f, "%f\n", grab[i]);
         if (err < 0) {
+            printf("Fail :(\n");
             error("error: %s (%d)\n", strerror(errno), -errno);
         }
     }
-
-    // Go through and increment the numbers
-    for (int i = 0; i < 10; i++) {
-        printf("\rIncrementing numbers (%d/%d)... ", i, 10);
-        fflush(stdout);
-
-        // Get current stream position
-        long    pos = ftell(f);
-
-        // Parse out the number and increment
-        int32_t number;
-        fscanf(f, "%ld", &number);
-        number += 1;
-
-        // Seek to beginning of number
-        fseek(f, pos, SEEK_SET);
-
-        // Store number
-        fprintf(f, "    %ld\n", number);
-
-        // Flush between write and read on same file
-        fflush(f);
-    }
-
-    printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10);
-
-    // Close the file which also flushes any cached writes
-    printf("Closing \"/fs/numbers.txt\"... ");
+    printf("Seeking file... ");
     fflush(stdout);
-    err = fclose(f);
+    err = fseek(f, 0, SEEK_SET);
     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
-    if (err < 0) {
-        error("error: %s (%d)\n", strerror(errno), -errno);
-    }
-
-    // Display the root directory
-    printf("Opening the root directory... ");
-    fflush(stdout);
-
-    DIR*    d = opendir("/fs/");
-    printf("%s\n", (!d ? "Fail :(" : "OK"));
-    if (!d) {
-        error("error: %s (%d)\n", strerror(errno), -errno);
-    }
-
-    printf("root directory:\n");
-    while (true) {
-        struct dirent*  e = readdir(d);
-        if (!e) {
-            break;
-        }
-
-        printf("    %s\n", e->d_name);
-    }
-
-    printf("Closing the root directory... ");
-    fflush(stdout);
-    err = closedir(d);
-    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
-    if (err < 0) {
-        error("error: %s (%d)\n", strerror(errno), -errno);
+    err = fclose(f);
+    
+    return 0;
+}
+int iterpre()
+{
+    
+    
+    anselect = av_select.read();
+    float r1 = anselect;
+    
+    int resp = 0;
+    int r = r1*100;
+    lcd.locate(5,1);
+    lcd.printf("___");
+    lcd.locate(5,1);
+    lcd.printf("%d",r);
+    if((r < 9)) {
+        resp =  5;
+        return resp;
     }
-
-    // Display the numbers file
-    printf("Opening \"/fs/numbers.txt\"... ");
-    fflush(stdout);
-    f = fopen("/fs/numbers.txt", "r");
-    printf("%s\n", (!f ? "Fail :(" : "OK"));
-    if (!f) {
-        error("error: %s (%d)\n", strerror(errno), -errno);
+    if((47 < r)&&(r < 49)) {
+        resp =  4;
+        return resp;
     }
-
-    printf("numbers:\n");
-    while (!feof(f)) {
-        int c = fgetc(f);
-        printf("%c", c);
-    }
-
-    printf("\rClosing \"/fs/numbers.txt\"... ");
-    fflush(stdout);
-    err = fclose(f);
-    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
-    if (err < 0) {
-        error("error: %s (%d)\n", strerror(errno), -errno);
+    if((20 < r)&&(r < 24)) {
+        resp =  3;
+        return resp;
     }
-
-    // Tidy up
-    printf("Unmounting... ");
-    fflush(stdout);
-    err = fileSystem.unmount();
-    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
-    if (err < 0) {
-        error("error: %s (%d)\n", strerror(-err), err);
-    }
-    
-    printf("Initializing the block device... ");
-    fflush(stdout);
-
-    err = blockDevice.init();
-    printf("%s\n", (err ? "Fail :(" : "OK"));
-    if (err) {
-        error("error: %s (%d)\n", strerror(-err), err);
+    if((73 < r)&&(r < 76)) {
+        resp =  2;
+        return resp;
     }
-
-    printf("Erasing the block device... ");
-    fflush(stdout);
-    err = blockDevice.erase(0, blockDevice.size());
-    printf("%s\n", (err ? "Fail :(" : "OK"));
-    if (err) {
-        error("error: %s (%d)\n", strerror(-err), err);
+    if((r == 100)) {
+        resp =  0;
+        return resp;
     }
-
-    printf("Deinitializing the block device... ");
-    fflush(stdout);
-    err = blockDevice.deinit();
-    printf("%s\n", (err ? "Fail :(" : "OK"));
-    if (err) {
-        error("error: %s (%d)\n", strerror(-err), err);
+    if((99 < r)&&(r < 101)) {
+        resp =  1;
+        return resp;
     }
-    
-    printf("\r\n");
-    
-    printf("Mbed OS filesystem example done!\n");
-}
+    return resp;
+}
\ No newline at end of file