From 9dd8a49f82e628658b01b7ffca7e67d141077b4c Mon Sep 17 00:00:00 2001
From: Scott Lahteine <github@thinkyhead.com>
Date: Thu, 8 Feb 2018 20:42:12 -0600
Subject: [PATCH] Add pin_t to Marln 1.1 branch

---
 Marlin/Marlin.h        |  4 ---
 Marlin/Marlin_main.cpp | 69 +++++++++---------------------------------
 Marlin/Sd2Card.cpp     |  2 +-
 Marlin/Sd2Card.h       |  2 +-
 Marlin/fastio.h        |  4 +++
 Marlin/pinsDebug.h     |  2 +-
 Marlin/temperature.cpp | 43 ++++++++++++++++++++++++--
 Marlin/temperature.h   |  4 +++
 8 files changed, 67 insertions(+), 63 deletions(-)

diff --git a/Marlin/Marlin.h b/Marlin/Marlin.h
index 28db5395d2..3cd60ac5e1 100644
--- a/Marlin/Marlin.h
+++ b/Marlin/Marlin.h
@@ -226,10 +226,6 @@ void clear_command_queue();
 extern millis_t previous_cmd_ms;
 inline void refresh_cmd_timeout() { previous_cmd_ms = millis(); }
 
-#if ENABLED(FAST_PWM_FAN)
-  void setPwmFrequency(uint8_t pin, int val);
-#endif
-
 /**
  * Feedrate scaling and conversion
  */
diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp
index 18a4d6894b..840f504305 100644
--- a/Marlin/Marlin_main.cpp
+++ b/Marlin/Marlin_main.cpp
@@ -6996,10 +6996,10 @@ inline void gcode_M31() {
 /**
  * Sensitive pin test for M42, M226
  */
-static bool pin_is_protected(const int8_t pin) {
-  static const int8_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
+static bool pin_is_protected(const pin_t pin) {
+  static const pin_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
   for (uint8_t i = 0; i < COUNT(sensitive_pins); i++)
-    if (pin == (int8_t)pgm_read_byte(&sensitive_pins[i])) return true;
+    if (pin == (pin_t)pgm_read_byte(&sensitive_pins[i])) return true;
   return false;
 }
 
@@ -7013,7 +7013,7 @@ inline void gcode_M42() {
   if (!parser.seenval('S')) return;
   const byte pin_status = parser.value_byte();
 
-  const int pin_number = parser.intval('P', LED_PIN);
+  const pin_t pin_number = parser.byteval('P', LED_PIN);
   if (pin_number < 0) return;
 
   if (pin_is_protected(pin_number)) {
@@ -7270,8 +7270,8 @@ inline void gcode_M42() {
     }
 
     // Get the range of pins to test or watch
-    const uint8_t first_pin = parser.byteval('P'),
-                  last_pin = parser.seenval('P') ? first_pin : NUM_DIGITAL_PINS - 1;
+    const pin_t first_pin = parser.byteval('P'),
+                last_pin = parser.seenval('P') ? first_pin : NUM_DIGITAL_PINS - 1;
 
     if (first_pin > last_pin) return;
 
@@ -7281,7 +7281,7 @@ inline void gcode_M42() {
     if (parser.boolval('W')) {
       SERIAL_PROTOCOLLNPGM("Watching pins");
       byte pin_state[last_pin - first_pin + 1];
-      for (int8_t pin = first_pin; pin <= last_pin; pin++) {
+      for (pin_t pin = first_pin; pin <= last_pin; pin++) {
         if (pin_is_protected(pin) && !ignore_protection) continue;
         pinMode(pin, INPUT_PULLUP);
         delay(1);
@@ -7299,7 +7299,7 @@ inline void gcode_M42() {
       #endif
 
       for (;;) {
-        for (int8_t pin = first_pin; pin <= last_pin; pin++) {
+        for (pin_t pin = first_pin; pin <= last_pin; pin++) {
           if (pin_is_protected(pin) && !ignore_protection) continue;
           const byte val =
             /*
@@ -7327,7 +7327,7 @@ inline void gcode_M42() {
     }
 
     // Report current state of selected pin(s)
-    for (uint8_t pin = first_pin; pin <= last_pin; pin++)
+    for (pin_t pin = first_pin; pin <= last_pin; pin++)
       report_pin_state_extended(pin, ignore_protection, true);
   }
 
@@ -9144,16 +9144,16 @@ inline void gcode_M221() {
  */
 inline void gcode_M226() {
   if (parser.seen('P')) {
-    const int pin_number = parser.value_int(),
+    const int pin = parser.value_int(),
               pin_state = parser.intval('S', -1); // required pin state - default is inverted
 
-    if (WITHIN(pin_state, -1, 1) && pin_number > -1 && !pin_is_protected(pin_number)) {
+    if (WITHIN(pin_state, -1, 1) && pin > -1 && !pin_is_protected(pin)) {
 
       int target = LOW;
 
       stepper.synchronize();
 
-      pinMode(pin_number, INPUT);
+      pinMode(pin, INPUT);
       switch (pin_state) {
         case 1:
           target = HIGH;
@@ -9162,13 +9162,13 @@ inline void gcode_M226() {
           target = LOW;
           break;
         case -1:
-          target = !digitalRead(pin_number);
+          target = !digitalRead(pin);
           break;
       }
 
-      while (digitalRead(pin_number) != target) idle();
+      while (digitalRead(pin) != target) idle();
 
-    } // pin_state -1 0 1 && pin_number > -1
+    } // pin_state -1 0 1 && pin > -1
   } // parser.seen('P')
 }
 
@@ -13259,45 +13259,6 @@ void prepare_move_to_destination() {
 
 #endif // FILAMENT_RUNOUT_SENSOR
 
-#if ENABLED(FAST_PWM_FAN)
-
-  void setPwmFrequency(uint8_t pin, int val) {
-    val &= 0x07;
-    switch (digitalPinToTimer(pin)) {
-      #ifdef TCCR0A
-        #if !AVR_AT90USB1286_FAMILY
-          case TIMER0A:
-        #endif
-        case TIMER0B:                           //_SET_CS(0, val);
-                                                  break;
-      #endif
-      #ifdef TCCR1A
-        case TIMER1A: case TIMER1B:             //_SET_CS(1, val);
-                                                  break;
-      #endif
-      #if defined(TCCR2) || defined(TCCR2A)
-        #ifdef TCCR2
-          case TIMER2:
-        #endif
-        #ifdef TCCR2A
-          case TIMER2A: case TIMER2B:
-        #endif
-                                                  _SET_CS(2, val); break;
-      #endif
-      #ifdef TCCR3A
-        case TIMER3A: case TIMER3B: case TIMER3C: _SET_CS(3, val); break;
-      #endif
-      #ifdef TCCR4A
-        case TIMER4A: case TIMER4B: case TIMER4C: _SET_CS(4, val); break;
-      #endif
-      #ifdef TCCR5A
-        case TIMER5A: case TIMER5B: case TIMER5C: _SET_CS(5, val); break;
-      #endif
-    }
-  }
-
-#endif // FAST_PWM_FAN
-
 void enable_all_steppers() {
   #if ENABLED(AUTO_POWER_CONTROL)
     powerManager.power_on();
diff --git a/Marlin/Sd2Card.cpp b/Marlin/Sd2Card.cpp
index 86c5cd9b7c..ab3fc2f370 100644
--- a/Marlin/Sd2Card.cpp
+++ b/Marlin/Sd2Card.cpp
@@ -297,7 +297,7 @@ bool Sd2Card::eraseSingleBlockEnable() {
  * \return true for success, false for failure.
  * The reason for failure can be determined by calling errorCode() and errorData().
  */
-bool Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
+bool Sd2Card::init(uint8_t sckRateID, pin_t chipSelectPin) {
   errorCode_ = type_ = 0;
   chipSelectPin_ = chipSelectPin;
   // 16-bit init start time allows over a minute
diff --git a/Marlin/Sd2Card.h b/Marlin/Sd2Card.h
index 9849980a25..c831f23cd4 100644
--- a/Marlin/Sd2Card.h
+++ b/Marlin/Sd2Card.h
@@ -140,7 +140,7 @@ class Sd2Card {
    * \return true for success or false for failure.
    */
   bool init(uint8_t sckRateID = SPI_FULL_SPEED,
-            uint8_t chipSelectPin = SD_CHIP_SELECT_PIN);
+            pin_t chipSelectPin = SD_CHIP_SELECT_PIN);
   bool readBlock(uint32_t block, uint8_t* dst);
 
   /**
diff --git a/Marlin/fastio.h b/Marlin/fastio.h
index 4f7c9648d6..bbceaa6a33 100644
--- a/Marlin/fastio.h
+++ b/Marlin/fastio.h
@@ -29,6 +29,10 @@
 #ifndef _FASTIO_ARDUINO_H
 #define _FASTIO_ARDUINO_H
 
+#include <stdint.h>
+
+typedef uint8_t pin_t;
+
 #include <avr/io.h>
 
 #define AVR_AT90USB1286_FAMILY (defined(__AVR_AT90USB1287__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1286P__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB646P__)  || defined(__AVR_AT90USB647__))
diff --git a/Marlin/pinsDebug.h b/Marlin/pinsDebug.h
index 239e9c4b16..9d8a0be354 100644
--- a/Marlin/pinsDebug.h
+++ b/Marlin/pinsDebug.h
@@ -71,7 +71,7 @@ bool endstop_monitor_flag = false;
 
 typedef struct {
   const char * const name;
-  uint8_t pin;
+  pin_t pin;
   bool is_digital;
 } PinInfo;
 
diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp
index 227bc8d000..9dcab02b0e 100644
--- a/Marlin/temperature.cpp
+++ b/Marlin/temperature.cpp
@@ -508,7 +508,7 @@ int Temperature::getHeaterPower(int heater) {
 #if HAS_AUTO_FAN
 
   void Temperature::checkExtruderAutoFans() {
-    static const int8_t fanPin[] PROGMEM = { E0_AUTO_FAN_PIN, E1_AUTO_FAN_PIN, E2_AUTO_FAN_PIN, E3_AUTO_FAN_PIN, E4_AUTO_FAN_PIN };
+    static const pin_t fanPin[] PROGMEM = { E0_AUTO_FAN_PIN, E1_AUTO_FAN_PIN, E2_AUTO_FAN_PIN, E3_AUTO_FAN_PIN, E4_AUTO_FAN_PIN };
     static const uint8_t fanBit[] PROGMEM = {
                     0,
       AUTO_1_IS_0 ? 0 :               1,
@@ -524,7 +524,7 @@ int Temperature::getHeaterPower(int heater) {
 
     uint8_t fanDone = 0;
     for (uint8_t f = 0; f < COUNT(fanPin); f++) {
-      int8_t pin = pgm_read_byte(&fanPin[f]);
+      pin_t pin = pgm_read_byte(&fanPin[f]);
       const uint8_t bit = pgm_read_byte(&fanBit[f]);
       if (pin >= 0 && !TEST(fanDone, bit)) {
         uint8_t newFanSpeed = TEST(fanState, bit) ? EXTRUDER_AUTO_FAN_SPEED : 0;
@@ -1267,6 +1267,45 @@ void Temperature::init() {
   #endif
 }
 
+#if ENABLED(FAST_PWM_FAN)
+
+  void setPwmFrequency(const pin_t pin, int val) {
+    val &= 0x07;
+    switch (digitalPinToTimer(pin)) {
+      #ifdef TCCR0A
+        #if !AVR_AT90USB1286_FAMILY
+          case TIMER0A:
+        #endif
+        case TIMER0B:                           //_SET_CS(0, val);
+                                                  break;
+      #endif
+      #ifdef TCCR1A
+        case TIMER1A: case TIMER1B:             //_SET_CS(1, val);
+                                                  break;
+      #endif
+      #if defined(TCCR2) || defined(TCCR2A)
+        #ifdef TCCR2
+          case TIMER2:
+        #endif
+        #ifdef TCCR2A
+          case TIMER2A: case TIMER2B:
+        #endif
+                                                  _SET_CS(2, val); break;
+      #endif
+      #ifdef TCCR3A
+        case TIMER3A: case TIMER3B: case TIMER3C: _SET_CS(3, val); break;
+      #endif
+      #ifdef TCCR4A
+        case TIMER4A: case TIMER4B: case TIMER4C: _SET_CS(4, val); break;
+      #endif
+      #ifdef TCCR5A
+        case TIMER5A: case TIMER5B: case TIMER5C: _SET_CS(5, val); break;
+      #endif
+    }
+  }
+
+#endif // FAST_PWM_FAN
+
 #if WATCH_HOTENDS
   /**
    * Start Heating Sanity Check for hotends that are below
diff --git a/Marlin/temperature.h b/Marlin/temperature.h
index c8bead83a4..b3d9e127e4 100644
--- a/Marlin/temperature.h
+++ b/Marlin/temperature.h
@@ -587,6 +587,10 @@ class Temperature {
 
   private:
 
+    #if ENABLED(FAST_PWM_FAN)
+      void setPwmFrequency(const pin_t pin, int val);
+    #endif
+
     static void set_current_temp_raw();
 
     static void updateTemperaturesFromRawValues();