 // potencjometr: niebieski z czarną kreską do GND, biały do +5V, niebieski do pinu A0 / A1
 // LCD: SDA->2, SCL->3
 
+// Stage/Abort status (on=stage, off=abort)
+uint8_t stageabort = 1;
+// joy1 switch status - change meaning of joy2 axes (X:on=rotate, off=axis; Y:on=hatswitch#0, off=rudder)
+uint8_t joy1switch = 0;
+// joy2 switch status - change meaning of joy1 axes (X:on=rotate, off=axis; Y:on=rotate, off=axis)
+uint8_t joy2switch = 0;
+
+// almost copy from ksp-gegi-mini!
+
+class digitalPin {
+  public:
+    digitalPin(const uint8_t id, const uint8_t pinSwitch);
+    void updateSwitch();
+    uint8_t getId() { return(m_id); }
+    uint8_t getSwitchState() const { return(m_lastPinState); }
+  private:
+    void updateJoystick() const;
+    const uint8_t m_id, m_pinSwitch;
+    uint8_t m_lastPinState { HIGH } ;
+};
+
+digitalPin digiPins[] = {
+  digitalPin(10,5),    // joy1switch
+  digitalPin(11,7)	   // joy2switch
+};
+
+digitalPin::digitalPin(const uint8_t id, const uint8_t pinSwitch) :
+  m_id(id), m_pinSwitch(pinSwitch)
+{
+  if (m_pinSwitch>0) {
+    pinMode(m_pinSwitch,INPUT_PULLUP);
+  }
+}
+
+void digitalPin::updateSwitch() {
+  if (m_pinSwitch>0) {
+    // reverse logic because switches are connected to GND with internal pullup, so pushed state (ON) is 0V
+    uint8_t state = !digitalRead(m_pinSwitch);
+    if (state!=m_lastPinState) {
+      Serial.write('D');
+      Serial.print(m_id);
+      Serial.write('=');
+      Serial.println(state);
+      m_lastPinState = state;
+      updateJoystick();
+    }
+  }
+}
+
+// map to joystick events
+void digitalPin::updateJoystick() const {
+  uint8_t buttonId = 0x80;
+  switch (m_id) {
+    case 10:
+      buttonId = 6;
+      break;
+    case 11:
+      buttonId = 7;
+      break;
+  }
+  if (buttonId < 0x80) {
+    Joystick.setButton(buttonId,m_lastPinState);
+  }
+}
+
+/////////////
+
+class analogInPin {
+  public:
+    analogInPin(const uint8_t id, const uint8_t pin, const int threshold);
+    void update();
+  private:
+    void updateJoystick() const;
+    const uint8_t m_id, m_pin;
+    const int m_threshold;
+    int m_lastAValue { 0 };
+};
+
+analogInPin analogInPins[] = {
+  analogInPin(0,A4,2),  // throttle
+  analogInPin(1,A8,5),	// timewarp
+  analogInPin(2,A0,2),	// joy1x
+  analogInPin(3,A1,2),	// joy1y
+  analogInPin(4,A2,2),	// joy2x
+  analogInPin(5,A3,2),	// joy2y
+};
+
+analogInPin::analogInPin(const uint8_t id, const uint8_t pin, const int threshold) :
+	m_id(id), m_pin(pin), m_threshold(threshold)
+{
+}
+
+void analogInPin::update() {
+	// map to the range of the analog out
+	int aValue = map(analogRead(m_pin), 0, 1023, 0, 255);
+	// is it different enough from the last reading?
+	if (abs(aValue-m_lastAValue)>m_threshold) {
+		if (aValue<m_threshold) {
+			aValue = 0;
+		}
+		if (aValue>255-m_threshold) {
+			aValue = 255;
+		}
+		// dump status
+		Serial.print("P");
+		Serial.print(m_id);
+		Serial.print("=");
+		Serial.println(aValue,HEX);
+		m_lastAValue = aValue;
+		// map to joystick events
+		updateJoystick();
+	}
+}
+
+void analogInPin::updateJoystick() const {
+	switch (m_id) {
+		case 0: // throttle pot
+			Joystick.setThrottle(m_lastAValue);
+			break;
+		case 2:	// joy1x X on: rotation, off: axis
+			if (joy2switch) {
+				Joystick.setXAxisRotation(m_lastAValue-127);
+			} else {
+				Joystick.setXAxis(m_lastAValue-127);
+			}
+			break;
+		case 3: // joy1y Y on: rotation, off: axis
+			if (joy2switch) {
+				Joystick.setYAxisRotation(m_lastAValue-127);
+			} else {
+				Joystick.setYAxis(m_lastAValue-127);
+			}
+			break;
+		case 4: // joy2x Z on: rotation, off: axis
+			if (joy1switch) {
+				Joystick.setZAxisRotation(m_lastAValue-127);
+			} else {
+				Joystick.setZAxis(m_lastAValue-127);
+			}
+			break;
+		case 5: // joy2y on: ignore, off: rudder
+			if (joy1switch) {
+			} else {
+				Joystick.setRudder(m_lastAValue);
+			}
+			break;
+	}
+}
+
 class analogOutPin {
   public:
     analogOutPin(const uint8_t id, const uint8_t pin);
     void updateState(const uint8_t val) const ;
-    const uint8_t getId() const { return(m_id); }
+    const uint8_t getId() { return(m_id); }
   private:
     const uint8_t m_id, m_pin;
 };

	}
}

void loop() {
  checkSerialInputUSBtoUART();
  checkSerialInputUARTtoUSB();
  Joystick.sendState();
  delay(200);
}


  analogOutPin(0,9),	// g-force
  analogOutPin(1,10)    // electrical power
};

// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
