Jump to content
43oh

terjeio

Members
  • Content Count

    145
  • Joined

  • Last visited

  • Days Won

    27

Reputation Activity

  1. Like
    terjeio got a reaction from agaelema in Compact command parser/dispatcher example   
    A command parser/dispatcher example from my CO2 laser engraver codebase, using a struct array containing the commands and associated  pointer to functions. A lot cleaner (and easier to maintain) than switch/case statements or if/else constructs...
    Functions get called with a pointer to the command tail for local parameter parsing.
    The struct array data are all placed in flash.
    typedef struct { char const *const command; bool (*const handler)(char *); const bool report; } command; bool query (char* params); bool start (char* params); bool moveXrel (char* params); bool moveYrel (char* params); bool moveZrel (char* params); bool XHome (char* params); bool YHome (char* params); bool ZHome (char* params); bool XYHome (char* params); bool zeroAllAxes (char* params); bool laser (char* params); bool setLaserPower (char* params); bool setImageDPI (char* params); bool setPulseDutyCycle (char* params); bool enableCoolant (char* params); bool enableAirAssist (char* params); bool setMode (char* params); bool getPosition (char* params); bool setPPI (char* params); bool setPulseWidth (char* params); bool enableExhaustFan (char* params); bool setEngravingSpeed (char* params); bool getStatus (char* params); bool setEchoMode (char* params); bool setAMode (char* params); bool setPWROffset (char* params); bool loadProfile (char* params); bool setXBcomp (char* params); void exeCommand (char *cmdline) { static const command commands[] = { "?", &query, true, "Power:", &setLaserPower, true, "DutyCycle:", &setPulseDutyCycle, true, "PulseWidth:", &setPulseWidth, true, "DPI:", &setImageDPI, true, "Start:", &start, true, "X:", &moveXrel, true, "Y:", &moveYrel, true, "Z:", &moveZrel, true, "HomeXY", &XYHome, true, "HomeX", &XHome, true, "HomeY", &YHome, true, "HomeZ", &ZHome, true, "ZeroAll", &zeroAllAxes, true, "Laser:", &laser, true, "Coolant:", &enableCoolant, true, "Air:", &enableAirAssist, true, "Mach3:", &setMode, true, "Pos", &getPosition, false, "PPI:", &setPPI, true, "Exhaust:", &enableExhaustFan, true, "Speed:", &setEngravingSpeed, true, "Status", &getStatus, false, "ASelect:", &setAMode, true, "PWROffset:", &setPWROffset, true, "LoadProfile:", &loadProfile, true, "XBComp:", &setXBcomp, true, "Echo:", &setEchoMode, false }; bool ok = false; uint32_t i = 0, numcmds = sizeof(commands) / sizeof(command), cmdlen; while(!ok && i < numcmds) { cmdlen = strlen(commands[i].command); if(!(ok = !strncmp(commands[i].command, cmdline, cmdlen))) i++; } if(ok) { ok = commands[i].handler(cmdline + cmdlen); if(commands[i].report) serialWriteLn(ok ? "OK" : "FAILED")); } else serialWriteLn("Bad command"); } For further reading see http://www.barrgroup.com/Embedded-Systems/How-To/C-Function-Pointers
     
     
     
  2. Like
    terjeio got a reaction from timotet in Compact command parser/dispatcher example   
    A command parser/dispatcher example from my CO2 laser engraver codebase, using a struct array containing the commands and associated  pointer to functions. A lot cleaner (and easier to maintain) than switch/case statements or if/else constructs...
    Functions get called with a pointer to the command tail for local parameter parsing.
    The struct array data are all placed in flash.
    typedef struct { char const *const command; bool (*const handler)(char *); const bool report; } command; bool query (char* params); bool start (char* params); bool moveXrel (char* params); bool moveYrel (char* params); bool moveZrel (char* params); bool XHome (char* params); bool YHome (char* params); bool ZHome (char* params); bool XYHome (char* params); bool zeroAllAxes (char* params); bool laser (char* params); bool setLaserPower (char* params); bool setImageDPI (char* params); bool setPulseDutyCycle (char* params); bool enableCoolant (char* params); bool enableAirAssist (char* params); bool setMode (char* params); bool getPosition (char* params); bool setPPI (char* params); bool setPulseWidth (char* params); bool enableExhaustFan (char* params); bool setEngravingSpeed (char* params); bool getStatus (char* params); bool setEchoMode (char* params); bool setAMode (char* params); bool setPWROffset (char* params); bool loadProfile (char* params); bool setXBcomp (char* params); void exeCommand (char *cmdline) { static const command commands[] = { "?", &query, true, "Power:", &setLaserPower, true, "DutyCycle:", &setPulseDutyCycle, true, "PulseWidth:", &setPulseWidth, true, "DPI:", &setImageDPI, true, "Start:", &start, true, "X:", &moveXrel, true, "Y:", &moveYrel, true, "Z:", &moveZrel, true, "HomeXY", &XYHome, true, "HomeX", &XHome, true, "HomeY", &YHome, true, "HomeZ", &ZHome, true, "ZeroAll", &zeroAllAxes, true, "Laser:", &laser, true, "Coolant:", &enableCoolant, true, "Air:", &enableAirAssist, true, "Mach3:", &setMode, true, "Pos", &getPosition, false, "PPI:", &setPPI, true, "Exhaust:", &enableExhaustFan, true, "Speed:", &setEngravingSpeed, true, "Status", &getStatus, false, "ASelect:", &setAMode, true, "PWROffset:", &setPWROffset, true, "LoadProfile:", &loadProfile, true, "XBComp:", &setXBcomp, true, "Echo:", &setEchoMode, false }; bool ok = false; uint32_t i = 0, numcmds = sizeof(commands) / sizeof(command), cmdlen; while(!ok && i < numcmds) { cmdlen = strlen(commands[i].command); if(!(ok = !strncmp(commands[i].command, cmdline, cmdlen))) i++; } if(ok) { ok = commands[i].handler(cmdline + cmdlen); if(commands[i].report) serialWriteLn(ok ? "OK" : "FAILED")); } else serialWriteLn("Bad command"); } For further reading see http://www.barrgroup.com/Embedded-Systems/How-To/C-Function-Pointers
     
     
     
  3. Like
    terjeio got a reaction from pine in Compact command parser/dispatcher example   
    A command parser/dispatcher example from my CO2 laser engraver codebase, using a struct array containing the commands and associated  pointer to functions. A lot cleaner (and easier to maintain) than switch/case statements or if/else constructs...
    Functions get called with a pointer to the command tail for local parameter parsing.
    The struct array data are all placed in flash.
    typedef struct { char const *const command; bool (*const handler)(char *); const bool report; } command; bool query (char* params); bool start (char* params); bool moveXrel (char* params); bool moveYrel (char* params); bool moveZrel (char* params); bool XHome (char* params); bool YHome (char* params); bool ZHome (char* params); bool XYHome (char* params); bool zeroAllAxes (char* params); bool laser (char* params); bool setLaserPower (char* params); bool setImageDPI (char* params); bool setPulseDutyCycle (char* params); bool enableCoolant (char* params); bool enableAirAssist (char* params); bool setMode (char* params); bool getPosition (char* params); bool setPPI (char* params); bool setPulseWidth (char* params); bool enableExhaustFan (char* params); bool setEngravingSpeed (char* params); bool getStatus (char* params); bool setEchoMode (char* params); bool setAMode (char* params); bool setPWROffset (char* params); bool loadProfile (char* params); bool setXBcomp (char* params); void exeCommand (char *cmdline) { static const command commands[] = { "?", &query, true, "Power:", &setLaserPower, true, "DutyCycle:", &setPulseDutyCycle, true, "PulseWidth:", &setPulseWidth, true, "DPI:", &setImageDPI, true, "Start:", &start, true, "X:", &moveXrel, true, "Y:", &moveYrel, true, "Z:", &moveZrel, true, "HomeXY", &XYHome, true, "HomeX", &XHome, true, "HomeY", &YHome, true, "HomeZ", &ZHome, true, "ZeroAll", &zeroAllAxes, true, "Laser:", &laser, true, "Coolant:", &enableCoolant, true, "Air:", &enableAirAssist, true, "Mach3:", &setMode, true, "Pos", &getPosition, false, "PPI:", &setPPI, true, "Exhaust:", &enableExhaustFan, true, "Speed:", &setEngravingSpeed, true, "Status", &getStatus, false, "ASelect:", &setAMode, true, "PWROffset:", &setPWROffset, true, "LoadProfile:", &loadProfile, true, "XBComp:", &setXBcomp, true, "Echo:", &setEchoMode, false }; bool ok = false; uint32_t i = 0, numcmds = sizeof(commands) / sizeof(command), cmdlen; while(!ok && i < numcmds) { cmdlen = strlen(commands[i].command); if(!(ok = !strncmp(commands[i].command, cmdline, cmdlen))) i++; } if(ok) { ok = commands[i].handler(cmdline + cmdlen); if(commands[i].report) serialWriteLn(ok ? "OK" : "FAILED")); } else serialWriteLn("Bad command"); } For further reading see http://www.barrgroup.com/Embedded-Systems/How-To/C-Function-Pointers
     
     
     
  4. Like
    terjeio got a reaction from zeke in Compact command parser/dispatcher example   
    A command parser/dispatcher example from my CO2 laser engraver codebase, using a struct array containing the commands and associated  pointer to functions. A lot cleaner (and easier to maintain) than switch/case statements or if/else constructs...
    Functions get called with a pointer to the command tail for local parameter parsing.
    The struct array data are all placed in flash.
    typedef struct { char const *const command; bool (*const handler)(char *); const bool report; } command; bool query (char* params); bool start (char* params); bool moveXrel (char* params); bool moveYrel (char* params); bool moveZrel (char* params); bool XHome (char* params); bool YHome (char* params); bool ZHome (char* params); bool XYHome (char* params); bool zeroAllAxes (char* params); bool laser (char* params); bool setLaserPower (char* params); bool setImageDPI (char* params); bool setPulseDutyCycle (char* params); bool enableCoolant (char* params); bool enableAirAssist (char* params); bool setMode (char* params); bool getPosition (char* params); bool setPPI (char* params); bool setPulseWidth (char* params); bool enableExhaustFan (char* params); bool setEngravingSpeed (char* params); bool getStatus (char* params); bool setEchoMode (char* params); bool setAMode (char* params); bool setPWROffset (char* params); bool loadProfile (char* params); bool setXBcomp (char* params); void exeCommand (char *cmdline) { static const command commands[] = { "?", &query, true, "Power:", &setLaserPower, true, "DutyCycle:", &setPulseDutyCycle, true, "PulseWidth:", &setPulseWidth, true, "DPI:", &setImageDPI, true, "Start:", &start, true, "X:", &moveXrel, true, "Y:", &moveYrel, true, "Z:", &moveZrel, true, "HomeXY", &XYHome, true, "HomeX", &XHome, true, "HomeY", &YHome, true, "HomeZ", &ZHome, true, "ZeroAll", &zeroAllAxes, true, "Laser:", &laser, true, "Coolant:", &enableCoolant, true, "Air:", &enableAirAssist, true, "Mach3:", &setMode, true, "Pos", &getPosition, false, "PPI:", &setPPI, true, "Exhaust:", &enableExhaustFan, true, "Speed:", &setEngravingSpeed, true, "Status", &getStatus, false, "ASelect:", &setAMode, true, "PWROffset:", &setPWROffset, true, "LoadProfile:", &loadProfile, true, "XBComp:", &setXBcomp, true, "Echo:", &setEchoMode, false }; bool ok = false; uint32_t i = 0, numcmds = sizeof(commands) / sizeof(command), cmdlen; while(!ok && i < numcmds) { cmdlen = strlen(commands[i].command); if(!(ok = !strncmp(commands[i].command, cmdline, cmdlen))) i++; } if(ok) { ok = commands[i].handler(cmdline + cmdlen); if(commands[i].report) serialWriteLn(ok ? "OK" : "FAILED")); } else serialWriteLn("Bad command"); } For further reading see http://www.barrgroup.com/Embedded-Systems/How-To/C-Function-Pointers
     
     
     
  5. Like
    terjeio got a reaction from yyrkoon in Compact command parser/dispatcher example   
    A command parser/dispatcher example from my CO2 laser engraver codebase, using a struct array containing the commands and associated  pointer to functions. A lot cleaner (and easier to maintain) than switch/case statements or if/else constructs...
    Functions get called with a pointer to the command tail for local parameter parsing.
    The struct array data are all placed in flash.
    typedef struct { char const *const command; bool (*const handler)(char *); const bool report; } command; bool query (char* params); bool start (char* params); bool moveXrel (char* params); bool moveYrel (char* params); bool moveZrel (char* params); bool XHome (char* params); bool YHome (char* params); bool ZHome (char* params); bool XYHome (char* params); bool zeroAllAxes (char* params); bool laser (char* params); bool setLaserPower (char* params); bool setImageDPI (char* params); bool setPulseDutyCycle (char* params); bool enableCoolant (char* params); bool enableAirAssist (char* params); bool setMode (char* params); bool getPosition (char* params); bool setPPI (char* params); bool setPulseWidth (char* params); bool enableExhaustFan (char* params); bool setEngravingSpeed (char* params); bool getStatus (char* params); bool setEchoMode (char* params); bool setAMode (char* params); bool setPWROffset (char* params); bool loadProfile (char* params); bool setXBcomp (char* params); void exeCommand (char *cmdline) { static const command commands[] = { "?", &query, true, "Power:", &setLaserPower, true, "DutyCycle:", &setPulseDutyCycle, true, "PulseWidth:", &setPulseWidth, true, "DPI:", &setImageDPI, true, "Start:", &start, true, "X:", &moveXrel, true, "Y:", &moveYrel, true, "Z:", &moveZrel, true, "HomeXY", &XYHome, true, "HomeX", &XHome, true, "HomeY", &YHome, true, "HomeZ", &ZHome, true, "ZeroAll", &zeroAllAxes, true, "Laser:", &laser, true, "Coolant:", &enableCoolant, true, "Air:", &enableAirAssist, true, "Mach3:", &setMode, true, "Pos", &getPosition, false, "PPI:", &setPPI, true, "Exhaust:", &enableExhaustFan, true, "Speed:", &setEngravingSpeed, true, "Status", &getStatus, false, "ASelect:", &setAMode, true, "PWROffset:", &setPWROffset, true, "LoadProfile:", &loadProfile, true, "XBComp:", &setXBcomp, true, "Echo:", &setEchoMode, false }; bool ok = false; uint32_t i = 0, numcmds = sizeof(commands) / sizeof(command), cmdlen; while(!ok && i < numcmds) { cmdlen = strlen(commands[i].command); if(!(ok = !strncmp(commands[i].command, cmdline, cmdlen))) i++; } if(ok) { ok = commands[i].handler(cmdline + cmdlen); if(commands[i].report) serialWriteLn(ok ? "OK" : "FAILED")); } else serialWriteLn("Bad command"); } For further reading see http://www.barrgroup.com/Embedded-Systems/How-To/C-Function-Pointers
     
     
     
  6. Like
    terjeio got a reaction from Fmilburn in Compact command parser/dispatcher example   
    A command parser/dispatcher example from my CO2 laser engraver codebase, using a struct array containing the commands and associated  pointer to functions. A lot cleaner (and easier to maintain) than switch/case statements or if/else constructs...
    Functions get called with a pointer to the command tail for local parameter parsing.
    The struct array data are all placed in flash.
    typedef struct { char const *const command; bool (*const handler)(char *); const bool report; } command; bool query (char* params); bool start (char* params); bool moveXrel (char* params); bool moveYrel (char* params); bool moveZrel (char* params); bool XHome (char* params); bool YHome (char* params); bool ZHome (char* params); bool XYHome (char* params); bool zeroAllAxes (char* params); bool laser (char* params); bool setLaserPower (char* params); bool setImageDPI (char* params); bool setPulseDutyCycle (char* params); bool enableCoolant (char* params); bool enableAirAssist (char* params); bool setMode (char* params); bool getPosition (char* params); bool setPPI (char* params); bool setPulseWidth (char* params); bool enableExhaustFan (char* params); bool setEngravingSpeed (char* params); bool getStatus (char* params); bool setEchoMode (char* params); bool setAMode (char* params); bool setPWROffset (char* params); bool loadProfile (char* params); bool setXBcomp (char* params); void exeCommand (char *cmdline) { static const command commands[] = { "?", &query, true, "Power:", &setLaserPower, true, "DutyCycle:", &setPulseDutyCycle, true, "PulseWidth:", &setPulseWidth, true, "DPI:", &setImageDPI, true, "Start:", &start, true, "X:", &moveXrel, true, "Y:", &moveYrel, true, "Z:", &moveZrel, true, "HomeXY", &XYHome, true, "HomeX", &XHome, true, "HomeY", &YHome, true, "HomeZ", &ZHome, true, "ZeroAll", &zeroAllAxes, true, "Laser:", &laser, true, "Coolant:", &enableCoolant, true, "Air:", &enableAirAssist, true, "Mach3:", &setMode, true, "Pos", &getPosition, false, "PPI:", &setPPI, true, "Exhaust:", &enableExhaustFan, true, "Speed:", &setEngravingSpeed, true, "Status", &getStatus, false, "ASelect:", &setAMode, true, "PWROffset:", &setPWROffset, true, "LoadProfile:", &loadProfile, true, "XBComp:", &setXBcomp, true, "Echo:", &setEchoMode, false }; bool ok = false; uint32_t i = 0, numcmds = sizeof(commands) / sizeof(command), cmdlen; while(!ok && i < numcmds) { cmdlen = strlen(commands[i].command); if(!(ok = !strncmp(commands[i].command, cmdline, cmdlen))) i++; } if(ok) { ok = commands[i].handler(cmdline + cmdlen); if(commands[i].report) serialWriteLn(ok ? "OK" : "FAILED")); } else serialWriteLn("Bad command"); } For further reading see http://www.barrgroup.com/Embedded-Systems/How-To/C-Function-Pointers
     
     
     
  7. Like
    terjeio got a reaction from bluehash in Compact command parser/dispatcher example   
    A command parser/dispatcher example from my CO2 laser engraver codebase, using a struct array containing the commands and associated  pointer to functions. A lot cleaner (and easier to maintain) than switch/case statements or if/else constructs...
    Functions get called with a pointer to the command tail for local parameter parsing.
    The struct array data are all placed in flash.
    typedef struct { char const *const command; bool (*const handler)(char *); const bool report; } command; bool query (char* params); bool start (char* params); bool moveXrel (char* params); bool moveYrel (char* params); bool moveZrel (char* params); bool XHome (char* params); bool YHome (char* params); bool ZHome (char* params); bool XYHome (char* params); bool zeroAllAxes (char* params); bool laser (char* params); bool setLaserPower (char* params); bool setImageDPI (char* params); bool setPulseDutyCycle (char* params); bool enableCoolant (char* params); bool enableAirAssist (char* params); bool setMode (char* params); bool getPosition (char* params); bool setPPI (char* params); bool setPulseWidth (char* params); bool enableExhaustFan (char* params); bool setEngravingSpeed (char* params); bool getStatus (char* params); bool setEchoMode (char* params); bool setAMode (char* params); bool setPWROffset (char* params); bool loadProfile (char* params); bool setXBcomp (char* params); void exeCommand (char *cmdline) { static const command commands[] = { "?", &query, true, "Power:", &setLaserPower, true, "DutyCycle:", &setPulseDutyCycle, true, "PulseWidth:", &setPulseWidth, true, "DPI:", &setImageDPI, true, "Start:", &start, true, "X:", &moveXrel, true, "Y:", &moveYrel, true, "Z:", &moveZrel, true, "HomeXY", &XYHome, true, "HomeX", &XHome, true, "HomeY", &YHome, true, "HomeZ", &ZHome, true, "ZeroAll", &zeroAllAxes, true, "Laser:", &laser, true, "Coolant:", &enableCoolant, true, "Air:", &enableAirAssist, true, "Mach3:", &setMode, true, "Pos", &getPosition, false, "PPI:", &setPPI, true, "Exhaust:", &enableExhaustFan, true, "Speed:", &setEngravingSpeed, true, "Status", &getStatus, false, "ASelect:", &setAMode, true, "PWROffset:", &setPWROffset, true, "LoadProfile:", &loadProfile, true, "XBComp:", &setXBcomp, true, "Echo:", &setEchoMode, false }; bool ok = false; uint32_t i = 0, numcmds = sizeof(commands) / sizeof(command), cmdlen; while(!ok && i < numcmds) { cmdlen = strlen(commands[i].command); if(!(ok = !strncmp(commands[i].command, cmdline, cmdlen))) i++; } if(ok) { ok = commands[i].handler(cmdline + cmdlen); if(commands[i].report) serialWriteLn(ok ? "OK" : "FAILED")); } else serialWriteLn("Bad command"); } For further reading see http://www.barrgroup.com/Embedded-Systems/How-To/C-Function-Pointers
     
     
     
  8. Like
    terjeio got a reaction from yosh in Compact command parser/dispatcher example   
    A command parser/dispatcher example from my CO2 laser engraver codebase, using a struct array containing the commands and associated  pointer to functions. A lot cleaner (and easier to maintain) than switch/case statements or if/else constructs...
    Functions get called with a pointer to the command tail for local parameter parsing.
    The struct array data are all placed in flash.
    typedef struct { char const *const command; bool (*const handler)(char *); const bool report; } command; bool query (char* params); bool start (char* params); bool moveXrel (char* params); bool moveYrel (char* params); bool moveZrel (char* params); bool XHome (char* params); bool YHome (char* params); bool ZHome (char* params); bool XYHome (char* params); bool zeroAllAxes (char* params); bool laser (char* params); bool setLaserPower (char* params); bool setImageDPI (char* params); bool setPulseDutyCycle (char* params); bool enableCoolant (char* params); bool enableAirAssist (char* params); bool setMode (char* params); bool getPosition (char* params); bool setPPI (char* params); bool setPulseWidth (char* params); bool enableExhaustFan (char* params); bool setEngravingSpeed (char* params); bool getStatus (char* params); bool setEchoMode (char* params); bool setAMode (char* params); bool setPWROffset (char* params); bool loadProfile (char* params); bool setXBcomp (char* params); void exeCommand (char *cmdline) { static const command commands[] = { "?", &query, true, "Power:", &setLaserPower, true, "DutyCycle:", &setPulseDutyCycle, true, "PulseWidth:", &setPulseWidth, true, "DPI:", &setImageDPI, true, "Start:", &start, true, "X:", &moveXrel, true, "Y:", &moveYrel, true, "Z:", &moveZrel, true, "HomeXY", &XYHome, true, "HomeX", &XHome, true, "HomeY", &YHome, true, "HomeZ", &ZHome, true, "ZeroAll", &zeroAllAxes, true, "Laser:", &laser, true, "Coolant:", &enableCoolant, true, "Air:", &enableAirAssist, true, "Mach3:", &setMode, true, "Pos", &getPosition, false, "PPI:", &setPPI, true, "Exhaust:", &enableExhaustFan, true, "Speed:", &setEngravingSpeed, true, "Status", &getStatus, false, "ASelect:", &setAMode, true, "PWROffset:", &setPWROffset, true, "LoadProfile:", &loadProfile, true, "XBComp:", &setXBcomp, true, "Echo:", &setEchoMode, false }; bool ok = false; uint32_t i = 0, numcmds = sizeof(commands) / sizeof(command), cmdlen; while(!ok && i < numcmds) { cmdlen = strlen(commands[i].command); if(!(ok = !strncmp(commands[i].command, cmdline, cmdlen))) i++; } if(ok) { ok = commands[i].handler(cmdline + cmdlen); if(commands[i].report) serialWriteLn(ok ? "OK" : "FAILED")); } else serialWriteLn("Bad command"); } For further reading see http://www.barrgroup.com/Embedded-Systems/How-To/C-Function-Pointers
     
     
     
  9. Like
    terjeio got a reaction from Fred in Compact command parser/dispatcher example   
    A command parser/dispatcher example from my CO2 laser engraver codebase, using a struct array containing the commands and associated  pointer to functions. A lot cleaner (and easier to maintain) than switch/case statements or if/else constructs...
    Functions get called with a pointer to the command tail for local parameter parsing.
    The struct array data are all placed in flash.
    typedef struct { char const *const command; bool (*const handler)(char *); const bool report; } command; bool query (char* params); bool start (char* params); bool moveXrel (char* params); bool moveYrel (char* params); bool moveZrel (char* params); bool XHome (char* params); bool YHome (char* params); bool ZHome (char* params); bool XYHome (char* params); bool zeroAllAxes (char* params); bool laser (char* params); bool setLaserPower (char* params); bool setImageDPI (char* params); bool setPulseDutyCycle (char* params); bool enableCoolant (char* params); bool enableAirAssist (char* params); bool setMode (char* params); bool getPosition (char* params); bool setPPI (char* params); bool setPulseWidth (char* params); bool enableExhaustFan (char* params); bool setEngravingSpeed (char* params); bool getStatus (char* params); bool setEchoMode (char* params); bool setAMode (char* params); bool setPWROffset (char* params); bool loadProfile (char* params); bool setXBcomp (char* params); void exeCommand (char *cmdline) { static const command commands[] = { "?", &query, true, "Power:", &setLaserPower, true, "DutyCycle:", &setPulseDutyCycle, true, "PulseWidth:", &setPulseWidth, true, "DPI:", &setImageDPI, true, "Start:", &start, true, "X:", &moveXrel, true, "Y:", &moveYrel, true, "Z:", &moveZrel, true, "HomeXY", &XYHome, true, "HomeX", &XHome, true, "HomeY", &YHome, true, "HomeZ", &ZHome, true, "ZeroAll", &zeroAllAxes, true, "Laser:", &laser, true, "Coolant:", &enableCoolant, true, "Air:", &enableAirAssist, true, "Mach3:", &setMode, true, "Pos", &getPosition, false, "PPI:", &setPPI, true, "Exhaust:", &enableExhaustFan, true, "Speed:", &setEngravingSpeed, true, "Status", &getStatus, false, "ASelect:", &setAMode, true, "PWROffset:", &setPWROffset, true, "LoadProfile:", &loadProfile, true, "XBComp:", &setXBcomp, true, "Echo:", &setEchoMode, false }; bool ok = false; uint32_t i = 0, numcmds = sizeof(commands) / sizeof(command), cmdlen; while(!ok && i < numcmds) { cmdlen = strlen(commands[i].command); if(!(ok = !strncmp(commands[i].command, cmdline, cmdlen))) i++; } if(ok) { ok = commands[i].handler(cmdline + cmdlen); if(commands[i].report) serialWriteLn(ok ? "OK" : "FAILED")); } else serialWriteLn("Bad command"); } For further reading see http://www.barrgroup.com/Embedded-Systems/How-To/C-Function-Pointers
     
     
     
  10. Like
    terjeio reacted to Fred in MSP430G2 emulator.   
    I'd suggest taking a look at the schematic of the G2 Launchpad. There's no magic going on.
     
    Ignore the EMULATOR side. On the side labeled MSP-EXP430G2 there's nothing scary - just a couple of buttons, LEDs, some decoupling capacitors and pull up resistors. I've found that an appropriate Launchpad is a good starting point when designing your own board.
  11. Like
    terjeio got a reaction from yyrkoon in MSP430G2 emulator.   
    Different VCCs should not be a problem as long as within absolute maximum ratings, from specs: "Voltage applied to any pin: -0.3V to VCC + 0.3V". I pull up unused pins, and do not forget P3 for parts where the pins are not brought out to the real world,  eg. 20 pin versions of 2553.
  12. Like
    terjeio got a reaction from Fmilburn in Sensor design   
    Be aware reed switches are a bit slow to react - if the pulse is of short duration, less than half a millisecond or so, it may fail to provide a signal. Consult the datasheet if in doubt...
  13. Like
    terjeio reacted to Fmilburn in Sensor design   
    Hi @@Flatbits,
     
    As you state in your post, there are lots of ways to go about this.  Here are a couple of ideas just to get you started...
     
    1) Read the reflection of an infrared LED off of a target -  I used a cheap CNY70 to make a tachometer and posted some photos here: https://github.com/fmilburn3/Tachometer_CNY70/wiki .  The Energia code is here:  https://github.com/fmilburn3/Tachometer_CNY70/blob/master/Tachometer_CNY70.ino
     
    2) Use a hall sensor and magnet - An example is this rainfall measurement device:  photos:  https://github.com/fmilburn3/Rainfall_Hall/wiki  code: https://github.com/fmilburn3/Rainfall_Hall/blob/master/Rainfall_Hall_Test.ino
     
    3) Photointerrupter: https://github.com/fmilburn3/Photointerrupter_Sharp_GP1S53VJ000F. I also salvaged some out of an old printer along with the motors for speed control/measurement.  They work the same, just be careful to put a current limiting resistor on the diodes when you test them or you will likely burn them out. 
     
    4) Reed Switch:  These are kind of fragile compared to the above but they are cheap and I have used them successfully.
     
    In all of the above examples (all with a F5529 if I remember correctly) I used a continuous 3.3V input.  The output varies depending on the sensor and how you are using it.  For a reed switch it will be on or off.  I think on the CNY70 I used an analog read with the analog signal varying according to how strong the reflection is. You could also drive to on or off with an op amp There are a number of tutorials on this type stuff, usually for Arduinos.  They are simple devices and the code normally ports without a change.  Just remember that much of the Arduino stuff is 5V while the LaunchPad is 3.3V and handle accordingly.
     
    The sensors above are inexpensive and it is fun just to get one of each and see what they do....
     
     
  14. Like
    terjeio got a reaction from greeeg in PCB Laser Exposer/Printer   
    Here are the files for my PCB Exposer/Printer, it is the complete package including mechanical design files.
     

    The printer itself.
     

    Example - a power control PCB for Raspberry Pi - 40 x 40 mm.
     
    Code includes driver for MCP4725 DAC, buffered serial port driver, stepper motor control and command parsing for the MSP430G2553 used as the main controller.
     
    Code and design files:
     
    PCB Exposer - controller code for MSP430G2553.zip
    PCB Exposer - desktop application.zip
    PCB Exposer - mechanical design files in Vectric format.zip
    PCB Exposer - schematics and PCBs.zip
     
    Desktop application is coded in C#, schematics and PCBs in KiCad format.
     
    There is some more information to be found in this tread:
    http://forum.43oh.com/topic/4990-what-are-you-doing-right-now/page-5
     
    Terje
     
     
  15. Like
    terjeio got a reaction from tripwire in Program won't fit weirdness   
    MSP430G2553 has only 512 bytes RAM - so not much to play with for storing variables.
    You can change the alpha_num data stucture to const to save RAM if you do not need to change it.
    16KB is the amount of flash memory available.
  16. Like
    terjeio got a reaction from agaelema in PCB Laser Exposer/Printer   
    Here are the files for my PCB Exposer/Printer, it is the complete package including mechanical design files.
     

    The printer itself.
     

    Example - a power control PCB for Raspberry Pi - 40 x 40 mm.
     
    Code includes driver for MCP4725 DAC, buffered serial port driver, stepper motor control and command parsing for the MSP430G2553 used as the main controller.
     
    Code and design files:
     
    PCB Exposer - controller code for MSP430G2553.zip
    PCB Exposer - desktop application.zip
    PCB Exposer - mechanical design files in Vectric format.zip
    PCB Exposer - schematics and PCBs.zip
     
    Desktop application is coded in C#, schematics and PCBs in KiCad format.
     
    There is some more information to be found in this tread:
    http://forum.43oh.com/topic/4990-what-are-you-doing-right-now/page-5
     
    Terje
     
     
  17. Like
    terjeio got a reaction from zeke in PCB Laser Exposer/Printer   
    Improved homing cycle has led to (near) perfect registration of solder mask.
     

     
    Layer registration mark, lines are ~0.1mm wide.
     

     
    Part of TSSOP 20 footprint, mask is way past "use before" date so some problems with spots appearing.
     
    I have started to apply the Riston film on wet PCBs, dust problem nearly gone - easier to handle.
     
    Terje
     
  18. Like
    terjeio got a reaction from agaelema in PCB Laser Exposer/Printer   
    Improved homing cycle has led to (near) perfect registration of solder mask.
     

     
    Layer registration mark, lines are ~0.1mm wide.
     

     
    Part of TSSOP 20 footprint, mask is way past "use before" date so some problems with spots appearing.
     
    I have started to apply the Riston film on wet PCBs, dust problem nearly gone - easier to handle.
     
    Terje
     
  19. Like
    terjeio got a reaction from Fmilburn in What are you doing right now..?   
    Making a stencil for my new Tiva based CO2 Laser controller, got the boards from China today. I have ported grbl to Tiva, I am going to integrate it into my own code so will make it into a library.
     
    Here is a video of the laser in action if you have nothing better to do:
     
    https://youtu.be/nkeuVN_bmTo
     
    Terje
  20. Like
    terjeio got a reaction from Rickta59 in What are you doing right now..?   
    Making a stencil for my new Tiva based CO2 Laser controller, got the boards from China today. I have ported grbl to Tiva, I am going to integrate it into my own code so will make it into a library.
     
    Here is a video of the laser in action if you have nothing better to do:
     
    https://youtu.be/nkeuVN_bmTo
     
    Terje
  21. Like
    terjeio reacted to Fmilburn in 4 x 6 cm Projects   
    I have had a package of those 4x6 cm pcb boards with 0.1" spacing for a while and realized this evening that I was about to run out.  They are a good and useful size.  Wondering where they all went I rummaged around and found these and there are more scattered about in various enclosed projects.
     

     
    I am in the habit of soldering one up if it is on the breadboard and I think there is a fair chance I might use it again.  I like to do this even when I am going to have PCBs fabricated.  From top left and going clockwise they are a RFID BoosterPack reader, a Nokia 5110 BoosterPack, an INA125P BoosterPack paired with a strain gauge, a breadboard BoosterPack, a MSP430G2553 Prototyping Board, and a MSP430G2955 Prototyping Board.  The G2955 is a 38 pin TSSOP so it is on an adapter.   The latest one is the Nokia 5110 I put together this afternoon.
     

     
    I had the idea that SMD parts might be directly soldered to these boards.  Here is a 0805 resistor soldered onto a piece of scrap (crooked but OK) with a SOT-223-4 part next to it.  The SOT-223 isn't soldered, I just put it there to show it aligned fairly well.  I ordered an inexpensive selection of 0805 resistors and capacitors and will be using them instead of through hole components to see how they do.
     

     
    Meanwhile, I haven't given up having PCBs fabricated, but this is handy.  What I really need want is a CNC mill.
  22. Like
    terjeio got a reaction from roadrunner84 in PCB Laser Exposer/Printer   
    Here are the files for my PCB Exposer/Printer, it is the complete package including mechanical design files.
     

    The printer itself.
     

    Example - a power control PCB for Raspberry Pi - 40 x 40 mm.
     
    Code includes driver for MCP4725 DAC, buffered serial port driver, stepper motor control and command parsing for the MSP430G2553 used as the main controller.
     
    Code and design files:
     
    PCB Exposer - controller code for MSP430G2553.zip
    PCB Exposer - desktop application.zip
    PCB Exposer - mechanical design files in Vectric format.zip
    PCB Exposer - schematics and PCBs.zip
     
    Desktop application is coded in C#, schematics and PCBs in KiCad format.
     
    There is some more information to be found in this tread:
    http://forum.43oh.com/topic/4990-what-are-you-doing-right-now/page-5
     
    Terje
     
     
  23. Like
    terjeio got a reaction from B@tto in G2553 and i2c slave   
    Here are my interrupt handlers, I read data into a buffer in TX_ISR and process them when a STOP interrupt is raised in RX_ISR. I am not using Energia so cannot replicate your code, but this approach may be of help?
    #pragma vector = USCIAB0TX_VECTOR __interrupt void USCIAB0TX_ISR(void) { if(IFG2 & UCB0TXIFG) { if(i == 0) xmitbuffer = x ? transfer.x : transfer.y; UCB0TXBUF = xmitbuffer & 0xFF; if(++i == 4) { i = 0; x = !x; } else xmitbuffer = xmitbuffer >> 8; } if(IFG2 & UCB0RXIFG) { if(i2cRXCount < 3) { x = UCB0RXBUF; *pi2cData++ = x; i2cRXCount++; } } } #pragma vector = USCIAB0RX_VECTOR __interrupt void USCIAB0RX_ISR(void) { unsigned char intstate = UCB0STAT; if(intstate & UCGC) { // General call? UCB0STAT &= ~UCGC; // Clear interrupt flag return; // and return } if(intstate & UCSTTIFG) { x = 1; i = 0; i2cRXCount = 0; pi2cData = i2cData; transfer.x = new.x; // read current position transfer.y = new.y; // into transfer copy } if((intstate & UCSTPIFG) && i2cRXCount) { switch(i2cData[0]) { case 1: // Reset counters P1OUT |= CLR; current.x = 0; current.y = 0; previous.x = 0; previous.y = 0; P1OUT &= ~CLR; break; case 2: // Set pulse width if(i2cRXCount == 3) pulseuS = (i2cData[1] | (i2cData[2] << 8)) * 16; break; case 3: // Set PPI if(i2cRXCount == 3) { ppi = (i2cData[1] | (i2cData[2] << 8)); ppiDist = PPICORE / ppi; } break; case 4: // Enable laser if(i2cRXCount == 2) enabled = i2cData[1]; break; } i2cRXCount = 0; } UCB0STAT &= ~intstate; // Clear interrupt flags }
  24. Like
    terjeio got a reaction from zeke in PCB Laser Exposer/Printer   
    Here are the files for my PCB Exposer/Printer, it is the complete package including mechanical design files.
     

    The printer itself.
     

    Example - a power control PCB for Raspberry Pi - 40 x 40 mm.
     
    Code includes driver for MCP4725 DAC, buffered serial port driver, stepper motor control and command parsing for the MSP430G2553 used as the main controller.
     
    Code and design files:
     
    PCB Exposer - controller code for MSP430G2553.zip
    PCB Exposer - desktop application.zip
    PCB Exposer - mechanical design files in Vectric format.zip
    PCB Exposer - schematics and PCBs.zip
     
    Desktop application is coded in C#, schematics and PCBs in KiCad format.
     
    There is some more information to be found in this tread:
    http://forum.43oh.com/topic/4990-what-are-you-doing-right-now/page-5
     
    Terje
     
     
  25. Like
    terjeio got a reaction from jazz in PCB Laser Exposer/Printer   
    Here are the files for my PCB Exposer/Printer, it is the complete package including mechanical design files.
     

    The printer itself.
     

    Example - a power control PCB for Raspberry Pi - 40 x 40 mm.
     
    Code includes driver for MCP4725 DAC, buffered serial port driver, stepper motor control and command parsing for the MSP430G2553 used as the main controller.
     
    Code and design files:
     
    PCB Exposer - controller code for MSP430G2553.zip
    PCB Exposer - desktop application.zip
    PCB Exposer - mechanical design files in Vectric format.zip
    PCB Exposer - schematics and PCBs.zip
     
    Desktop application is coded in C#, schematics and PCBs in KiCad format.
     
    There is some more information to be found in this tread:
    http://forum.43oh.com/topic/4990-what-are-you-doing-right-now/page-5
     
    Terje
     
     
×
×
  • Create New...