J L / Mbed 2 deprecated Neptune_170620

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <cstdlib>
00002 #include <string>
00003 #include "main.h"
00004 #include "Functions.h"
00005 #include "Definitions.h"
00006 #include "Boolean.h"
00007 #include "NextionLCD.h"
00008 #include "mbed.h"
00009 #include "platform/CircularBuffer.h"
00010 #include "rtos.h"
00011 #include "Threads.h"
00012 #include "Languages.h"
00013 #include "Ser25lcxxx.h"
00014 #include "NVM.h"
00015 #include "Watchdog.h"
00016 #include "NextionPages.h"
00017 #include "Controls.h"
00018 #include "FastPWM.h"
00019 #include "MotorControl.h"
00020 
00021 /*
00022 
00023   _/      _/  _/_/_/_/  _/_/_/    _/_/_/    _/_/_/_/  _/_/_/        _/_/_/      _/_/      _/_/_/   
00024  _/      _/  _/        _/    _/  _/    _/  _/        _/    _/      _/    _/  _/    _/  _/          
00025 _/      _/  _/_/_/    _/_/_/    _/    _/  _/_/_/    _/_/_/        _/    _/  _/    _/    _/_/       
00026  _/  _/    _/        _/    _/  _/    _/  _/        _/    _/      _/    _/  _/    _/        _/      
00027   _/      _/_/_/_/  _/    _/  _/_/_/    _/_/_/_/  _/    _/      _/_/_/      _/_/    _/_/_/    
00028 
00029 */
00030 
00031 //================================================================================================================================
00032 // COMPANY                :  Verderflex
00033 //
00034 // PROJECT                :  Verder DOS Peristaltic Pump
00035 // DATE                   :  3rd March 2020   
00036 
00037 string HARDWARE =           "Hardware Version : HVDOS X1.00";                                                
00038 string SOFTWARE =           "Software Version : SVDOS X1.00";
00039 string AUTHOR   =           "Author : Jim Lowe";
00040 
00041 /*
00042  *   Firmware Numbering Scheme, V Major.Minor.Bugfix
00043  *
00044  *   Major  : Major Changes to function
00045  *   Minor  : Minor Changes to function
00046  *   Bugfix : Firmware fixes to function
00047  *
00048  */
00049 
00050 // HARDWARE              :   NUCLEO-F746ZG + Nextion 4.3" Display
00051 //
00052 // COMPILER              :   Mbed Studio
00053 
00054 //================================================================================================================================
00055 //************************************************  Code Status / Changes ********************************************************
00056 //================================================================================================================================
00057 
00058 /*
00059  *
00060  *  NOTES on Nextion Displays:
00061  *  Nextion Push event is generated when the touch screen is PRESSED
00062  *  Nextion Pop event is generated when the touch screen is RELEASED
00063 
00064  *  Firmware Log, Initial Software Development Phase
00065 
00066     09/05/19    itoa() now working
00067     09/05/19_E  Nextion now running at 115200 Baud
00068     13/05/19_D  Ported over to Nucleo-F746ZG
00069     15/05/19_A  First version working on Mbed Studio
00070     15/05/19_F  Nextion RX Interrupt message decoder started
00071     16/05/19_A  Increment / Decrement arrow work but project need to be cleaned up before going further
00072     16/05/19_A  Push & Pop functions for touch buttons are working. 
00073     17/05/19_A  Adding debugging functions
00074     17/05/19_A  Created a debug thread seems to work ok  
00075     17/05/19_B  Success Nextion slider is working   
00076     17/05/19_B  Flow rate / RPM selection now working
00077     17/05/19_C  PWM now working, needs scaling factors to represent 100% for 1250 RPM etc
00078     17/05/19_C  Progress bar working
00079     20/05/19_A  Peripheral Pin List added
00080     20/05/19_D  PWM frequency changed to 24kHz, this was found to be the frequency used on the DRV8306EVM BLDC Demo board
00081     20/05/19_D  BLDC Motor working under PWM control, now reaches the same build level as QAD with Mbed 
00082     28/05/19_A  STM32 does not have any EEPROM, need EEPROM for NVM capability, adding EEPROM capabilities to project
00083     28/05/19_A  Add digital motor control
00084     28/05/19_A  Motor direction control working, other I/O need to be done when schematics arrive from TI
00085     28/05/19_B  Nextion back light dim now working, it is fussy about where it is executed
00086     29/05/19_B  EEPROM testing thread proves EEPROM can be accessed and read correctly, removing EEPROM result in all 00, with EEPROM pattern is repeatable.
00087     29/05/19_E  Good demo code for NVM, needs tidying up
00088     30/05/19_A  Can read full NVM, can write single bytes to NVM
00089     30/05/19_B  This EEPROM library cannot write a single byte to a memory location, it does not work, you have to write the entire NVM which is not good
00090     31/05/19_A  Other EEPROM library no good, will try this again
00091     31/05/19_A now working in a way to develop the NVM feature properly, this will be pursued after my holiday   
00092     10/06/19_A  Continue with NVM development  
00093     10/06/19_B  NVM ready for Nextion menu to be taken to next level to feature NVM settings
00094     12/06/19_A  Rotation arrow on menu was not controlling motor rotation, it had been broken by adding other menu buttons, and this is something I am aware of now and to watch out for
00095     13/06/19_B  Culled the amount of project in Mbed Studio as this was too big for the resources for Mbed Studio causing it to not recognize the target as an Mbed enabled device
00096     13/06/19_B  Though I could get away with doing the NVM update in the RX ISR but this causes a run time error crashing the processor, I will place the NVM update in a thread
00097     14/06/19_A  This code is broken trying to transition to better way to implement metering units selection, use next code or previous code
00098     14/06/19_C  Taken most of the day but now NVM RAM is being written to from EEPROM, due to char* pointer this mechanism not working as thought, I have a working method now.
00099     17/06/18_A  First implementation of working NVM, needs some refinement
00100     17/06/19_D  Good refinement achieved for Metering Units NVM
00101     17/06/19_E  memcpy() replaces for loop, implemented free() to free up dynamically created pointer memory after use as eepro.read() uses malloc()
00102     27/06/19_A  Pre work ONLY for 4-20mA, setup of button membrane, 4-20mA control inputs
00103     01/07/19_A  Pre work ONLY for 4-20mA, Open Drain outputs setup and default to off at power up
00104     02/07/19_A  Adding map function
00105     12/07/19_A  Assigned new I/O for start/stop buttons and start/stop LEDS as I/O I has assigned might be required later on as they are part of USB and ST_LINK stuff.
00106     15/07/19_A  Grey Box build all working correctly, phew
00107     15/07/19_A  Need to implement start / stop button control, added state machine
00108     15/07/19_A  Motor thread commented out, motor control part of state machine
00109     15/07/19_B  Not sure how text box b2 is displaying RPM when stopped, I have overridden this for now
00110     15/07/19_C  Stop/Start operational
00111     16/07/19_A  Scaling for RPM, ml/min and % working perfectly
00112     16/07/19_A_TEXT This is the first version that uses TEXT for displaying metering digits using printf
00113     17/07/19_C  Issues with nextion, this version look ok    
00114     18/07/19_A  Page component IDs definitions had changed on Nextion HMI code again when I created  16/07/19_A_TEXT version, its a really crappy and stops your working code from working
00115     19/07/19_A  3 Second Watchdog implemented and working   
00116     19/07/19_A  Nextion power control added and working 
00117     19/07/19_A  1ms timer implemented and working, not uses at present
00118     22/07/19_A  Fit 4-20mA Module and connect up to system
00119     22/07/19_A  DAC pins already use I will have to re-assign pins to be able to use DAC
00120     22/07/19_A  ADC is now measuring mV, ADC is 12bit so more noise than 10bit ADC
00121     23/07/19_B  4-20mA is outputting to debug screen and is scaled using mapF, look fairly stable and no averaging has been applied yet.
00122     24/07/19_A  Look at ways to make analogue reading  more stable 
00123     24/07/19_A  Speed control using LoopMate achieved, although values from ain.read_u16() do not seem to make any sense at this time, was expecting 0 - 4095?
00124     25/07/19_A  Good results displayed on debug screen without any software smoothing applied.
00125     25/07/19_B  Working really well now I have add the moving average algorithm, this adds a nice ramp rate to the product
00126     26/07/19_A  Full motor speed does not seem to be there, need to fix this and create a function for motor control via 4-20mA
00127     26/07/19_A  Motor speed is ok now, used mapF() to make sure motor has stopped at 4.0mA, previously it would only stop at 0.0mA
00128     26/07/19_A  Wow, showing excellent stability at 10uA resolution
00129     26/07/19_A  Metering unit displayed against mA, cool
00130     29/07/19_A  Rough ramp rate applied when change of motor direction, this code is only an experiment and is blocking and needs to be refined in to non-blocking code   
00131     01/08/19_A  Create functions for manual and 4-20mA
00132     01/08/19_A  Nextion definition need changing for updated display GUI featuring QDOS type menu system
00133     02/08/19_A  Created Timers.cpp to hold all timer functions and structures
00134     05/08/19_A  Trying to implement manual control with dual speed inc/dec functionality
00135     05/08/19_A  I have inc/dec functionality working, tidy code up and move forward
00136     05/08/19_C  meter val changed to int16_t data type from uint16_t so abs() can be used for zeroing negative numbers
00137     05/08/19_C  Nextion screen initializes on page 33 wher no buttons are, page 1 is called when start button is press, stop button calls page 33
00138     06/08/19_A  Threads remove until required, all processing done in main and nextion RX ISR, all working.
00139     07/08/19_A  Can now display flow units retrieval from NVM
00140     08/08/19_A  Implement flow unit selection and storage to EEPROM
00141     08/08/19_A  flow unit can not be see using extern!
00142     08/08/19_B  Flow unit can be written and read from EEPROM
00143     08/08/19_C  Implement manual and analogue operating mode stored in EEPROM     
00144     12/08/19_A  Analogue function need to support all flow units
00145     12/08/19_C  This version can demo flow units change, analogue and manual mode selected from menu system
00146     12/08/19_D  meterVal, minScale, maxScale are now floats to give resolution required, this version is not full working at this time
00147     13/08/19_A  displayed info in p1left not correct, need to implement incDecSpeed variable so when the touch key is held the inc/dec speed increases
00148     14/08/19_A  Beware EEPROM value had somehow been changed for run.mode stopping normal program from running, suspect power glitch, as a consequence 
00149                 it has taken all morning to figure out the problem.
00150     14/08/19_B  System can now be re-booted by pressing and holding down the ON button for 3 seconds         
00151     15/08/19_A  Add some diagnostics to check EEPROM contents    
00152     16/08/19_A  Change state machine structure so meter screen is correctly updated on return from Nextion pages
00153     27/08/19_B  Inc/Dec indexer needs improvement
00154     28/08/19_B  Inc/Dec control has been improved, speed can be set manually some tweaking may still be required at later point, moving on to analogue out now.
00155     28/08/19_C  Start of DAC implementation to provide analogue output
00156     28/08/19_C  SPI moved from SPI1 on CN7 to SPI14 on CN9, this is required to allow connection to DACs required for analogue output
00157     28/08/19_C  I have verified that the SPI NVM is working after moving to other microcontroller pins for SPI14
00158     28/08/19_C  I am able to set analogue output currents for test purposes.
00159     28/08/19_E  Analogue output development started
00160     29/08/19_B  Manual speed adjustment improved further now.
00161     29/08/19_B  Stop LED used to prompt user to number indexer when setting flow rates
00162     02/09/19_A  Accurate analogue output when in manual mode  can be demonstrated
00163     02/09/19_B  Two dimensional array created for ALL VDOS flow rate types   
00164     02/09/19_B  Can store and retrieve floats to EEPROM as an experiment, need to implement this in analogue cal routine  
00165     04/09/19_A  Able to switch in and out of ANA_CAL state read for analogue menus
00166     05/09/19_A  WARNING page 1 component ID have changed
00167     06/09/19_B  Almost finished improved flow unit manual inc/dec control
00168     09/09/19_A  I need to be moving forward with analogue calibration so I have put on hold development o inc/dec control for now
00169     12/09/19_A  Floats can be written to and read from EEPROM for mA high, flow high, mA low, flow low calibrations
00170     12/09/19_B  % / mA graph working as initial demo
00171     16/09/19_A  Calibration graph line generation now done on Nextion by timer so no resources are required for this micro.
00172     17/09/19_A  IncDecControl function created that can be used for all inc/dec controls on all pages
00173     17/09/19_A  Clear out functions that are not used general tidy up of code
00174     18/09/19_A  Working on calibration graph, good progress made here
00175     19/09/19_A  struct settingsNVM has been updated to include calibration variables
00176     20/09/19_A  Hysteresis add to 4-20 pump motor start threshold
00177     20/09/19_A  Re-declaration of constants to give accurate rage #define ADC_MIN_V 0.013, #define ADC_MAX_V 2.9012//V   
00178     23/09/19_B  full NVM RAM loaded into NVM structure in READ_NVM
00179     23/09/19_B  USB ISOLATOR TO BE USED FROM NOW ON TO STOP USB GND EFFECTING THE UNIT
00180     23/09/19_B  Calibration is working, checking all aspects of calibration
00181     24/09/19_A  anaOut_mA(mapF(ana_mA, I_3MA, I_20MA, nvm.mA_low, nvm.mA_high)); APPEARS TO BE WORKING NOW, TESTING REQUIRED
00182     25/09/19_B  Calibarion, analogue output, displayed flow  units all appear to be match well to QDOS
00183     26/09/19_B  Works like QDOS for calibration now and analogue  in/out, issue when leaving calibration normal running does not resume correctly
00184     27/09/19_A  Calibration graph has refresh problem when adjusting flow rate %
00185     30/09/19_A  I hope the issue above in now fixed, this was done by erasing the RED line before going to the next calibration screen
00186     30/09/19_B  Start looking at speed feedback from tacho output on motor controller
00187     30/09/19_B  Tacko from fGout processed by isr test working
00188     01/10/19_A  General code tidy, removal of unused variables, add ISR module
00189     03/10/19_A  Implement motor stall detection
00190     07/10/19_A  Motor development put on hold as present motor controller cannot produce high torque at low speed as QDOS 20 does
00191     07/10/19_A  Remote Start / Stop implemented
00192     07/10/19_A  Remote Fluid Recover, reverses motor in analogue mode, will check more when we receive new QDOS with this feature
00193     07/10/19_A  Open Collector outputs now function, they have to be allocated to status and alarm software functions yet
00194     10/10/19_A  Remote run/stop operational, code tidy now required
00195     10/10/19_A  Need to store and retrieve the speed setting
00196     11/10/19_A  configure Neptune motor for shaft speed range of 100 - 2500 RPM
00197     14/10/19_A  Motor controller is on hold for now, I will concentrate of delivering the features of QDOS 20
00198     14/10/19_A  Implement Auto-restart feature    
00199     14/10/19_A  Flow units in manual mode saved and retreived from NVM after power loss
00200     16/10/19_B  State machine does not work as intended causing problem for auto-restart and flow unit change over, re-do state machine
00201     18/10/19_A  flow rate can be switched from one to another matching the percentage as QDOS does
00202     21/10/19_A  Auto-Restart, remote inputs and O/C outputs working, read for checking with QDOS 30
00203     22/10/19_A  Motor rotation is CCW but QDOS arrow is CW, Neptune will be set the same
00204     22/10/19_A  FastPWM library added, leave PWM prescaler at 1 so that that the smallest speed inc/dec results in a measureable PWM change,
00205                 resolution is 12 bit (4096), measured bit width is ~20ns/bit, pwm period is 1/10000 = 100us, so 100us / 20ns = 5000
00206     22/10/19_B  Implement MAX prime function   
00207     23/10/19_A  To allow the touch button to be active at the stop screen as with QDOS the nexKeyScan() need to be call in the main loop   
00208     23/10/19_A  New motor ramp time added to motor function, need to connect up nBrake to motor controller         
00209     24/10/19_A  New readNVM() created to hide complexity and improve readability, working on motor ramp time parameter
00210     25/10/19_A  Nextion display has failed, as extra protection tristate TX & RX line until Nextion has powered up
00211     30/10/19_A  New Nextion display fitted, 1K resistor mod added to RX line
00212     30/10/19_B  QDOS 30 flow units added to replace QDOS 20 flow units
00213     31/10/19_A  Removed ramNVM so epprom data is transferred to and from direct to NVM data structure
00214     01/11/19_A  NVM memory working good and more easy to understand and use now
00215     01/11/19_B  PWM is now incremented / decremented at 0.1 resolution, so a change of 0.1% alter the PWM    
00216     01/11/19_B  Cannot find any problems with this version, best version so far       
00217     05/11/19_A  Calibration Menu cannot be accessed after initial calibration.  
00218     11/11/19_A  Work required for remote control inputs 
00219     11/11/19_A  Adding output1&2 settings
00220     12/11/19_A  Discovery that SPI frequency was set to 1000 Hz, I forgot about this    
00221     18/11/19_A  Various icon indicators added to page 1 top line
00222     20/11/19_A  Implementation of control settings
00223     21/11/19_A  Provide a version of software to use USB USART1 (PA9 & PA10) on product ONLY as product will not have ST-LINK 
00224     02/12/19_A  Method of programming via USB has been designed in hardware, trap software in while loop and it will re-boot via watchdog timer with boot0 connected to vdd
00225     03/12/19_A  #ifdef BOOTLOADER setup for production bootloader without USB/ST-LINK
00226     05/12/19_A  Microcontroller pin assignment checked with Kevin Moon's work, all ok.
00227     06/12/19_A  Continue with control settings menu, EEPROM variables for control settings
00228     11/12/19_A  Output 1 NVM working
00229     02/01/20_A  Output 1 & 2 appears to be working, NVM setting is being saved and retrieved correctly for displayed string names
00230     06/01/20_A  Trying to fix return from analogue cal routine, the nvm.flowUnitVal is being set to minium value
00231     06/01/20_B  The above is fixed, reset() was clearing nvm.flowUnitVal, this has been commeted out for now.    
00232     08/01/20_A  Code refactoring 
00233     13/01/20_A  Control Setting output 1&2, tick boxes for High and Low logic under development
00234     13/01/20_A  High & Low output logic setting now stored in nvm, develop function for remote inputs and O/C outputs
00235     16/01/20_A  Start/Stop input Pump Stop logic NVM tick boxes working.
00236     17/02/20_A  4-20mA Scale NVM tick boxes working.
00237     03/02/20_A  Remote pump Ster/Stop working same as QDOS
00238     04/02/20_A  Improve Nextion driver so that only updated data is sent to Nextion, if the data is the same nothing is done.
00239     04/02/20_B  Demonstrated code working for alarm outputs, continue this tomorrow
00240     10/02/20_A  Rotation arrow issues now resolved
00241     12/02/20_A  Control setting, inputs, o/c outputs, mA output scale all working
00242     13/02/20_A  DEBUG_LCD added to improve loop time further by being able to see if any LCD functions are being called continuoisly.
00243     24/02/20_A  Problem with with inc/dec controls, this is related to the speedLimit variable introduced
00244     24/02/20_A  SpeedLimit now operates from nvm.speedLimit, speed limit menu need to be implemented
00245     28/02/20_A  Improvements made to pause/stop symbol fucntion and output1 & 2 logic, test concur operation same as QDOS
00246     28/02/20_B  Staring investigatory work for ED motor controller controler support, setup an serial interface for rs232 motor control    
00247     28/02/20_C  Communication established on usart6 for motor control
00248     28/02/20_C  Speeded up tremendously the serial communiucations on all at usarts, they all work at 921600 Baud, Nextion seem not to 
00249                 have a problem with this unimate fast baud, if I start to see problems I will slow the baud down.
00250 
00251 */
00252 
00253 /*
00254 
00255     ***** TARGET CAN BE LOST FROM USB FOR NO REASON, ONLY WAY TO GET IT BACK IS TO RE-BOOT PC AND SELECT ACTIVE PROGRAM AGAIN *****
00256 
00257     Nextion text for mA readings will have to be set txt_maxl = 4 to limit precision
00258 
00259     Analogue read is ~15us
00260         
00261     Find relationship between RPM and PWM, make sure we have 4000 RPM, measure with tachometer
00262     Ramping need to be implemented so there is no abrupt stop and start at high speed
00263     Hide away complexity in functions
00264     Review all function, trim out useless functions
00265     
00266     Measured 4426 RPM unloaded
00267 
00268     Neptune requirement is 120 Litres/Hour = 2000ml/min
00269 
00270     Meeting today 16/8/19
00271 
00272     US Flow Units, DB
00273     Screen RED for alarm
00274     Screen saver
00275     Cooling fan as standard
00276     Digital Dosing Pump 
00277     Heads Left & Right fitment, rotation direction fixed based on which head is selected
00278     Sep 2020 exhibition
00279 
00280     QDOS 20 & 30 are the initial target
00281 
00282     PA_4 and PA_5 are DAC1 & DAC2 outputs, these are presently used for SPI, I am going to have to move the SPI in order to use the DACs
00283 
00284 */
00285 
00286 //PERIPHERAL PINS
00287 //https://github.com/ARMmbed/mbed-os/blob/master/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F746xG/TARGET_NUCLEO_F746ZG/PeripheralPins.capabilities
00288 
00289 /*
00290     25LC010AT
00291 
00292     1K SPI EEPROM (128 x 8)
00293 
00294     EEPROM 3V3 ok
00295          ---------                
00296     SCK |1       6| VDD
00297     VSS |2       5| /CS
00298     SI  |3       4| SO
00299          ---------
00300 
00301     PA_7 MOSI// Connected to RMII_CRS_DV [LAN8742A-CZ-TR_CRS_DV]
00302     PA_6 MISO  
00303     PA_5 SCLK    
00304     PA_4 SSEL  
00305 */
00306 
00307 //Objects
00308 
00309 
00310 /*USART
00311 
00312 USART1 = VERDERBUS
00313 USART2 = NEXTION DISPLAY
00314 USART3 = BOOTLOADER
00315 USART6 = MOTOR CONTROL
00316 
00317 
00318 */
00319 
00320 #ifdef BOOTLOADER//USB TO UART BOOTLOADER
00321     /* 
00322         Physical connections PB10, PB11 USART3 required for Bootloader
00323         RXPC <- PB10 OUTPUT TXN        
00324         TXPC -> PB11 INPUT RXN
00325         These become Virtual Com Port PD_8, PD_9
00326     */
00327     Serial pc(SERIAL_TX, SERIAL_RX);//Bootloader port
00328 #else//MBED VIA ST-LINK
00329     /* 
00330         Physical connections PD_8, PD_9 {USART3} debug port
00331         RXPC <- PD_8 OUTPUT TXN        
00332         TXPC -> PD_9 INPUT RXN
00333     */
00334     Serial pc(USBTX, USBRX);//Virtual Com Port PD_8, PD_9 Debug port required for MBED
00335 #endif
00336 
00337 NextionLCD lcd(USART2_TX, USART2_RX);//Tx, Rx, Nextion Port {USART2}
00338 
00339 Serial usart6(USART6_TX, USART6_RX);//Tx, Rx Motor Controller {USART6}
00340 
00341 SPI spi(MOSI,MISO,SCLK);//MOSI, MISO, SCLK, SSEL
00342 Ser25LCxxx eeprom(&spi,SSEL,BYTE_SIZE,PAGE_SIZE);
00343 Watchdog wd;
00344 
00345 Timer tPop;//touch button release timer
00346 Timer tPush;//touch button press timer
00347 Timer tRst;//reset timer
00348 Timer tSpeed;//numerical update rate of flow units
00349 
00350 volatile float ml = 0;
00351 bool maxBtnPress = false;//Prime buttOn (MAX)
00352 uint8_t storedTask = false;//Remember the previous state machine state
00353 bool rmtFlag = false;//Remote input flag
00354 char *readbuf;
00355 bool maxFlag = true;
00356 bool arrowFlag = false;   
00357 
00358 ///////////////////////////////////////////////////////////////////////////////
00359 //VARIABLES
00360 ///////////////////////////////////////////////////////////////////////////////
00361 float minScale = 0;
00362 float maxScale = 0;//Max Scale variable to stop max RPM, max ml/min and max %
00363 
00364 ///////////////////////////////////////////////////////////////////////////////
00365 // ARRAYS
00366 ///////////////////////////////////////////////////////////////////////////////
00367 
00368 ///////////////////////////////////////////////////////////////////////////////
00369 // CONSTANTS
00370 ///////////////////////////////////////////////////////////////////////////////
00371 
00372 ///////////////////////////////////////////////////////////////////////////////
00373 // STRUCTURE DEFINITIONS
00374 ///////////////////////////////////////////////////////////////////////////////
00375 struct settingsNVM nvm;
00376 
00377 ///////////////////////////////////////////////////////////////////////////////
00378 // DEBUG
00379 ///////////////////////////////////////////////////////////////////////////////
00380 
00381 ///////////////////////////////////////////////////////////////////////////////
00382 // MAIN
00383 ///////////////////////////////////////////////////////////////////////////////
00384 int main(){
00385 
00386     uint8_t task;
00387 
00388     task = INITIALISE;
00389  
00390     while(1){
00391     
00392         switch(task){
00393         
00394             case INITIALISE:    
00395                                 sysInit();                                                                         
00396                                 task = STOP_SCRN;
00397                                 //task = DIAGS;
00398                                 dbgStates(task); 
00399                                 break;
00400 
00401             case STOP_SCRN:    
00402                                 ml=ZERO;            
00403                                 lcd.nexChgPage(PAGE_1_HOME);       
00404 /*                                                            
00405                                 lcd.nexDispSymbol("key", ON);
00406                                 lcd.nexDispSymbol("leak", ON);
00407                                 lcd.nexDispSymbol("update", ON);
00408                                 lcd.nexDispSymbol("error", ON);
00409                                 lcd.nexDispSymbol("wifi", ON);        
00410                                 lcd.nexDispSymbol("locked", ON);
00411                                 lcd.nexDispSymbol("unlocked", ON);
00412                                 lcd.nexDispSymbol("temp", ON);
00413                                 lcd.nexDispSymbol("stop", ON);
00414                                 lcd.nexDispSymbol("pause", ON);                                                                                                                                                            
00415 */
00416                                 lcd.nexDispSymbol("stop", ON);
00417                                 lcd.nexDispSymbol("pause", OFF);                                  
00418                                 lcd.nexDispSymbol("unlocked", ON);
00419                                 decodeByteNVM(nvm.flowUnits, &precision);     
00420                                
00421                                 lcd.nexSendTxt("p1left","VDOS");//page 1 middle text box    
00422                                 lcd.nexSetFontCol("p1mid",RED);                                                                                          
00423                                 lcd.nexSendTxt("p1mid","Manual Mode");//page 1 middle text box  
00424 
00425                                 lcd.nexSendFloat("meter", nvm.flowUnitVal, precision);//display the flow units                                                             
00426                                 stopBtn(BTN_BYPASS_TRUE);//button disasbled LEDs only      
00427                                 motor(ZERO, CCW, BRAKE_OFF, MOTOR_DISABLE, 1);                      
00428 
00429                                 storedTask = STOP_SCRN;//this state   
00430                                 task = START;
00431                                 dbgStates(task); 
00432                                 break;
00433                                                                                                  
00434             case START:         
00435                                 anaOut_mA(I_3MA);                                               
00436                                 task = startUpMode(nvm.runMode);                         
00437                                 storedTask = START;
00438                                                              
00439                                 dbgStates(task); 
00440                                 break;      
00441                                                             
00442             case MANUAL_SET:    //Setup for Manual running mode             
00443                                 lcd.nexChgPage(PAGE_1_HOME);   
00444                                 lcd.nexDispSymbol("unlocked", ON);
00445                                 //lcd.nexSetFontCol("p1mid",BLACK);                                                                                           
00446                                 //lcd.nexSendTxt("p1mid","Manual Mode");//page 1 middle text box                                                                                                                                                                                                                                                                
00447                         
00448                                 lcd.nexSetCrop("inc",PAGE_1_HOME);//restore inc button image required for manual mode    
00449                                 lcd.nexSetCrop("dec",PAGE_1_HOME);//restore dec button image required for manual mode                                                                                
00450                                 decodeByteNVM(nvm.flowUnits, &precision);//display the flow unit symbol                                  
00451                                 lcd.nexSendFloat("meter", nvm.flowUnitVal, precision);//display the flow unit value           
00452                                 
00453                                 storedTask = MANUAL_SET;//this state                                                                         
00454                                 task = RUNNING;//next state  
00455                                 dbgStates(task);                                
00456                                 break;
00457 
00458             case ANALOGUE_SET:  //Setup for Analogue running mode                                    
00459                                 lcd.nexChgPage(PAGE_1_HOME); 
00460                                 lcd.nexDispSymbol("unlocked", ON);                                
00461                                 lcd.nexSetFontCol("p1mid",RED);   
00462                                 
00463                                 lcd.nexSendTxt("p1mid","4-20mA Input Mode");//page 1 middle text box                                                                                                                                                                                               
00464                                 lcd.nexSetCrop("inc",CLEAR_INC_DEC);//remove inc button image not required for analogue mode   
00465                                 lcd.nexSetCrop("dec",CLEAR_INC_DEC);//remove dec button image not required for analogue mode                                                                                                                                                                            
00466                                 decodeByteNVM(nvm.flowUnits, &precision); 
00467  
00468                                 storedTask = ANALOGUE_SET;
00469                                 task = RUNNING;  
00470                                 dbgStates(task);                                                                                      
00471                                 break;           
00472      
00473             case RUNNING:                   
00474                                 switch(runModeNVM(nvm.runMode)){                                                                                                                        
00475                                     case MANUAL_SET:    task = manual();    break;
00476                                     case ANALOGUE_SET:  task = anaIn();       break;                                    
00477                                     //case FLOW_CAL:      task = flowCalMode();   break;                                    
00478                                     //case CONTACT_SET:   task = contactMode();   break;
00479                                     //case FLUIDREC_SET:  task = fluidRecMode();  break;  
00480                                     
00481                                     default:;
00482                                 }   
00483                                   
00484                                 task = STOP;   
00485                                 dbgStates(task);                       
00486                                 break;
00487 
00488             case STOP:                                       
00489                                 if(((rmtRunStop == LO)&&(rmtFlag == true))||stopBtn(BTN_BYPASS_FALSE)){
00490                                                          
00491                                     lcd.nexDispSymbol("stop", OFF);                                       
00492                             
00493                                     task = STOP_SCRN;                                                                                                          
00494                                 }
00495                                 else
00496                                     task = RUNNING;
00497                                                 
00498                                 dbgStates(task); 
00499                                 break;
00500 
00501             case ANA_CAL:            
00502                                 task = anaCal();     
00503                                 dbgStates(task);                                                         
00504                                 break;
00505 
00506             case STALL:         while(1);
00507             
00508                                 break;
00509 
00510             case FAULT:
00511                                 break;     
00512 
00513             case DIAGS:         
00514              
00515                                 break;                                                       
00516 
00517             default:            pc.printf("Error Main : Last task = %d",task);  
00518                              
00519                                 //////////////////////////////////////while(1)
00520                                     //////////////////////////////////////////////////////wd.Service();//Kick the dog
00521         }
00522 
00523         #ifdef LOOP_TIME  
00524             loopTimeMeasure();
00525         #endif    
00526  
00527         #ifndef WDT_TEST
00528             if(!startBtn(OFF))//holding the start button will re-boot the system
00529                 wd.Service();//Kick the dog    
00530         #endif        
00531 
00532         outPuts(leakIn, &nvm); 
00533         task = nexKeyScan(task, storedTask);//read nextion touch keys  
00534     }
00535 }