Skip to content

Instantly share code, notes, and snippets.

@Grezzo
Last active December 14, 2015 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Grezzo/148d25de66fe82916d90 to your computer and use it in GitHub Desktop.
Save Grezzo/148d25de66fe82916d90 to your computer and use it in GitHub Desktop.
/*
* Sketch for DigiSpark that will change display orientation by using usb
* keyboard emulation to press Ctrl+Alt+Up-Arrow or Ctrl+Alt+Left-Arrow
* (which are the default hotkeys to rotate the display on windows systems
* with Intel graphics drivers) when it senses (using a tilt switch) that
* the monitor has been physically rotated.
*
* It also illuminates the onboard LED when it thinks the monitor should
* be in portrait mode
*
* The circuit:
* Tilt switch attached from pin 2 to pin 0 (acting as ground)
*/
#include "DigiKeyboard.h"
// For some reason these aren't defined in the library
#define KEY_ARROW_UP 0x52
#define KEY_ARROW_RIGHT 0x4F
const int ledPin = 1; // the number of the led pin
const int switchPin = 2; // the number of the tilt switch pin
const int groundPin = 0; // the number of the pin that will be used for ground (it's closer to the switchPin than the actual ground)
const int protraitSwitchMode = HIGH; //The state that the tilt switch should be in when monitor is in portrait mode
const int portraitDirection = KEY_ARROW_LEFT; //On my dell monitor, I need LEFT, but others may rotate the other way so may need RIGHT
int lastSwitchState = HIGH; // the previous reading from the input pin
int switchState = HIGH; // the current reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time
void setLandscape() {
DigiKeyboard.sendKeyStroke(KEY_ARROW_UP, MOD_CONTROL_LEFT | MOD_ALT_LEFT);
digitalWrite(ledPin, LOW);
}
void setPortrait() {
DigiKeyboard.sendKeyStroke(portraitDirection, MOD_CONTROL_LEFT | MOD_ALT_LEFT);
digitalWrite(ledPin, HIGH);
}
void setup() {
pinMode(switchPin, INPUT);
digitalWrite(switchPin, HIGH); //Set internal pullup resistor
pinMode(groundPin, OUTPUT);
digitalWrite(groundPin, LOW); //Set to low so that it acts like ground
pinMode(ledPin, OUTPUT);
//Flash the LED Pin so that we know that it is now ready and not in progrmm mode
digitalWrite(ledPin, HIGH);
delay(20);
digitalWrite(ledPin, LOW);
}
void loop() {
// Prevent some older systems from missing the first char after delay
DigiKeyboard.sendKeyStroke(0);
int reading = digitalRead(switchPin);
// if input changed, reset the debounce timer
if (reading != lastSwitchState) {
lastDebounceTime = millis();
// otherwise, check if input has not changed for a while (i.e. been steady)
// and that the switch state has changed
} else if (((millis() - lastDebounceTime) > debounceDelay) && (reading != switchState)) {
switchState = reading;
//If the tilt switch is closed
if (switchState == protraitSwitchMode) {
setPortrait();
} else {
setLandscape();
}
}
lastSwitchState = reading;
// Allow keyboard library to interface with PC if necesarry
DigiKeyboard.delay(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment