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 21 18:41:26 2018 +0000
Revision:
1:f95f37892056
Parent:
0:356e8d3c2cb7
Child:
2:de08bea849b8
Added some #defines as switches for various functions

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