Discover the secret life of plants! FLORANIUM FDRM-KL25Z Shield An experimental Polygraph for green plants. www.floranion.de

Dependencies:   FastPWM SPI_TFT_ILI9341 TFT_fonts

SOME BELIEVE PLANTS CAN FEEL OUR PRESENCE...

The FLORANIUM PROJECT is an innovative plant life indicator, similar to a humans polygraph, that allow you to visualize activities of living plants, caused by movement, touch, changes in climate, environment and many other elements of life. It's a whole new way to interact with green plants!

Please visit http://www.floranium.com or http://www.floranion.de for more information.

/media/uploads/lasmahei/screenshot_2018-02-14_13-35-22.png/media/uploads/lasmahei/floranium.png

Committer:
lasmahei
Date:
Wed Feb 14 11:55:45 2018 +0000
Revision:
0:356e8d3c2cb7
Child:
1:f95f37892056
Discover the secret life of plants! FLORANIUM FDRM-KL25Z Shield An experimental Polygraph for green plants. www.floranion.de

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lasmahei 0:356e8d3c2cb7 1 /*******************************************************************************************
lasmahei 0:356e8d3c2cb7 2 FLORANION SCI SHIELD for FRDM KL25Z REV2.00
lasmahei 0:356e8d3c2cb7 3 Discover the secret life of plants!
lasmahei 0:356e8d3c2cb7 4 (c) Martin Heine - Light Art Vision 2018
lasmahei 0:356e8d3c2cb7 5
lasmahei 0:356e8d3c2cb7 6 This program is free software: you can redistribute it and/or modify
lasmahei 0:356e8d3c2cb7 7 it under the terms of the GNU General Public License as published by
lasmahei 0:356e8d3c2cb7 8 the Free Software Foundation, either version 3 of the License, or
lasmahei 0:356e8d3c2cb7 9 (at your option) any later version.
lasmahei 0:356e8d3c2cb7 10
lasmahei 0:356e8d3c2cb7 11 This program is distributed in the hope that it will be useful,
lasmahei 0:356e8d3c2cb7 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
lasmahei 0:356e8d3c2cb7 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
lasmahei 0:356e8d3c2cb7 14 GNU General Public License for more details.
lasmahei 0:356e8d3c2cb7 15
lasmahei 0:356e8d3c2cb7 16 You should have received a copy of the GNU General Public License
lasmahei 0:356e8d3c2cb7 17 along with this program. If not, see <http://www.gnu.org/licenses/>
lasmahei 0:356e8d3c2cb7 18 ********************************************************************************************/
lasmahei 0:356e8d3c2cb7 19 #include <mbed.h>
lasmahei 0:356e8d3c2cb7 20 #include <FastPWM.h>
lasmahei 0:356e8d3c2cb7 21 #include <SDBlockDevice.h>
lasmahei 0:356e8d3c2cb7 22 #include <FATFileSystem.h>
lasmahei 0:356e8d3c2cb7 23 #include "SPI_TFT_ILI9341.h"
lasmahei 0:356e8d3c2cb7 24 #include "Arial12x12.h"
lasmahei 0:356e8d3c2cb7 25 #include "Arial24x23.h"
lasmahei 0:356e8d3c2cb7 26 #include "Arial28x28.h"
lasmahei 0:356e8d3c2cb7 27 #include "font_big.h"
lasmahei 0:356e8d3c2cb7 28
lasmahei 0:356e8d3c2cb7 29 #define firmware 2.00
lasmahei 0:356e8d3c2cb7 30 #define RGBmask 0xFF
lasmahei 0:356e8d3c2cb7 31 #define dacMask 0xFFF
lasmahei 0:356e8d3c2cb7 32 #define samples 100
lasmahei 0:356e8d3c2cb7 33 #define samplesOut 1000
lasmahei 0:356e8d3c2cb7 34 #define baudrate 115000
lasmahei 0:356e8d3c2cb7 35 #define QVGA true //enter "false" here if no TFT Disülay is connected
lasmahei 0:356e8d3c2cb7 36
lasmahei 0:356e8d3c2cb7 37 /************************************** Inits ***************************************/
lasmahei 0:356e8d3c2cb7 38 // I/Os
lasmahei 0:356e8d3c2cb7 39 AnalogIn ain(PTB0); // Amplified and filtered Plant-Signal
lasmahei 0:356e8d3c2cb7 40 AnalogIn ain0(PTB1); // Non amplified absolute voltage level / skin resistance level.
lasmahei 0:356e8d3c2cb7 41
lasmahei 0:356e8d3c2cb7 42 //LED Bar for Plants absolute voltage level / skin resistance level.
lasmahei 0:356e8d3c2cb7 43 DigitalOut barDot3(PTC9);
lasmahei 0:356e8d3c2cb7 44 DigitalOut barDot2(PTC8);
lasmahei 0:356e8d3c2cb7 45 DigitalOut barDot1(PTA5);
lasmahei 0:356e8d3c2cb7 46 DigitalOut barDot0(PTA4);
lasmahei 0:356e8d3c2cb7 47 DigitalOut barDot7(PTA12);
lasmahei 0:356e8d3c2cb7 48 DigitalOut barDot6(PTD4);
lasmahei 0:356e8d3c2cb7 49 DigitalOut barDot5(PTC17);
lasmahei 0:356e8d3c2cb7 50 DigitalOut barDot4(PTC16);
lasmahei 0:356e8d3c2cb7 51 DigitalIn Record(PTB8); //SD Card Data Record Switch
lasmahei 0:356e8d3c2cb7 52 PwmOut pwmAnalogOut(PTA13); // PWM out for Analog Voltage.
lasmahei 0:356e8d3c2cb7 53 AnalogOut aout(PTE30); // Analog Out (DAC)
lasmahei 0:356e8d3c2cb7 54
lasmahei 0:356e8d3c2cb7 55 //Using FastPWM for higher speed and resolution. Otherwise, the RGB LED will flicker
lasmahei 0:356e8d3c2cb7 56 //RGB LED on the FRDM KL25Z board
lasmahei 0:356e8d3c2cb7 57 FastPWM r(LED_RED);
lasmahei 0:356e8d3c2cb7 58 FastPWM g(LED_GREEN);
lasmahei 0:356e8d3c2cb7 59 FastPWM b(LED_BLUE);
lasmahei 0:356e8d3c2cb7 60
lasmahei 0:356e8d3c2cb7 61 //External RGB LEDs
lasmahei 0:356e8d3c2cb7 62 FastPWM extRed(PTE20);
lasmahei 0:356e8d3c2cb7 63 FastPWM extGreen(PTE21);
lasmahei 0:356e8d3c2cb7 64 FastPWM extBlue(PTE29);
lasmahei 0:356e8d3c2cb7 65
lasmahei 0:356e8d3c2cb7 66 // SD-Card Pins
lasmahei 0:356e8d3c2cb7 67 SDBlockDevice sd(PTE1, PTE3, PTE2, PTE4); // mosi,miso,sck,cs
lasmahei 0:356e8d3c2cb7 68 FATFileSystem fs("sd");
lasmahei 0:356e8d3c2cb7 69
lasmahei 0:356e8d3c2cb7 70 // QVGA 2.2 TFT SPI 240x320 Color Display
lasmahei 0:356e8d3c2cb7 71 SPI_TFT_ILI9341 TFT(PTC6, PTC7, PTC5, PTC4, PTC0, PTC3, "TFT"); // mosi, miso, sclk, cs, reset, DC/RS
lasmahei 0:356e8d3c2cb7 72 DigitalOut LCD_LED(PTC12); // the display has a backlight switch on board
lasmahei 0:356e8d3c2cb7 73
lasmahei 0:356e8d3c2cb7 74 Serial pc(USBTX, USBRX);
lasmahei 0:356e8d3c2cb7 75
lasmahei 0:356e8d3c2cb7 76 Thread thread;
lasmahei 0:356e8d3c2cb7 77
lasmahei 0:356e8d3c2cb7 78 /************************************* Global Variables ******************************/
lasmahei 0:356e8d3c2cb7 79 long int plant, plant0, tmp, dacOut;;
lasmahei 0:356e8d3c2cb7 80 short int plantRGB;
lasmahei 0:356e8d3c2cb7 81 double pwmNormVal;
lasmahei 0:356e8d3c2cb7 82 float colorRange = 255;
lasmahei 0:356e8d3c2cb7 83 float Volt;
lasmahei 0:356e8d3c2cb7 84 int i; float bl; unsigned int upDown = 0;
lasmahei 0:356e8d3c2cb7 85 bool plantConnected, fileOpen, newFile = 1;
lasmahei 0:356e8d3c2cb7 86 bool rec = false;
lasmahei 0:356e8d3c2cb7 87 bool noLogo;
lasmahei 0:356e8d3c2cb7 88 bool SDOK;
lasmahei 0:356e8d3c2cb7 89 //char filename[64];
lasmahei 0:356e8d3c2cb7 90 int n = 0;
lasmahei 0:356e8d3c2cb7 91 short unsigned y=16, yStart=16, yold = y, plantRGBold, plantBit4, plantBit4old, msg=0;
lasmahei 0:356e8d3c2cb7 92 float dacRange = 4095;
lasmahei 0:356e8d3c2cb7 93
lasmahei 0:356e8d3c2cb7 94 /**************************************** Functions **********************************/
lasmahei 0:356e8d3c2cb7 95 void barDot(short unsigned level)
lasmahei 0:356e8d3c2cb7 96 {
lasmahei 0:356e8d3c2cb7 97 barDot0 = 0;
lasmahei 0:356e8d3c2cb7 98 barDot1 = 0;
lasmahei 0:356e8d3c2cb7 99 barDot2 = 0;
lasmahei 0:356e8d3c2cb7 100 barDot3 = 0;
lasmahei 0:356e8d3c2cb7 101 barDot4 = 0;
lasmahei 0:356e8d3c2cb7 102 barDot5 = 0;
lasmahei 0:356e8d3c2cb7 103 barDot6 = 0;
lasmahei 0:356e8d3c2cb7 104 barDot7 = 0;
lasmahei 0:356e8d3c2cb7 105
lasmahei 0:356e8d3c2cb7 106 switch (level)
lasmahei 0:356e8d3c2cb7 107 {
lasmahei 0:356e8d3c2cb7 108 case 0: barDot0 = 1; break;
lasmahei 0:356e8d3c2cb7 109 case 1: barDot1 = 1; break;
lasmahei 0:356e8d3c2cb7 110 case 2: barDot2 = 1; break;
lasmahei 0:356e8d3c2cb7 111 case 3: barDot3 = 1; break;
lasmahei 0:356e8d3c2cb7 112 case 4: barDot4 = 1; break;
lasmahei 0:356e8d3c2cb7 113 case 5: barDot5 = 1; break;
lasmahei 0:356e8d3c2cb7 114 case 6: barDot6 = 1; break;
lasmahei 0:356e8d3c2cb7 115 case 7: barDot7 = 1; break;
lasmahei 0:356e8d3c2cb7 116 }
lasmahei 0:356e8d3c2cb7 117 }
lasmahei 0:356e8d3c2cb7 118
lasmahei 0:356e8d3c2cb7 119 void initPWMs()
lasmahei 0:356e8d3c2cb7 120 {
lasmahei 0:356e8d3c2cb7 121 r.period_ms(10); // RGB LED PWM Frequency
lasmahei 0:356e8d3c2cb7 122 g.period_ms(10);
lasmahei 0:356e8d3c2cb7 123 b.period_ms(10);
lasmahei 0:356e8d3c2cb7 124
lasmahei 0:356e8d3c2cb7 125 extRed.period_ms(10); // RGB extern PWM Frequency
lasmahei 0:356e8d3c2cb7 126 extGreen.period_ms(10);
lasmahei 0:356e8d3c2cb7 127 extBlue.period_ms(10);
lasmahei 0:356e8d3c2cb7 128
lasmahei 0:356e8d3c2cb7 129 pwmAnalogOut.period_us(20); //50kHz
lasmahei 0:356e8d3c2cb7 130 //pwmAnalogOut.period_ms(5); // PWM out for Analog Voltage.
lasmahei 0:356e8d3c2cb7 131 }
lasmahei 0:356e8d3c2cb7 132
lasmahei 0:356e8d3c2cb7 133 void fadeBlue()
lasmahei 0:356e8d3c2cb7 134 {
lasmahei 0:356e8d3c2cb7 135 r=1;
lasmahei 0:356e8d3c2cb7 136 g=1;
lasmahei 0:356e8d3c2cb7 137 if (!upDown) i++; else i--;
lasmahei 0:356e8d3c2cb7 138 bl = (1/colorRange)*i;
lasmahei 0:356e8d3c2cb7 139 b=1-bl;
lasmahei 0:356e8d3c2cb7 140 if (i >=256) upDown = 1;
lasmahei 0:356e8d3c2cb7 141 if ((i == 0) && (upDown == 1)) upDown = 0;
lasmahei 0:356e8d3c2cb7 142 }
lasmahei 0:356e8d3c2cb7 143
lasmahei 0:356e8d3c2cb7 144 void TFTinit()
lasmahei 0:356e8d3c2cb7 145 {
lasmahei 0:356e8d3c2cb7 146 TFT.claim(stdout); // send stdout to the TFT display
lasmahei 0:356e8d3c2cb7 147 TFT.set_orientation(1);
lasmahei 0:356e8d3c2cb7 148 TFT.background(Black); // set background to black
lasmahei 0:356e8d3c2cb7 149 TFT.foreground(White); // set chars to white
lasmahei 0:356e8d3c2cb7 150 TFT.cls(); // clear the screen
lasmahei 0:356e8d3c2cb7 151 LCD_LED = 1; // backlite on
lasmahei 0:356e8d3c2cb7 152 }
lasmahei 0:356e8d3c2cb7 153
lasmahei 0:356e8d3c2cb7 154 void TFTwelcome()
lasmahei 0:356e8d3c2cb7 155 {
lasmahei 0:356e8d3c2cb7 156 TFT.foreground(Cyan);
lasmahei 0:356e8d3c2cb7 157 TFT.set_font((unsigned char*) Arial24x23);
lasmahei 0:356e8d3c2cb7 158 TFT.locate(80,100);
lasmahei 0:356e8d3c2cb7 159 TFT.printf("Welcome to");
lasmahei 0:356e8d3c2cb7 160 TFT.locate(85,125);
lasmahei 0:356e8d3c2cb7 161 TFT.printf("Floranium");
lasmahei 0:356e8d3c2cb7 162 wait(2);
lasmahei 0:356e8d3c2cb7 163 TFT.foreground(White);
lasmahei 0:356e8d3c2cb7 164 TFT.set_font((unsigned char*) Arial12x12);
lasmahei 0:356e8d3c2cb7 165 TFT.locate(20,200);
lasmahei 0:356e8d3c2cb7 166 TFT.printf("FLORANIUM Firmware Version %f",firmware);
lasmahei 0:356e8d3c2cb7 167 wait(1);
lasmahei 0:356e8d3c2cb7 168 TFT.cls(); // clear the screen
lasmahei 0:356e8d3c2cb7 169 TFT.rect(15,0,320,225,LightGrey);
lasmahei 0:356e8d3c2cb7 170 TFT.foreground(White);
lasmahei 0:356e8d3c2cb7 171 }
lasmahei 0:356e8d3c2cb7 172
lasmahei 0:356e8d3c2cb7 173 void data2TFT()
lasmahei 0:356e8d3c2cb7 174 {
lasmahei 0:356e8d3c2cb7 175 if (msg == 1)
lasmahei 0:356e8d3c2cb7 176 {
lasmahei 0:356e8d3c2cb7 177 TFT.cls();
lasmahei 0:356e8d3c2cb7 178 TFT.rect(15,0,320,225,LightGrey);
lasmahei 0:356e8d3c2cb7 179 yold = yStart;
lasmahei 0:356e8d3c2cb7 180 y = yStart;
lasmahei 0:356e8d3c2cb7 181 msg = 0;
lasmahei 0:356e8d3c2cb7 182 }
lasmahei 0:356e8d3c2cb7 183
lasmahei 0:356e8d3c2cb7 184 plantBit4 = (plant & 0xFF00); // Avoid line at rollover from 255 -> 1 and 1 > 255
lasmahei 0:356e8d3c2cb7 185 TFT.line(y+1,1,y+1,224,Cyan); // traveling blue line
lasmahei 0:356e8d3c2cb7 186 TFT.line(y,1,y,224,Black);
lasmahei 0:356e8d3c2cb7 187 if (y == 319) TFT.line(y,1,y,224,DarkGrey);
lasmahei 0:356e8d3c2cb7 188 if (plantBit4 - plantBit4old == 0) TFT.line(yold,plantRGBold/1.13,y,(RGBmask-plantRGB)/1.13,GreenYellow); // x-y to x-y
lasmahei 0:356e8d3c2cb7 189
lasmahei 0:356e8d3c2cb7 190 plantRGBold = RGBmask-plantRGB; //invert so curve on display goes up when plantRGB increases
lasmahei 0:356e8d3c2cb7 191 yold = y;
lasmahei 0:356e8d3c2cb7 192 y++;
lasmahei 0:356e8d3c2cb7 193 plantBit4old = plantBit4; // to avoid line on display when upper or lower border reached.
lasmahei 0:356e8d3c2cb7 194
lasmahei 0:356e8d3c2cb7 195 // Circle Bar on left side for overall voltage level
lasmahei 0:356e8d3c2cb7 196 TFT.fillcircle(6,6,5,DarkGrey);
lasmahei 0:356e8d3c2cb7 197 TFT.fillcircle(6,219,5,DarkGrey);
lasmahei 0:356e8d3c2cb7 198 TFT.fillrect(0,6,12,219,DarkGrey);
lasmahei 0:356e8d3c2cb7 199 TFT.fillcircle(6,219-(plant/310),5,GreenYellow);
lasmahei 0:356e8d3c2cb7 200
lasmahei 0:356e8d3c2cb7 201 // bottom ADC and voltage values
lasmahei 0:356e8d3c2cb7 202 TFT.foreground(White);
lasmahei 0:356e8d3c2cb7 203 // TFT.locate(0,228);
lasmahei 0:356e8d3c2cb7 204 // TFT.printf("ADC %5d", plant);
lasmahei 0:356e8d3c2cb7 205 if (!Record)
lasmahei 0:356e8d3c2cb7 206 {
lasmahei 0:356e8d3c2cb7 207 if (SDOK)
lasmahei 0:356e8d3c2cb7 208 {
lasmahei 0:356e8d3c2cb7 209 TFT.fillcircle(6,230,5,Red); // Red Dot for recording
lasmahei 0:356e8d3c2cb7 210 }
lasmahei 0:356e8d3c2cb7 211 else
lasmahei 0:356e8d3c2cb7 212 {
lasmahei 0:356e8d3c2cb7 213 TFT.fillcircle(6,230,5,Yellow); // Yello Dot SD Card failure (not present)
lasmahei 0:356e8d3c2cb7 214 }
lasmahei 0:356e8d3c2cb7 215 }
lasmahei 0:356e8d3c2cb7 216 else
lasmahei 0:356e8d3c2cb7 217 {
lasmahei 0:356e8d3c2cb7 218 TFT.fillcircle(6,230,5,DarkGrey);
lasmahei 0:356e8d3c2cb7 219 }
lasmahei 0:356e8d3c2cb7 220 TFT.locate(16,228);
lasmahei 0:356e8d3c2cb7 221 TFT.printf("Udisp %6.4fV", Volt);
lasmahei 0:356e8d3c2cb7 222 TFT.locate(130,228);
lasmahei 0:356e8d3c2cb7 223 TFT.printf("RGB %3d", plantRGB);
lasmahei 0:356e8d3c2cb7 224 TFT.locate(225,228);
lasmahei 0:356e8d3c2cb7 225 TFT.printf("Uin %6.4fV", (plant0/65535.0)*3.3);
lasmahei 0:356e8d3c2cb7 226
lasmahei 0:356e8d3c2cb7 227 // when curve reaches end of display, start from the beginning
lasmahei 0:356e8d3c2cb7 228 if (y >= 319)
lasmahei 0:356e8d3c2cb7 229 {
lasmahei 0:356e8d3c2cb7 230 yold = yStart;
lasmahei 0:356e8d3c2cb7 231 y = yStart;
lasmahei 0:356e8d3c2cb7 232 TFT.rect(15,0,320,225,LightGrey);
lasmahei 0:356e8d3c2cb7 233 }
lasmahei 0:356e8d3c2cb7 234 }
lasmahei 0:356e8d3c2cb7 235
lasmahei 0:356e8d3c2cb7 236 void adc2rgb_thread()
lasmahei 0:356e8d3c2cb7 237 {
lasmahei 0:356e8d3c2cb7 238 unsigned short i;
lasmahei 0:356e8d3c2cb7 239 long long sample, sample0;
lasmahei 0:356e8d3c2cb7 240 float color,red,green,blue;
lasmahei 0:356e8d3c2cb7 241
lasmahei 0:356e8d3c2cb7 242 while (1)
lasmahei 0:356e8d3c2cb7 243 {
lasmahei 0:356e8d3c2cb7 244 barDot(plant0/8192); // LED Bargraph
lasmahei 0:356e8d3c2cb7 245
lasmahei 0:356e8d3c2cb7 246 for (i=1; i<=samples; i++)
lasmahei 0:356e8d3c2cb7 247 {
lasmahei 0:356e8d3c2cb7 248 sample += ain.read_u16(); // read filtered and amplified plant signal ADC Value
lasmahei 0:356e8d3c2cb7 249 sample0 += ain0.read_u16(); // read plant DC level ADC Value
lasmahei 0:356e8d3c2cb7 250 wait_us(100);
lasmahei 0:356e8d3c2cb7 251 }
lasmahei 0:356e8d3c2cb7 252
lasmahei 0:356e8d3c2cb7 253 plant=sample/samples; // Averaging
lasmahei 0:356e8d3c2cb7 254 plant0 = sample0/samples;
lasmahei 0:356e8d3c2cb7 255
lasmahei 0:356e8d3c2cb7 256 sample = 0;
lasmahei 0:356e8d3c2cb7 257 sample0= 0;
lasmahei 0:356e8d3c2cb7 258 pwmNormVal = (plant/65535.0);
lasmahei 0:356e8d3c2cb7 259 Volt = pwmNormVal*3.3;
lasmahei 0:356e8d3c2cb7 260
lasmahei 0:356e8d3c2cb7 261 if (Volt <= 3.2) // Only if a green plant is connected!
lasmahei 0:356e8d3c2cb7 262 {
lasmahei 0:356e8d3c2cb7 263 plantConnected = true;
lasmahei 0:356e8d3c2cb7 264 plantRGB = (plant & RGBmask); // mask LSB Bit 0-4 for RGB LED Display
lasmahei 0:356e8d3c2cb7 265
lasmahei 0:356e8d3c2cb7 266 // ********************* RGB LED Color mixing
lasmahei 0:356e8d3c2cb7 267 color = (1/colorRange)*plantRGB;
lasmahei 0:356e8d3c2cb7 268 if(plantRGB >= 0 && plantRGB <= 85 ) { red = 1-(color*3); green = color*3; blue = 0; }
lasmahei 0:356e8d3c2cb7 269 if(plantRGB > 85 && plantRGB <= 170) { red = 0; green = 2-(color*3); blue = (color*3)-1; }
lasmahei 0:356e8d3c2cb7 270 if(plantRGB > 170 && plantRGB <= 255) { red = (color*3)-2; green = 0; blue = (1-color)*3; }
lasmahei 0:356e8d3c2cb7 271
lasmahei 0:356e8d3c2cb7 272 //FRDM-KL25Z RGB LED
lasmahei 0:356e8d3c2cb7 273 r=1-red;
lasmahei 0:356e8d3c2cb7 274 g=1-green;
lasmahei 0:356e8d3c2cb7 275 b=1-blue;
lasmahei 0:356e8d3c2cb7 276
lasmahei 0:356e8d3c2cb7 277 //PWM Output RGB EXT (Shield Connector J19)
lasmahei 0:356e8d3c2cb7 278 extRed=1-red;
lasmahei 0:356e8d3c2cb7 279 extGreen=1-green;
lasmahei 0:356e8d3c2cb7 280 extBlue=1-blue;
lasmahei 0:356e8d3c2cb7 281 //aout = (1/colorRange)*plantRGB; // DAC Analog Out (J18)
lasmahei 0:356e8d3c2cb7 282 //pwmAnalogOut = pwmNormVal; // PWM out for Analog Voltage (PWM: J15 and DC J12) Please Comment out RGB Color Display above if this PWM or/and Analog Output is used.
lasmahei 0:356e8d3c2cb7 283 }
lasmahei 0:356e8d3c2cb7 284 else
lasmahei 0:356e8d3c2cb7 285 {
lasmahei 0:356e8d3c2cb7 286 fadeBlue();
lasmahei 0:356e8d3c2cb7 287 plantConnected = false;
lasmahei 0:356e8d3c2cb7 288 }
lasmahei 0:356e8d3c2cb7 289 }
lasmahei 0:356e8d3c2cb7 290 }
lasmahei 0:356e8d3c2cb7 291
lasmahei 0:356e8d3c2cb7 292 void data2serial()
lasmahei 0:356e8d3c2cb7 293 {
lasmahei 0:356e8d3c2cb7 294 short unsigned bar,j;
lasmahei 0:356e8d3c2cb7 295 pc.printf("%5d %6.5f %3d ", plant, Volt, plantRGB);
lasmahei 0:356e8d3c2cb7 296 bar = (plant & 0xFF)/8;
lasmahei 0:356e8d3c2cb7 297 for (j=1; j<=bar; j++) pc.printf(" ");
lasmahei 0:356e8d3c2cb7 298 pc.printf("*\n\r");
lasmahei 0:356e8d3c2cb7 299 }
lasmahei 0:356e8d3c2cb7 300
lasmahei 0:356e8d3c2cb7 301 bool data2SDCard()
lasmahei 0:356e8d3c2cb7 302 {
lasmahei 0:356e8d3c2cb7 303 char filename[64];
lasmahei 0:356e8d3c2cb7 304
lasmahei 0:356e8d3c2cb7 305 if (newFile)
lasmahei 0:356e8d3c2cb7 306 {
lasmahei 0:356e8d3c2cb7 307 mkdir("/sd/data", 0777);
lasmahei 0:356e8d3c2cb7 308 while(1)
lasmahei 0:356e8d3c2cb7 309 {
lasmahei 0:356e8d3c2cb7 310 sprintf(filename, "/sd/data/flrm%04d.csv", n); // construct the filename fileNNN.csv
lasmahei 0:356e8d3c2cb7 311 FILE *fp = fopen(filename, "r"); // try and open it
lasmahei 0:356e8d3c2cb7 312 if(fp == NULL)
lasmahei 0:356e8d3c2cb7 313 {
lasmahei 0:356e8d3c2cb7 314 break; // if not found, we're done!
lasmahei 0:356e8d3c2cb7 315 }
lasmahei 0:356e8d3c2cb7 316 fclose(fp); // close the file
lasmahei 0:356e8d3c2cb7 317 n++; // and try the next one
lasmahei 0:356e8d3c2cb7 318 }
lasmahei 0:356e8d3c2cb7 319 pc.printf("Data are logged to file: %s...\n\r", filename);
lasmahei 0:356e8d3c2cb7 320 newFile = 0;
lasmahei 0:356e8d3c2cb7 321 }
lasmahei 0:356e8d3c2cb7 322 FILE *fp = fopen(filename, "a");
lasmahei 0:356e8d3c2cb7 323 if(fp == NULL)
lasmahei 0:356e8d3c2cb7 324 {
lasmahei 0:356e8d3c2cb7 325 pc.printf("Could not open File for write. SD Card present?\n\r");
lasmahei 0:356e8d3c2cb7 326 return 0;
lasmahei 0:356e8d3c2cb7 327 }
lasmahei 0:356e8d3c2cb7 328 fprintf(fp,"%5d,%6.4f,%3d\n\r", plant, Volt, plantRGB);
lasmahei 0:356e8d3c2cb7 329 fclose(fp);
lasmahei 0:356e8d3c2cb7 330 return 1;
lasmahei 0:356e8d3c2cb7 331 }
lasmahei 0:356e8d3c2cb7 332
lasmahei 0:356e8d3c2cb7 333 /************************************ Main Program **********************************/
lasmahei 0:356e8d3c2cb7 334 int main()
lasmahei 0:356e8d3c2cb7 335 {
lasmahei 0:356e8d3c2cb7 336 bool SDmounted;
lasmahei 0:356e8d3c2cb7 337
lasmahei 0:356e8d3c2cb7 338 pc.baud(baudrate);
lasmahei 0:356e8d3c2cb7 339 initPWMs();
lasmahei 0:356e8d3c2cb7 340 pc.printf("Welcome to \033[32mFLORANIUM \033[0m Firmware Vers. %3.2f (c)Light Art Vision 2018\n\n\r", firmware);
lasmahei 0:356e8d3c2cb7 341 if (QVGA) TFTinit();
lasmahei 0:356e8d3c2cb7 342 if (QVGA) TFTwelcome();
lasmahei 0:356e8d3c2cb7 343
lasmahei 0:356e8d3c2cb7 344 thread.start(adc2rgb_thread);
lasmahei 0:356e8d3c2cb7 345 while(1)
lasmahei 0:356e8d3c2cb7 346 {
lasmahei 0:356e8d3c2cb7 347 data2serial();
lasmahei 0:356e8d3c2cb7 348 if (QVGA) data2TFT();
lasmahei 0:356e8d3c2cb7 349 if (!Record)
lasmahei 0:356e8d3c2cb7 350 {
lasmahei 0:356e8d3c2cb7 351 if (!SDmounted) // Mount SD-Card if not already mounted
lasmahei 0:356e8d3c2cb7 352 {
lasmahei 0:356e8d3c2cb7 353 sd.init();
lasmahei 0:356e8d3c2cb7 354 fs.mount(&sd);
lasmahei 0:356e8d3c2cb7 355 SDmounted = true;
lasmahei 0:356e8d3c2cb7 356 pc.printf("SD Card mounted\n\r");
lasmahei 0:356e8d3c2cb7 357
lasmahei 0:356e8d3c2cb7 358 }
lasmahei 0:356e8d3c2cb7 359 SDOK = data2SDCard();
lasmahei 0:356e8d3c2cb7 360 }
lasmahei 0:356e8d3c2cb7 361 else
lasmahei 0:356e8d3c2cb7 362 {
lasmahei 0:356e8d3c2cb7 363 if (SDmounted) // Unmount SD-Card if not already unmounted
lasmahei 0:356e8d3c2cb7 364 {
lasmahei 0:356e8d3c2cb7 365 fs.unmount();
lasmahei 0:356e8d3c2cb7 366 sd.deinit();
lasmahei 0:356e8d3c2cb7 367 SDmounted = false;
lasmahei 0:356e8d3c2cb7 368 pc.printf("SD Card unmounted. You can remove it safely!\n\r");
lasmahei 0:356e8d3c2cb7 369 newFile = 1;
lasmahei 0:356e8d3c2cb7 370 }
lasmahei 0:356e8d3c2cb7 371 }
lasmahei 0:356e8d3c2cb7 372 if (!QVGA) wait(0.5);
lasmahei 0:356e8d3c2cb7 373 }
lasmahei 0:356e8d3c2cb7 374 }