X-Git-Url: http://git.hcoop.net/clinton/Smoothieware.git/blobdiff_plain/a5bf7be70e6e4cdb80f3aac61d830ad36eda2c9b..4e71bfc96d6ad5bd338f3d6c16faefe5dbed2b8b:/src/libs/utils.cpp diff --git a/src/libs/utils.cpp b/src/libs/utils.cpp index de174c64..b3e60fa8 100644 --- a/src/libs/utils.cpp +++ b/src/libs/utils.cpp @@ -14,9 +14,11 @@ #include #include #include -using std::string; +#include + +#include "mbed.h" -volatile bool _isr_context = false; +using std::string; uint16_t get_checksum(const string &to_check) { @@ -131,7 +133,7 @@ string shift_parameter( string ¶meters ) } // Separate command from arguments -string get_arguments( string possible_command ) +string get_arguments( const string& possible_command ) { size_t beginning = possible_command.find_first_of(" "); if( beginning == string::npos ) { @@ -198,3 +200,78 @@ string absolute_from_relative( string path ) return cwd + '/' + path; } + +// FIXME this does not handle empty strings correctly +//split a string on a delimiter, return a vector of the split tokens +vector split(const char *str, char c) +{ + vector result; + + do { + const char *begin = str; + + while(*str != c && *str) + str++; + + result.push_back(string(begin, str)); + } while (0 != *str++); + + return result; +} + +// FIXME this does not handle empty strings correctly +// parse a number list "1.1,2.2,3.3" and return the numbers in a vector of floats +vector parse_number_list(const char *str) +{ + vector l= split(str, ','); + vector r; + for(auto& s : l){ + float x = strtof(s.c_str(), nullptr); + r.push_back(x); + } + return r; +} + +vector parse_number_list(const char *str, uint8_t radix) +{ + vector l= split(str, ','); + vector r; + for(auto& s : l){ + uint32_t x = strtol(s.c_str(), nullptr, radix); + r.push_back(x); + } + return r; +} + +int append_parameters(char *buf, std::vector> params, size_t bufsize) +{ + size_t n= 0; + for(auto &i : params) { + if(n >= bufsize) break; + buf[n++]= i.first; + n += snprintf(&buf[n], bufsize-n, "%1.4f ", i.second); + } + return n; +} + +string wcs2gcode(int wcs) { + string str= "G5"; + str.append(1, std::min(wcs, 5) + '4'); + if(wcs >= 6) { + str.append(".").append(1, '1' + (wcs - 6)); + } + return str; +} + +void safe_delay_ms(uint32_t delay) +{ + safe_delay_us(delay*1000); +} + +void safe_delay_us(uint32_t dus) +{ + uint32_t start = us_ticker_read(); + while ((us_ticker_read() - start) < dus) { + THEKERNEL->call_event(ON_IDLE); + } +}