temp logg

Files at this revision

API Documentation at this revision

Comitter:
glsfacom
Date:
Sun Nov 01 14:44:37 2020 -0400
Parent:
5:c2811b726e63
Commit message:
escrevendo csv no SD

Changed in this revision

Mlx90615.cpp 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
--- a/Mlx90615.cpp	Sun Oct 11 09:39:59 2020 -0400
+++ b/Mlx90615.cpp	Sun Nov 01 14:44:37 2020 -0400
@@ -1,7 +1,8 @@
+
 #include "Mlx90615.h"
-#include "crc8.h"
 
-I2C i2c(p28, p27);//I2C_SDA, I2C_SCL 
+MbedCRC<POLY_8BIT_CCITT , 8>ct;
+
 
 Mlx90615::Mlx90615(int quant):
     n_mlx{quant}
@@ -38,14 +39,14 @@
     char cmd = 0x27;
 	Mlx90615::i2c.start();
 	ACK = Mlx90615::i2c.write(mlx_addr<<1);
-	if(!ACK) return -2;
+	if(!ACK) return -1;
     ACK = Mlx90615::i2c.write(addr);
-	if(!ACK) return -3;
+	if(!ACK) return -1;
 	Mlx90615::i2c.start();
     ACK = Mlx90615::i2c.write((mlx_addr<<1)|1);
-	if(!ACK) return -4;
+	if(!ACK) return -1;
     read[0] = Mlx90615::i2c.read(ACK);
-    if(!ACK) return -5;
+    if(!ACK) return -1;
     read[1] = Mlx90615::i2c.read(ACK);
     Mlx90615::i2c.read(ACK);
 	float temp = (float((read[1] << 8) | read[0]));
@@ -67,7 +68,7 @@
 		temp = _read(RAM, 0x07, mlx_addr);
         cont++;
 	}
-	return temp;
+	return temp*0.02 - 273.15;
 }
 
 void 
@@ -75,7 +76,6 @@
 {
     for(int i = 0; i < n_mlx; i++)
         temps[i] = read_temperature(i+1);
-    printf("%f\n",temps[0]);
 }
 
 void 
@@ -83,22 +83,25 @@
 {
 	unsigned char addr = EEPROM + reg_addr;
 	char bytes[4];
+    uint32_t mcrc;
     unsigned char crc, vh, vl;
 	vh = value >> 8;
 	vl = value;
-	bytes[0] = 0xB6;
+	bytes[0] = 0x00;
 	bytes[1] = addr;
 	bytes[2] = vl;
 	bytes[3] = vh;
-	crc = crc8(bytes, 4);
+	ct.compute((void*)bytes, 4, &mcrc);
 
+    crc = mcrc & 0xff;
     Mlx90615::i2c.start();
-    Mlx90615::i2c.write(mlx_addr<<1);
+    Mlx90615::i2c.write(0x00<<1);
 	Mlx90615::i2c.write(addr);
 	Mlx90615::i2c.write(vl);
     Mlx90615::i2c.write(vh);
 	Mlx90615::i2c.write(crc);
     Mlx90615::i2c.stop();
+    ThisThread::sleep_for(300);
 }
 
 void 
@@ -107,9 +110,12 @@
 }
 
 void 
