Created
January 6, 2019 15:12
-
-
Save CanNuhlar/62a3789eb94515e6b964463aacdeb193 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fstream> | |
#include <iostream> | |
#include <unistd.h> | |
#include <math.h> | |
#include <sstream> | |
using namespace std; | |
struct XYZAxisStruct{ | |
int xAxis; | |
int yAxis; | |
int zAxis; | |
}; | |
enum screenOrientation{normal, inverted, leftUp, rightUp, initialState}; | |
void handleOrientation(XYZAxisStruct, bool, screenOrientation*); | |
void readAxis(bool, screenOrientation*); | |
int runCommand(string); | |
int runCommand(string command){ | |
FILE* pipe = popen(command.c_str(), "r"); | |
if(pipe) | |
return 0; | |
else{ | |
cerr << "Couldn't run command : \"" << command << "\"" << endl; | |
return -1; | |
} | |
} | |
void readAxis(bool verbose, screenOrientation* orientation){ | |
string xValue, yValue, zValue; | |
XYZAxisStruct XYZAxis; | |
ifstream xAxisPath ("/sys/bus/iio/devices/iio:device0/in_accel_x_raw"); | |
ifstream yAxisPath ("/sys/bus/iio/devices/iio:device0/in_accel_y_raw"); | |
ifstream zAxisPath ("/sys/bus/iio/devices/iio:device0/in_accel_z_raw"); | |
if(xAxisPath.is_open() && yAxisPath.is_open() && zAxisPath.is_open()){ | |
while(getline(xAxisPath,xValue) && getline(yAxisPath, yValue) && getline(zAxisPath, zValue)){ | |
if(verbose) | |
cout << "Raw X Value: " << xValue << " | Raw Y Value: " << yValue << " | Raw Z Value: " << zValue << endl; | |
stringstream(xValue) >> XYZAxis.xAxis; | |
stringstream(yValue) >> XYZAxis.yAxis; | |
stringstream(zValue) >> XYZAxis.zAxis; | |
XYZAxis.xAxis*=0.000598; | |
XYZAxis.yAxis*=0.000598; | |
XYZAxis.zAxis*=0.000598; | |
handleOrientation(XYZAxis, verbose, orientation); | |
} | |
} | |
else{ | |
cerr << "Couldn't open device paths" << endl; | |
} | |
} | |
void handleOrientation(XYZAxisStruct XYZAxis, bool verbose, screenOrientation* orientation){ | |
string xrandrRotationCmd[4] = {"xrandr --orientation normal", | |
"xrandr --orientation inverted", | |
"xrandr --orientation left", | |
"xrandr --orientation right"}; | |
string xinputRotationCmd[4] = {"xinput set-prop 'ATML1000:00 03EB:8C42' 'Coordinate Transformation Matrix' 1 0 0 0 1 0 0 0 1", | |
"xinput set-prop 'ATML1000:00 03EB:8C42' 'Coordinate Transformation Matrix' -1 0 1 0 -1 1 0 0 1", | |
"xinput set-prop 'ATML1000:00 03EB:8C42' 'Coordinate Transformation Matrix' 0 -1 1 1 0 0 0 0 1", | |
"xinput set-prop 'ATML1000:00 03EB:8C42' 'Coordinate Transformation Matrix' 0 1 0 -1 0 1 0 0 1"}; | |
const char* startOnboardCmd = "onboard"; | |
const char* terminateOnboardCmd = "killall onboard >/dev/null 2>&1"; | |
if(verbose){ | |
cout << "X Axis g (approx.): " << XYZAxis.xAxis << endl; | |
cout << "Y Axis g (approx.): " << XYZAxis.yAxis << endl; | |
cout << "Z Axis g (approx.): " << XYZAxis.zAxis << endl; | |
cout << "----------" << endl; | |
} | |
if(7 < abs(XYZAxis.xAxis) && 11 > abs(XYZAxis.xAxis) && XYZAxis.xAxis > 0 && *orientation != normal){ | |
if(verbose) cout << "Orientation is normal" << endl; | |
*orientation = normal; | |
runCommand(xrandrRotationCmd[0]); | |
runCommand(terminateOnboardCmd); | |
runCommand(xinputRotationCmd[0]); | |
}else if(7 < abs(XYZAxis.xAxis) && 11 > abs(XYZAxis.xAxis) && XYZAxis.zAxis < -2 && *orientation != inverted){ | |
if(verbose) cout << "Orientation is inverted" << endl; | |
*orientation = inverted; | |
runCommand(xrandrRotationCmd[1]); | |
runCommand(startOnboardCmd); | |
runCommand(xinputRotationCmd[1]); | |
}else if(-7 >= XYZAxis.yAxis && -11 <= XYZAxis.yAxis && *orientation != leftUp){ | |
if(verbose) cout << "Orientation is left up" << endl; | |
*orientation = leftUp; | |
runCommand(xrandrRotationCmd[2]); | |
runCommand(startOnboardCmd); | |
runCommand(xinputRotationCmd[2]); | |
}else if(11 > XYZAxis.yAxis && 7 < XYZAxis.yAxis && XYZAxis.xAxis == 0 && *orientation != rightUp){ | |
if(verbose) cout << "Orientation is rightUp" << endl; | |
*orientation = rightUp; | |
runCommand(xrandrRotationCmd[3]); | |
runCommand(startOnboardCmd); | |
runCommand(xinputRotationCmd[3]); | |
} | |
} | |
int main(int argc, char* argv[]){ | |
bool verbose = false; | |
screenOrientation orientation = initialState; | |
if(argc > 1 && (string(argv[1]) == "-v" || string(argv[1]) == "--verbose")) | |
verbose = true; | |
cout << "Listening for orientation change..." << endl; | |
while(true){ | |
readAxis(verbose, &orientation); | |
usleep(100); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment