Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a pwm reader component #42

Merged
merged 1 commit into from
Apr 27, 2024
Merged
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
4 changes: 4 additions & 0 deletions src/MarlinSimulator/hardware/Heater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "pinmapping.h"
#include "Heater.h"
#include "pwm_reader.h"

constexpr double absolute_zero_offset = -273.15;
double thermistor_ext_coef[] = {
Expand Down Expand Up @@ -40,6 +41,9 @@ Heater::Heater(pin_type heater_pin, pin_type adc_pin, heater_data heater, hotend
Gpio::attach(this->heater_pin, [this](GpioEvent& event){ this->interrupt(event); });
hotend_energy = hotend_ambient_temperature * (hotend_specific_heat * hotend_mass);
hotend_temperature = hotend_ambient_temperature;
static uint64_t elementid = 0;
add_component<PWMReader>("Element" + std::to_string(elementid), heater_pin);
++elementid;
}

Heater::~Heater() {
Expand Down
55 changes: 55 additions & 0 deletions src/MarlinSimulator/hardware/pwm_reader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <imgui.h>
#include "pinmapping.h"
#include "pwm_reader.h"

PWMReader::PWMReader(pin_type pwm_pin) : VirtualPrinter::Component("PWMReader"), pwm_pin(pwm_pin) {
Gpio::attach(this->pwm_pin, [this](GpioEvent& event){ this->interrupt(event); });
}

PWMReader::~PWMReader() {
}

void PWMReader::update() {

}

void PWMReader::ui_widget() {
if (pwm_mode == Software && Kernel::TimeControl::getTicks() - pwm_last_update > ((pwm_period * 10) % Kernel::TimeControl::nanosToTicks(Kernel::TimeControl::ONE_BILLION))) {
pwm_mode = Inactive;
pwm_duty = 0;
pwm_period = 0;
pwm_hightick = 0;
}
switch (pwm_mode) {
case Inactive:
ImGui::Text("Inactive (%s) Duty Cycle: %.2f%%", Gpio::get_pin_value(pwm_pin) ? "High" : "Low", Gpio::get_pin_value(pwm_pin) * 100.0f);
break;
case Software:
ImGui::Text("Software PWM (%.2fHz) Duty Cycle: %.2f%%", pwm_period ? float(Kernel::TimeControl::ONE_BILLION) / Kernel::TimeControl::ticksToNanos(pwm_period) : 0, pwm_period ? float(pwm_duty * 100) / pwm_period : 0);
break;
case Hardware:
ImGui::Text("Hardware PWM Duty Cycle : %.2f%%", pwm_period ? float(pwm_duty * 100) / pwm_period : 0);
break;
}
}

void PWMReader::interrupt(GpioEvent& ev) {
if (ev.pin_id == pwm_pin) {
double time_delta = Kernel::TimeControl::ticksToNanos(ev.timestamp - pwm_last_update) / (double)Kernel::TimeControl::ONE_BILLION;
pwm_last_update = ev.timestamp;
if (ev.event == ev.RISE) {
if (pwm_hightick) {
pwm_period = ev.timestamp - pwm_hightick;
pwm_mode = Software;
}
pwm_hightick = ev.timestamp;
} else if ( ev.event == ev.FALL) {
pwm_lowtick = ev.timestamp;
pwm_duty = ev.timestamp - pwm_hightick;
} else if ( ev.event == ev.SET_VALUE) { // Hardware PWM
pwm_mode = Hardware;
pwm_duty = Gpio::get_pin_value(pwm_pin);
pwm_period = 255;
}
}
}
31 changes: 31 additions & 0 deletions src/MarlinSimulator/hardware/pwm_reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#pragma once

#include <cmath>
#include "Gpio.h"
#include "../virtual_printer.h"

class PWMReader: public VirtualPrinter::Component {
public:
enum PwmMode {
Inactive,
Hardware,
Software
};

PWMReader(pin_type pwm_pin);
virtual ~PWMReader();

void interrupt(GpioEvent& ev);
void update();
void ui_widget();

pin_type pwm_pin;
uint64_t pwm_period = 0;
uint64_t pwm_duty = 0;
uint64_t pwm_hightick = 0;
uint64_t pwm_lowtick = 0;
uint64_t pwm_last_update = 0;
PwmMode pwm_mode = Inactive;
};
38 changes: 35 additions & 3 deletions src/MarlinSimulator/virtual_printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "hardware/FilamentRunoutSensor.h"
#include "hardware/NeoPixelDevice.h"
#include "hardware/KinematicSystem.h"
#include "hardware/pwm_reader.h"

#include "virtual_printer.h"

Expand Down Expand Up @@ -103,15 +104,46 @@ void VirtualPrinter::build() {
root->add_component<Heater>("Bed Heater", HEATER_BED_PIN, TEMP_BED_PIN, heater_data{12, 1.2}, hotend_data{325, 824, 0.897}, adc_data{4700, 12});
#endif

#if TEMP_SENSOR_CHAMBER
root->add_component<Heater>("Chamber Heater", HEATER_CHAMBER_PIN, TEMP_CHAMBER_PIN, heater_data{12, 1.2}, hotend_data{325, 824, 0.897}, adc_data{4700, 12});
#endif

#if ENABLED(FILAMENT_RUNOUT_SENSOR)
root->add_component<FilamentRunoutSensor>("Filament Runout Sensor 1", FIL_RUNOUT1_PIN, FIL_RUNOUT1_STATE);
#if NUM_RUNOUT_SENSORS > 1
root->add_component<FilamentRunoutSensor>("Filament Runout Sensor 2", FIL_RUNOUT2_PIN, FIL_RUNOUT2_STATE);
#endif
#if NUM_RUNOUT_SENSORS > 2
root->add_component<FilamentRunoutSensor>("Filament Runout Sensor 3", FIL_RUNOUT3_PIN, FIL_RUNOUT3_STATE);
#endif
#if NUM_RUNOUT_SENSORS > 3
root->add_component<FilamentRunoutSensor>("Filament Runout Sensor 4", FIL_RUNOUT4_PIN, FIL_RUNOUT4_STATE);
#endif
#if NUM_RUNOUT_SENSORS > 4
root->add_component<FilamentRunoutSensor>("Filament Runout Sensor 5", FIL_RUNOUT5_PIN, FIL_RUNOUT5_STATE);
#endif
#if NUM_RUNOUT_SENSORS > 5
root->add_component<FilamentRunoutSensor>("Filament Runout Sensor 6", FIL_RUNOUT6_PIN, FIL_RUNOUT6_STATE);
#endif
#if NUM_RUNOUT_SENSORS > 6
root->add_component<FilamentRunoutSensor>("Filament Runout Sensor 7", FIL_RUNOUT7_PIN, FIL_RUNOUT7_STATE);
#endif
#if NUM_RUNOUT_SENSORS > 7
root->add_component<FilamentRunoutSensor>("Filament Runout Sensor 8", FIL_RUNOUT8_PIN, FIL_RUNOUT8_STATE);
#endif
#endif

#ifdef FAN0_PIN
root->add_component<PWMReader>("Fan0", FAN0_PIN);
#endif

#if ENABLED(SPI_FLASH)
root->add_component<W25QxxDevice>("SPI Flash", spi_bus_by_pins<SPI_FLASH_SCK_PIN, SPI_FLASH_MOSI_PIN, SPI_FLASH_MISO_PIN>(), SPI_FLASH_CS_PIN, SPI_FLASH_SIZE);
#endif

#ifdef SDSUPPORT
root->add_component<SDCard>("SD Card", spi_bus_by_pins<SD_SCK_PIN, SD_MOSI_PIN, SD_MISO_PIN>(), SD_SS_PIN, SD_DETECT_PIN, SD_DETECT_STATE);
#endif
#if ENABLED(FILAMENT_RUNOUT_SENSOR)
root->add_component<FilamentRunoutSensor>("Filament Runout Sensor", FIL_RUNOUT1_PIN, FIL_RUNOUT_STATE);
#endif

#ifndef LCD_PINS_EN
#define LCD_PINS_EN LCD_PINS_ENABLE
Expand Down