Merge pull request #365 from wolfmanjm/fix/config-set
[clinton/Smoothieware.git] / src / main.cpp
index 2ca4b44..dad0577 100644 (file)
@@ -7,17 +7,25 @@
 
 #include "libs/Kernel.h"
 #include "modules/tools/laser/Laser.h"
-#include "modules/tools/extruder/Extruder.h"
+#include "modules/tools/extruder/ExtruderMaker.h"
 #include "modules/tools/temperaturecontrol/TemperatureControlPool.h"
 #include "modules/tools/endstops/Endstops.h"
+#include "modules/tools/touchprobe/Touchprobe.h"
 #include "modules/tools/switch/SwitchPool.h"
-#include "modules/robot/Player.h"
+#include "modules/robot/Conveyor.h"
 #include "modules/utils/simpleshell/SimpleShell.h"
 #include "modules/utils/configurator/Configurator.h"
 #include "modules/utils/currentcontrol/CurrentControl.h"
+#include "modules/utils/player/Player.h"
 #include "modules/utils/pausebutton/PauseButton.h"
+#include "modules/utils/PlayLed/PlayLed.h"
+#include "modules/utils/panel/Panel.h"
+#include "libs/Network/uip/Network.h"
+#include "Config.h"
+#include "checksumm.h"
+#include "ConfigValue.h"
+
 // #include "libs/ChaNFSSD/SDFileSystem.h"
-#include "libs/Config.h"
 #include "libs/nuts_bolts.h"
 #include "libs/utils.h"
 
 #include "libs/USBDevice/USBMSD/SDCard.h"
 #include "libs/USBDevice/USBSerial/USBSerial.h"
 #include "libs/USBDevice/DFU.h"
-
 #include "libs/SDFAT.h"
+#include "StreamOutputPool.h"
 
 #include "libs/Watchdog.h"
 
+#include "version.h"
+#include "system_LPC17xx.h"
+
+#define second_usb_serial_enable_checksum  CHECKSUM("second_usb_serial_enable")
+#define disable_msd_checksum  CHECKSUM("msd_disable")
+#define disable_leds_checksum  CHECKSUM("leds_disable")
+
 // Watchdog wd(5000000, WDT_MRI);
 
-// #include "libs/USBCDCMSC/USBCDCMSC.h"
-// SDFileSystem sd(p5, p6, p7, p8, "sd");  // LPC17xx specific : comment if you are not using a SD card ( for example with a mBed ).
-SDCard sd(P0_9, P0_8, P0_7, P0_6);
-//LocalFileSystem local("local");       // LPC17xx specific : comment if you are not running a mBed
-// USBCDCMSC cdcmsc(&sd);                  // LPC17xx specific : Composite serial + msc USB device
+// USB Stuff
+SDCard sd(P0_9, P0_8, P0_7, P0_6);      // this selects SPI1 as the sdcard as it is on Smoothieboard
+//SDCard sd(P0_18, P0_17, P0_15, P0_16);  // this selects SPI0 as the sdcard
 
 USB u;
-
 USBSerial usbserial(&u);
 USBMSD msc(&u, &sd);
+//USBMSD *msc= NULL;
 DFU dfu(&u);
-//USBSerial usbserial2(&u);
 
 SDFAT mounter("sd", &sd);
 
