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

Fix pin mapping #47

Merged
merged 4 commits into from
Jul 14, 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: 2 additions & 2 deletions include/pinmapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ constexpr pin_t analogInputToDigitalPin(const int8_t p) {
int16_t GET_PIN_MAP_INDEX(const pin_t pin);

// Test whether the pin is valid
bool VALID_PIN(const pin_t p);
bool isValidPin(const pin_t p);

// Get the analog index for a digital pin
int8_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t p);
int8_t digitalPinToAnalogIndex(const pin_t p);

// Test whether the pin is PWM
bool PWM_PIN(const pin_t p);
Expand Down
12 changes: 6 additions & 6 deletions src/MarlinSimulator/marlin_arduino_impl/arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,28 @@ extern "C" void delay(const int msec) {
// IO functions
// As defined by Arduino INPUT(0x0), OUTPUT(0x1), INPUT_PULLUP(0x2)
void pinMode(const pin_t pin, const uint8_t mode) {
if (!VALID_PIN(pin)) return;
if (!isValidPin(pin)) return;
Gpio::setMode(pin, mode);
}

void digitalWrite(pin_t pin, uint8_t pin_status) {
if (!VALID_PIN(pin)) return;
if (!isValidPin(pin)) return;
Gpio::set(pin, pin_status);
}

bool digitalRead(pin_t pin) {
if (!VALID_PIN(pin)) return false;
if (!isValidPin(pin)) return false;
return Gpio::get(pin);
}

void analogWrite(pin_t pin, int pwm_value) { // 1 - 254: pwm_value, 0: LOW, 255: HIGH
if (!VALID_PIN(pin)) return;
if (!isValidPin(pin)) return;
Gpio::set(pin, pwm_value);
}

uint16_t analogRead(pin_t adc_pin) {
if (!VALID_PIN(DIGITAL_PIN_TO_ANALOG_PIN(adc_pin))) return 0;
return Gpio::get(DIGITAL_PIN_TO_ANALOG_PIN(adc_pin));
if (!isValidPin(digitalPinToAnalogIndex(adc_pin))) return 0;
return Gpio::get(digitalPinToAnalogIndex(adc_pin));
}

char *dtostrf(double __val, signed char __width, unsigned char __prec, char *__s) {
Expand Down
4 changes: 2 additions & 2 deletions src/MarlinSimulator/marlin_arduino_impl/pinmapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ int16_t GET_PIN_MAP_INDEX(const pin_t pin) {
}

// Test whether the pin is valid
bool VALID_PIN(const pin_t p) {
bool isValidPin(const pin_t p) {
return WITHIN(p, 0, NUM_DIGITAL_PINS);
}

// Get the analog index for a digital pin
int8_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t p) {
int8_t digitalPinToAnalogIndex(const pin_t p) {
return (WITHIN(p, analog_offset, NUM_DIGITAL_PINS) ? p - analog_offset : P_NC);
}

Expand Down
2 changes: 1 addition & 1 deletion src/MarlinSimulator/marlin_hal_impl/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ bool MarlinHAL::adc_ready() {

uint16_t MarlinHAL::adc_value() {
pin_t pin = analogInputToDigitalPin(active_ch);
if (!VALID_PIN(pin)) return 0;
if (!isValidPin(pin)) return 0;
uint16_t data = ((Gpio::get(pin) >> 2) & 0x3FF);
return data; // return 10bit value as Marlin expects
}
Expand Down