Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ extern "C" {
}
void _delay_ms(const int delay);
void delayMicroseconds(unsigned long);
uint32_t millis();
unsigned long millis();
uint64_t micros();

//IO functions
Expand Down
4 changes: 2 additions & 2 deletions src/MarlinSimulator/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Application::Application() {
user_interface.addElement<UiWindow>("Pin List", [this](UiWindow* window){
for (auto p : pin_array) {
bool value = Gpio::get_pin_value(p.pin);
if(ImGui::Checkbox((std::string("##") + p.name).c_str(), &value)) {
if (ImGui::Checkbox((std::string("##") + p.name).c_str(), &value)) {
Gpio::set(p.pin, value);
}
ImGui::SameLine();
Expand Down Expand Up @@ -169,7 +169,7 @@ Application::Application() {
static pin_type monitor_pin = X_STEP_PIN;
static const char* label = "Select Pin";
static char* active_label = (char *)label;
if(ImGui::BeginCombo("##Select Pin", active_label)) {
if (ImGui::BeginCombo("##Select Pin", active_label)) {
for (auto p : pin_array) {
if (ImGui::Selectable(p.name, p.pin == monitor_pin)) {
monitor_pin = p.pin;
Expand Down
15 changes: 7 additions & 8 deletions src/MarlinSimulator/execution_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ std::deque<KernelTimer*> Kernel::isr_stack;
bool Kernel::quit_requested = false;
std::atomic_uint64_t Kernel::isr_timing_error = 0;

bool Kernel::is_initialized(bool known_state) {
static bool is_running = known_state;
bool Kernel::is_initialized(const bool known_state/*=false*/) {
static bool is_running = false;
is_running = is_running || known_state;
return is_running;
}

bool Kernel::execute_loop( uint64_t max_end_ticks) {
bool Kernel::execute_loop(uint64_t max_end_ticks) {
// Marlin often gets into reentrant loops, this is the only way to unroll out of that call stack early
if (quit_requested) throw (std::runtime_error("Quit Requested"));
if (debug_break_flag) { debug_break_flag = false; debug_break(); }
Expand Down Expand Up @@ -76,7 +76,6 @@ bool Kernel::execute_loop( uint64_t max_end_ticks) {
serial_stream_3.receive_buffer.write((uint8_t *)buffer, count);
}


uint64_t current_ticks = TimeControl::getTicks();
uint64_t current_priority = std::numeric_limits<uint64_t>::max();
auto stack_size = isr_stack.size();
Expand All @@ -94,7 +93,7 @@ bool Kernel::execute_loop( uint64_t max_end_ticks) {
}
}

if (next_isr != nullptr ) {
if (next_isr != nullptr) {
if (current_ticks > lowest_isr) {
isr_timing_error = TimeControl::ticksToNanos(current_ticks - lowest_isr);
next_isr->source_offset = current_ticks; // late interrupt
Expand All @@ -120,7 +119,7 @@ uint64_t Kernel::TimeControl::nanos() {
}

// if a thread wants to wait, see what should be executed during that wait
void Kernel::delayCycles(uint64_t cycles) {
void Kernel::delayCycles(const uint64_t cycles) {
if (is_initialized()) {
auto end = TimeControl::getTicks() + cycles;
while (execute_loop(end) && TimeControl::getTicks() < end);
Expand All @@ -131,13 +130,13 @@ void Kernel::delayCycles(uint64_t cycles) {
// this is needed for when marlin loops idle waiting for an event with no delays (syncronize)
void Kernel::yield() {
if (is_initialized()) {
if(isr_stack.size() == 0) {
if (isr_stack.size() == 0) {
// Kernel not started?
TimeControl::addTicks(TimeControl::nanosToTicks(100));
return;
}
auto max_yield = isr_stack.back()->next_interrupt(TimeControl::frequency);
if(!execute_loop(max_yield)) { // dont wait longer than this threads exec period
if (!execute_loop(max_yield)) { // dont wait longer than this threads exec period
TimeControl::setTicks(max_yield);
isr_stack.back()->source_offset = max_yield; // there was nothing to run, and we now overrun our next cycle.
}
Expand Down
40 changes: 16 additions & 24 deletions src/MarlinSimulator/execution_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ struct KernelTimer {
bool enabled() { return active; }
void disable() { active = false; }

// in timer frequency
// In timer frequency
void set_compare(const uint64_t compare) { this->compare = compare; }
uint64_t get_compare() { return compare; }
uint64_t get_count(const uint64_t source_count, const uint64_t source_frequency) { return tickConvertFrequency(source_count - source_offset, source_frequency, timer_frequency); }


void set_isr(std::string name, void (*callback)()) {
isr_function = {callback};
this->name = name;
Expand All @@ -65,9 +64,9 @@ class Kernel {
auto now = clock.now();
auto delta = now - last_clock_read;
uint64_t delta_uint64 = std::chrono::duration_cast<std::chrono::nanoseconds>(delta).count();
if(delta_uint64 > std::numeric_limits<std::uint64_t>::max() - ONE_BILLION) {
if (delta_uint64 > std::numeric_limits<std::uint64_t>::max() - ONE_BILLION) {
//printf("rt info: %ld : %f\n", delta_uint64, realtime_scale.load());
//aparently time can go backwards, thread issue?
// Apparently time can go backwards, thread issue?
delta_uint64 = 0;
}
uint64_t delta_uint64_scaled = delta_uint64 * realtime_scale;
Expand All @@ -82,7 +81,7 @@ class Kernel {
if (getRealtimeTicks() > getTicks() || realtime_scale > 99.0f) {
realtime_nanos = SimulationRuntime::nanos();
} else while (getTicks() > getRealtimeTicks()) {
if (quit_requested) throw (std::runtime_error("Quit Requested")); // quit program when stuck at 0 speed
if (quit_requested) throw (std::runtime_error("Quit Requested")); // Quit program when stuck at 0 speed
updateRealtime();
realtime_scale > 20.0f ? std::this_thread::yield() : std::this_thread::sleep_for(std::chrono::nanoseconds(1));
}
Expand Down Expand Up @@ -157,22 +156,22 @@ class Kernel {
inline static void timerInit(uint8_t timer_id, uint32_t rate) {
if (timer_id < timers.size()) {
timers[timer_id].timer_frequency = rate;
// printf("Timer[%d] Initialised( rate: %d )\n", timer_id, rate);
//printf("Timer[%d] Initialised( rate: %d )\n", timer_id, rate);
}
}

inline static void timerStart(uint8_t timer_id, uint32_t interrupt_frequency) {
if (timer_id < timers.size()) {
timers[timer_id].compare = timers[timer_id].timer_frequency / interrupt_frequency;
timers[timer_id].source_offset = TimeControl::getTicks();
// printf("Timer[%d] Started( frequency: %d compare: %ld)\n", timer_id, interrupt_frequency, timers[timer_id].compare);
//printf("Timer[%d] Started( frequency: %d compare: %ld)\n", timer_id, interrupt_frequency, timers[timer_id].compare);
}
}

inline static void timerEnable(uint8_t timer_id) {
if (timer_id < timers.size()) {
timers[timer_id].active = true;
// printf("Timer[%d] Enabled\n", timer_id);
//printf("Timer[%d] Enabled\n", timer_id);
}
}

Expand All @@ -190,9 +189,7 @@ class Kernel {
}

inline static void timerSetCompare(uint8_t timer_id, uint64_t compare) {
if (timer_id < timers.size()) {
timers[timer_id].compare = compare;
}
if (timer_id < timers.size()) timers[timer_id].compare = compare;
}

inline static uint64_t timerGetCount(uint8_t timer_id) {
Expand All @@ -214,13 +211,13 @@ class Kernel {

// To avoid issues with global initialization order, this should be called with a true value
// to enable operation of execute_loop.
static bool is_initialized(bool known_state = false);
static bool is_initialized(const bool known_state = false);

//execute highest priority thread with closest interrupt, return true if something was executed
// Execute highest priority thread with closest interrupt, return true if something was executed
static bool execute_loop(uint64_t max_end_ticks = std::numeric_limits<uint64_t>::max());
// if a thread wants to wait, see what should be executed during that wait
static void delayCycles(uint64_t cycles);
// this was neede for when marlin loops idle waiting for an event with no delays
// If a thread wants to wait, see what should be executed during that wait
static void delayCycles(const uint64_t cycles);
// This was needed for when marlin loops idle waiting for an event with no delays
static void yield();

static void shutdown() {
Expand All @@ -230,14 +227,9 @@ class Kernel {

static void execution_break() { debug_break_flag = true; }

//Timers
inline static void disableInterrupts() {
timers_active = false;
}

inline static void enableInterrupts() {
timers_active = true;
}
// Timers
inline static void disableInterrupts() { timers_active = false; }
inline static void enableInterrupts() { timers_active = true; }

inline static void delayNanos(uint64_t ns) {
delayCycles(TimeControl::nanosToTicks(ns));
Expand Down
2 changes: 1 addition & 1 deletion src/MarlinSimulator/hardware/Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Button : public VirtualPrinter::Component {

void ui_widget() {
ImGui::Button("State");
if(ImGui::IsItemActive()) {
if (ImGui::IsItemActive()) {
active = invert_logic;
} else {
active = !invert_logic;
Expand Down
8 changes: 6 additions & 2 deletions src/MarlinSimulator/hardware/HD44780Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ HD44780Device::HD44780Device(pin_type rs, pin_type en, pin_type d4, pin_type d5,
active_rom = hd44780_a00_rom;
#elif DISPLAY_CHARSET_HD44780 == WESTERN
active_rom = hd44780_a02_rom;
#elif DISPLAY_CHARSET_HD44780 == CYRILLIC
active_rom = hd44780_a02_rom;
#warning "CYRILLIC HD44780 Character ROM not available. Falling back to WESTERN."
#else
#error Unavailable HD44780 Character ROM
active_rom = hd44780_a02_rom;
#warning "Unknown HD44780 Character ROM. Falling back to WESTERN."
#endif
}

Expand Down Expand Up @@ -143,7 +147,7 @@ void HD44780Device::update() {
}

void HD44780Device::interrupt(GpioEvent& ev) {
if(ev.pin_id == en_pin && ev.event == GpioEvent::RISE) {
if (ev.pin_id == en_pin && ev.event == GpioEvent::RISE) {
//read the bus
data_byte |= (Gpio::get_pin_value(d7_pin) << 3 | Gpio::get_pin_value(d6_pin) << 2 | Gpio::get_pin_value(d5_pin) << 1 |Gpio::get_pin_value(d4_pin)) << (4 * !data_low_nibble);
data_low_nibble = !data_low_nibble;
Expand Down
2 changes: 1 addition & 1 deletion src/MarlinSimulator/hardware/bus/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class SpiBus {
callbacks.push_back(std::function<void(SpiEvent&)>(args...));
}

void acquire() { if(busy == true) printf("spi bus contention!\n"); busy = true; }
void acquire() { if (busy) printf("spi bus contention!\n"); busy = true; }
void release() { busy = false; }
bool is_busy() { return busy; }

Expand Down
30 changes: 15 additions & 15 deletions src/MarlinSimulator/marlin_arduino_impl/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,22 @@ long Stream::parseInt(char skipChar)

c = peekNextDigit();
// ignore non numeric leading characters
if(c < 0)
if (c < 0)
return 0; // zero returned if timeout

do{
if(c == skipChar)
do {
if (c == skipChar)
; // ignore this charactor
else if(c == '-')
else if (c == '-')
isNegative = true;
else if(c >= '0' && c <= '9') // is c a digit?
else if (c >= '0' && c <= '9') // is c a digit?
value = value * 10 + c - '0';
read(); // consume the character we got with peek
c = timedPeek();
}
while( (c >= '0' && c <= '9') || c == skipChar );

if(isNegative)
if (isNegative)
value = -value;
return value;
}
Expand All @@ -165,30 +165,30 @@ float Stream::parseFloat(char skipChar){
float fraction = 1.0;

c = peekNextDigit();
// ignore non numeric leading characters
if(c < 0)
// ignore non numeric leading characters
if (c < 0)
return 0; // zero returned if timeout

do{
if(c == skipChar)
do {
if (c == skipChar)
; // ignore
else if(c == '-')
else if (c == '-')
isNegative = true;
else if (c == '.')
isFraction = true;
else if(c >= '0' && c <= '9') { // is c a digit?
else if (c >= '0' && c <= '9') { // is c a digit?
value = value * 10 + c - '0';
if(isFraction)
if (isFraction)
fraction *= 0.1;
}
read(); // consume the character we got with peek
c = timedPeek();
}
while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );

if(isNegative)
if (isNegative)
value = -value;
if(isFraction)
if (isFraction)
return value * fraction;
else
return value;
Expand Down
5 changes: 5 additions & 0 deletions src/MarlinSimulator/marlin_arduino_impl/arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,18 @@ uint16_t analogRead(pin_t adc_pin) {
return Gpio::get(digitalPinToAnalogIndex(adc_pin));
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

char *dtostrf(double __val, signed char __width, unsigned char __prec, char *__s) {
char format_string[20];
snprintf(format_string, 20, "%%%d.%df", __width, __prec);
sprintf(__s, format_string, __val);
return __s;
}

#pragma GCC diagnostic pop

int32_t random(int32_t max) {
return rand() % max;
}
Expand Down
2 changes: 1 addition & 1 deletion src/MarlinSimulator/resources/resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace resource {
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
m_data.reserve(size + 1);
if(file.read(m_data.data(), size)) {
if (file.read(m_data.data(), size)) {
m_buffer = m_data.data();
m_data[size] = 0;
} else {
Expand Down
14 changes: 7 additions & 7 deletions src/MarlinSimulator/visualisation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void Visualisation::create() {
}

auto kin = virtual_printer.get_component<KinematicSystem>("Cartesian Kinematic System");
if(kin == nullptr) kin = virtual_printer.get_component<KinematicSystem>("Delta Kinematic System");
if (kin == nullptr) kin = virtual_printer.get_component<KinematicSystem>("Delta Kinematic System");
if (kin != nullptr && kin->state.effector_position.size() == extrusion.size()) {
size_t i = 0;
for (auto state : kin->state.effector_position) {
Expand Down Expand Up @@ -250,7 +250,7 @@ void Visualisation::update() {
}

void Visualisation::destroy() {
if(framebuffer != nullptr) {
if (framebuffer != nullptr) {
framebuffer->release();
delete framebuffer;
}
Expand Down Expand Up @@ -283,7 +283,7 @@ void Visualisation::set_head_position(size_t hotend_index, extruder_state& state
if (active_buffer != nullptr && active_buffer->size() > 1 && active_buffer->size() < renderer::MAX_BUFFER_SIZE) {

if (glm::length(glm::vec3(position) - glm::vec3(extruder.last_position)) > m_config.extrusion_segment_minimum_length) { // smooth out the path so the model renders with less geometry, rendering each individual step hurts the fps
if((points_are_collinear(position, active_buffer->cdata().end()[-3].position, active_buffer->cdata().end()[-2].position, m_config.extrusion_segment_collinearity_max_deviation) && extruder.extruding == extruder.last_extruding) || ( extruder.extruding == false && extruder.last_extruding == false)) {
if ((points_are_collinear(position, active_buffer->cdata().end()[-3].position, active_buffer->cdata().end()[-2].position, m_config.extrusion_segment_collinearity_max_deviation) && extruder.extruding == extruder.last_extruding) || ( extruder.extruding == false && extruder.last_extruding == false)) {
// collinear and extrusion state has not changed so we can just change the current point.
active_buffer->data().end()[-2].position = position;
active_buffer->data().end()[-1].position = position;
Expand Down Expand Up @@ -464,18 +464,18 @@ void Visualisation::ui_viewport_callback(UiWindow* window) {
}

// IMGUI: Relative Mouse mode within a window does not seem to be supported through the imgui mouse api
if (mouse_captured && !last_mouse_captured) {
if (mouse_captured && !last_mouse_captured) { // Mouse button was just pressed
ImVec2 mouse_pos = ImGui::GetMousePos();
mouse_lock_pos = {mouse_pos.x, mouse_pos.y};
mouse_lock_pos = { mouse_pos.x, mouse_pos.y };
SDL_SetWindowGrab(SDL_GL_GetCurrentWindow(), SDL_TRUE);
SDL_SetRelativeMouseMode(SDL_TRUE);
SDL_GetRelativeMouseState(nullptr, nullptr);
} else if (!mouse_captured && last_mouse_captured) {
} else if (!mouse_captured && last_mouse_captured) { // Mouse button was just released
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_SetWindowGrab(SDL_GL_GetCurrentWindow(), SDL_FALSE);
SDL_WarpMouseInWindow(SDL_GL_GetCurrentWindow(), mouse_lock_pos.x, mouse_lock_pos.y);
SDL_GetRelativeMouseState(nullptr, nullptr);
} else if (mouse_captured) {
} else if (mouse_captured) { // Mouse button is being held
int rel_x, rel_y;
SDL_GetRelativeMouseState(&rel_x, &rel_y);
camera.rotation.x -= rel_x * 0.2;
Expand Down
1 change: 0 additions & 1 deletion src/MarlinSimulator/visualisation.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ class Visualisation {
std::shared_ptr<renderer::ShaderProgram> default_program;

bool mouse_captured = false;
bool input_state[6] = {};
glm::vec<2, int> mouse_lock_pos;

#define BED_NORMAL 0.0, 1.0, 0.0
Expand Down
2 changes: 1 addition & 1 deletion src/MarlinSimulator/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,5 @@ void* Window::getHandle() {
}

void Window::swap_buffers() {
if(window_impl::window_valid) SDL_GL_SwapWindow(window_impl::window);
if (window_impl::window_valid) SDL_GL_SwapWindow(window_impl::window);
}