-char buf[512];
-
 GPIO leds[5] = {
     GPIO(P1_18),
     GPIO(P1_19),
@@ -62,71 +72,115 @@ GPIO leds[5] = {
 };
 
 int main() {
-    for (int i = 0; i < 5; i++)
-    {
+
+    // Default pins to low status
+    for (int i = 0; i < 5; i++){
         leds[i].output();
-        leds[i] = (i & 1) ^ 1;
+        leds[i]= 0;
     }
 
-    sd.disk_initialize();
-
     Kernel* kernel = new Kernel();
 
-    kernel->streams->printf("Smoothie ( grbl port ) version 0.7.2 \r\n");
-
-//     kernel->streams->printf("Disk Status: %d, Type: %d\n", sd.disk_status(), sd.card_type());
-//     if (sd.disk_status() == 0) {
-//         uint16_t s1;
-//         uint8_t s2;
-//         char suffix;
-//         if (sd.disk_sectors() >= (1<<21)) {
-//             s1 = sd.disk_sectors() >> 21;
-//             s2 = ((sd.disk_sectors() * 10) >> 21) - (s1 * 10);
-//             suffix = 'G';
-//         }
-//         else if (sd.disk_sectors() >= (1<<11)) {
-//             s1 = sd.disk_sectors() >> 11;
-//             s2 = ((sd.disk_sectors() * 10) >> 11) - (s1 * 10);
-//             suffix = 'M';
-//         }
-//         else if (sd.disk_sectors() >= (1<< 1)) {
-//             s1 = sd.disk_sectors() >> 1;
-//             s2 = ((sd.disk_sectors() * 10) >> 1) - (s1 * 10);
-//             suffix = 'K';
-//         }
-//         else {
-//             s1 = sd.disk_sectors() << 9;
-//             s2 = 0;
-//             suffix = ' ';
-//         }
-//         kernel->streams->printf("Card has %lu blocks; %llu bytes; %d.%d%cB\n", sd.disk_sectors(), sd.disk_size(), s1, s2, suffix);
-//     }
-
-     kernel->add_module( new Laser() );
-//     kernel->add_module( &wd );
-    kernel->add_module( new Extruder() );
+    kernel->streams->printf("Smoothie Running @%ldMHz\r\n", SystemCoreClock / 1000000);
+    Version version;
+    kernel->streams->printf("  Build version %s, Build date %s\r\n", version.get_build(), version.get_build_date());
+
+    //some boards don't have leds.. TOO BAD!
+    kernel->use_leds= !kernel->config->value( disable_leds_checksum )->by_default(false)->as_bool();
+
+    // attempt to be able to disable msd in config
+    // if(!kernel->config->value( disable_msd_checksum )->by_default(false)->as_bool()){
+    //     msc= new USBMSD(&u, &sd);
+    // }else{
+    //     msc= NULL;
+    //     kernel->streams->printf("MSD is disabled\r\n");
+    // }
+
+    bool sdok= (sd.disk_initialize() == 0);
+
+    // Create and add main modules
     kernel->add_module( new SimpleShell() );
     kernel->add_module( new Configurator() );
     kernel->add_module( new CurrentControl() );
-    kernel->add_module( new TemperatureControlPool() );
     kernel->add_module( new SwitchPool() );
     kernel->add_module( new PauseButton() );
+    kernel->add_module( new PlayLed() );
     kernel->add_module( new Endstops() );
+    kernel->add_module( new Player() );
 
+    // these modules can be completely disabled in the Makefile by adding to EXCLUDE_MODULES
+    #ifndef NO_TOOLS_TEMPERATURECONTROL
+    kernel->add_module( new TemperatureControlPool() );
+    #endif
+    #ifndef NO_TOOLS_EXTRUDER
+    kernel->add_module( new ExtruderMaker() );
+    #endif
+    #ifndef NO_TOOLS_LASER
+    kernel->add_module( new Laser() );
+    #endif
+    #ifndef NO_UTILS_PANEL
+    kernel->add_module( new Panel() );
+    #endif
+    #ifndef NO_TOOLS_TOUCHPROBE
+    kernel->add_module( new Touchprobe() );
+    #endif
+    #ifndef NONETWORK
+    kernel->add_module( new Network() );
+    #endif
+
+    // Create and initialize USB stuff
     u.init();
+    //if(sdok) { // only do this if there is an sd disk
+    //    msc= new USBMSD(&u, &sd);
+    //    kernel->add_module( msc );
+    //}
+
+    // if(msc != NULL){
+    //     kernel->add_module( msc );
+    // }
 
     kernel->add_module( &msc );
     kernel->add_module( &usbserial );
-//    kernel->add_module( &usbserial2 );
+    if( kernel->config->value( second_usb_serial_enable_checksum )->by_default(false)->as_bool() ){
+        kernel->add_module( new USBSerial(&u) );
+    }
     kernel->add_module( &dfu );
     kernel->add_module( &u );
 
-    struct SerialMessage message;
-    message.message = "G90";
-    message.stream = kernel->serial;
-    kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
+    // clear up the config cache to save some memory
+    kernel->config->config_cache_clear();
+
+    if(kernel->use_leds) {
+        // set some leds to indicate status... led0 init doe, led1 mainloop running, led2 idle loop running, led3 sdcard ok
+        leds[0]= 1; // indicate we are done with init
+        leds[3]= sdok?1:0; // 4th led inidicates sdcard is available (TODO maye should indicate config was found)
+    }
+
+    if(sdok) {
+        // load config override file if present
+        // NOTE only Mxxx commands that set values should be put in this file. The file is generated by M500
+        FILE *fp= fopen(kernel->config_override_filename(), "r");
+        if(fp != NULL) {
+            char buf[132];
+            kernel->streams->printf("Loading config override file: %s...\n", kernel->config_override_filename());
+            while(fgets(buf, sizeof buf, fp) != NULL) {
+                kernel->streams->printf("  %s", buf);
+                if(buf[0] == ';') continue; // skip the comments
+                struct SerialMessage message= {&(StreamOutput::NullStream), buf};
+                kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
+            }
+            kernel->streams->printf("config override file executed\n");
+            fclose(fp);
+        }
+    }
 
+    uint16_t cnt= 0;
+    // Main loop
     while(1){
+        if(kernel->use_leds) {
+            // flash led 2 to show we are alive
+            leds[1]= (cnt++ & 0x1000) ? 1 : 0;
+        }
         kernel->call_event(ON_MAIN_LOOP);
         kernel->call_event(ON_IDLE);
     }