-Mlx90615::write_eeprom_address(unsigned char reg_addr, uint16_t data, unsigned char mlx_addr){
-	_write(reg_addr, 0x0000, mlx_addr);
-    _write(reg_addr, data, mlx_addr);
+Mlx90615::write_eeprom_address(unsigned char reg_addr, uint16_t data, unsigned char mlx_addr)
+{
+    if(reg_addr>= 0x04 && reg_addr <=0x0d)
+        return;
+    Mlx90615::erase_eeprom_address(reg_addr, mlx_addr);
+    Mlx90615::_write(reg_addr, data, mlx_addr);
 }
 
 void 
--- a/main.cpp	Sun Oct 11 09:39:59 2020 -0400
+++ b/main.cpp	Sun Nov 01 14:44:37 2020 -0400
@@ -4,16 +4,16 @@
 #include "Mlx90615.h"
 #include "TMP117.h"
 #include "PinDetect.h"
+#include <cstdio>
  
 #define LOG_DELAY 1 //seconds
 #define TMP_QUANT 1 // MAX = 4
-#define MLX_QUANT 1 // MAX = 127
-#define MLX_SETUP 0 // 1 or 0
+#define MLX_QUANT 1 // MAX = 127 (NEED TO BE CORRECT ON SETUP)
+#define MLX_SETUP 0 // 1=Setup or 0=LOG
 
 
 DigitalOut myled(LED1);
 DigitalOut led2(LED2);
-PinDetect button(p11);
 
 int volatile quit = 0;
 
@@ -27,17 +27,19 @@
 main()
 {
     // set_time(1595446700 - 3600*4); // set time to 22/07/2020 15:38:20 @CGMS
-    // Use internal pullup for pushbutton
-    button.mode(PullUp);
-    // Delay for initial pullup to take effect
-    ThisThread::sleep_for(10);
-    // Setup Interrupt callback function for a button hit
-    button.attach_deasserted(&buttonISR);
-    // Start sampling button input using interrupts
-    button.setSampleFrequency();
+    
     
     if(MLX_SETUP)
     {
+        PinDetect button(p11);
+        // Use internal pullup for pushbutton
+        button.mode(PullUp);
+        // Delay for initial pullup to take effect
+        ThisThread::sleep_for(10);
+        // Setup Interrupt callback function for a button hit
+        button.attach_deasserted(&buttonISR);
+        // Start sampling button input using interrupts
+        button.setSampleFrequency(); 
         Mlx90615 mlx{MLX_QUANT};
         int lastbutton = quit;
         for(int i = 0; i < MLX_QUANT; i++)
@@ -46,18 +48,26 @@
             while(lastbutton==quit);
             led2=0;
             lastbutton = quit;
+            printf("write slave addr %d\n", i+1);
             mlx.write_eeprom_address(0x00, (uint16_t)i+1, 0x00); //all mlx will ACK to 0x00
-            ThisThread::sleep_for(300);
         }
     }
     else
     {
-        double mtemps[127];
+        printf("Entering log mode\n");
         while (!quit) 
         {   
             //Os objetos que bloqueiam sleep(I2C, SPI) não estão declarados, então o sleep no começo deveria funcionar
             ThisThread::sleep_for(LOG_DELAY*1000);
-
+            PinDetect button(p11);
+            // Use internal pullup for pushbutton
+            button.mode(PullUp);
+            // Delay for initial pullup to take effect
+            ThisThread::sleep_for(10);
+            // Setup Interrupt callback function for a button hit
+            button.attach_deasserted(&buttonISR);
+            // Start sampling button input using interrupts
+            button.setSampleFrequency(); 
 
             Mlx90615 mlx{MLX_QUANT};
             TMP117 tmp(0x48);
@@ -86,7 +96,7 @@
             }
             printf("SD mount done.\n");
 
-            FILE *fp = fopen("/fs/log.txt","a+");
+            FILE *fp = fopen("/fs/log.csv","a+");
             if(fp == nullptr)
             {
                 printf("Could not open file\n");
@@ -102,21 +112,19 @@
             printf("%s",ctime(&seconds));
             printf("{\nTMP:\n");
             fprintf(fp, "%s",ctime(&seconds));
-            fprintf(fp, "{\nTMP:\n");
             for(int i = 0; i < TMP_QUANT; i++)
             {
                 printf("\t%d: %.2f ,", i, tmp.temps[i]);
-                fprintf(fp, "\t%d: %.2f ,", i, tmp.temps[i]);
+                fprintf(fp, ", %.2f", tmp.temps[i]);
             }
             printf("\nMLX:\n");
-            fprintf(fp, "\nMLX:\n");
             for(int i = 0; i < MLX_QUANT; i++)
             {
                 printf("\t%d: %.2f ,", i, mlx.temps[i]);
-                fprintf(fp, "\t%d: %.2f ,", i, mlx.temps[i]);
+                fprintf(fp, ", %.2f", mlx.temps[i]);
             }
             printf("\n}\n");
-            fprintf(fp,"\n}\n");
+            fprintf(fp,"\n");
             
             fflush(fp);
             myled = 0;