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:
Thu Feb 22 10:36:24 2018 +0000
Revision:
2:de08bea849b8
Parent:
1:f95f37892056
Disabled Full Range PWM Output (J12 / J15). If this feature is used, please disable RGB PWMs in the source code!

Who changed what in which revision?

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