void WRITE(pin_t pin, bool s) { bool old_state = state[pin]; uint64_t nseconds = sim_runtime_ns(); sim_assert(pin < PIN_NB, "WRITE: Pin number out of range"); if (direction[pin] == out) { state[pin] = s; } if (old_state != s) { record_pin(TRACE_PINS + pin, s, nseconds); #ifdef TRACE_ALL_PINS fgreen(); for (int i = 0; i < PIN_NB; i++) { if (state[i]) bred(); else bblack(); fputc('A' + i, stdout); } fbreset(); printf("\n"); #else bred(); if (s) sim_tick('A' + pin); else sim_tick('a' + pin); fbreset(); #endif } if (s && !old_state) { /* rising edge */ int axis = AXIS_NONE; int dir; switch (pin) { case X_STEP_PIN: dir = state[X_DIR_PIN] ? 1 : -1; axis = X_AXIS; break; case Y_STEP_PIN: dir = state[Y_DIR_PIN] ? 1 : -1; axis = Y_AXIS; break; case Z_STEP_PIN: dir = state[Z_DIR_PIN] ? 1 : -1; axis = Z_AXIS; break; case E_STEP_PIN: dir = state[E_DIR_PIN] ? 1 : -1; axis = E_AXIS; break; default: break; } if ( axis != AXIS_NONE ) { pos[axis] += dir; record_pin(TRACE_POS + axis, pos[axis], nseconds); print_pos(); } } }
/** Simulate min endstops. "on" at -10, "off" at 0. */ static void sim_endstop( int axis ) { bool on ; if (axis == AXIS_NONE) return; else if (pos[axis] <= -20) on = true; else if (pos[axis] >= 0) on = false; else return ; const char * strstate = on ? "ON" : "OFF"; int minpin; switch (axis) { case X_AXIS: #ifdef X_INVERT_MIN on = ! on; #endif minpin = X_MIN_PIN; break; case Y_AXIS: #ifdef Y_INVERT_MIN on = ! on; #endif minpin = Y_MIN_PIN; break; case Z_AXIS: #ifdef Z_INVERT_MIN on = ! on; #endif minpin = Z_MIN_PIN; break; default: return; } // No change if (state[minpin] == on) return; // Change the endstop state and report it state[minpin] = on; record_pin(TRACE_PINS + minpin, on, sim_runtime_ns()); bred(); if (on) sim_tick('A' + minpin); else sim_tick('a' + minpin); fbreset(); sim_info("%c-Endstop: %s", "XYZE???"[axis], strstate); }
static void timer1_isr(void) { const uint8_t tr = timer_reason; if ( ! sim_interrupts) { // Interrupts disabled. Schedule another callback in 10us. schedule_timer(10); return; } timer_reason = 0; cli(); #ifdef SIM_DEBUG uint64_t now = now_ns(); static unsigned int cc_1s = 0, prev_1s = 0; if ( ! clock_counter_1s && prev_1s) ++cc_1s; prev_1s = clock_counter_1s; //uint16_t now = sim_tick_counter(); uint64_t real = (now-begin) / 1000; uint64_t avr = cc_1s * 4 + clock_counter_1s; avr *= 250; avr += clock_counter_250ms * 10; avr += clock_counter_10ms; avr *= 1000 ; printf("test: Real: %us %u.%03ums AVR: %us %u.%03ums Real/AVR: %u\n", real / 1000000 , (real % 1000000)/1000 , real % 1000 , avr / 1000000 , (avr % 1000000)/1000 , avr % 1000 , real / (avr?avr:1) ); printf("test: 10ms=%u 250ms=%u 1s=%u total=%luns actual=%luns\n", clock_counter_10ms, clock_counter_250ms, clock_counter_1s, now - begin, now - then, sim_runtime_ns()); //printf(" timer1_isr tick_time=%04X now=%04X delta=%u total=%u\n", // TICK_TIME , now, now_us() - then, (now_us() - begin)/1000000 ) ; then = now; #endif if (tr & TIMER_OCR1A) TIMER1_COMPA_vect(); if (tr & TIMER_OCR1B) TIMER1_COMPB_vect(); sei(); // Setup next timer sim_timer_set(); }
void _WRITE(pin_t pin, bool s) { bool old_state = state[pin]; uint64_t nseconds = sim_runtime_ns(); sim_assert(pin < PIN_NB, "WRITE: Pin number out of range"); if (direction[pin] == out) { state[pin] = s; } if (old_state != s) { record_pin(TRACE_PINS + pin, s, nseconds); #ifdef TRACE_ALL_PINS fgreen(); for (int i = 0; i < PIN_NB; i++) { if (state[i]) bred(); else bblack(); fputc('A' + i, stdout); } fbreset(); printf("\n"); #else bred(); if (s) sim_tick('A' + pin); else sim_tick('a' + pin); fbreset(); #endif } if (s && !old_state) { /* rising edge */ int axis = AXIS_NONE; int dir; switch (pin) { case X_STEP_PIN: dir = state[X_DIR_PIN] ? 1 : -1; #ifdef X_INVERT_DIR dir = -dir; #endif axis = X_AXIS; break; case Y_STEP_PIN: dir = state[Y_DIR_PIN] ? 1 : -1; #ifdef Y_INVERT_DIR dir = -dir; #endif axis = Y_AXIS; break; case Z_STEP_PIN: dir = state[Z_DIR_PIN] ? 1 : -1; #ifdef Z_INVERT_DIR dir = -dir; #endif axis = Z_AXIS; break; case E_STEP_PIN: dir = state[E_DIR_PIN] ? 1 : -1; #ifdef E_INVERT_DIR dir = -dir; #endif axis = E_AXIS; break; default: break; } switch ( axis ) { #ifdef KINEMATICS_COREXY case X_AXIS: pos[X_AXIS] += dir; pos[Y_AXIS] += dir; break; case Y_AXIS: pos[X_AXIS] += dir; pos[Y_AXIS] -= dir; break; #endif case Z_AXIS: case E_AXIS: default: pos[axis] += 2 * dir; break; case AXIS_NONE: break; } if ( axis != AXIS_NONE ) { for (int a = X_AXIS; a <= E_AXIS; a++) record_pin(TRACE_POS + axis, pos[axis] / 2, nseconds); static uint64_t prev_ns = -1; if (prev_ns != nseconds) print_pos(); prev_ns = nseconds; for (int a = X_AXIS; a < E_AXIS; a++) sim_endstop(a); } } }