使用 Arduino 确定 'landscape' 和 'portrait' 方向
Determine 'landscape' and 'portrait' orientation with Arduino
移动设备如何确定它处于横向模式还是纵向模式?
如果可以使用相同的传感器,是否可以在 Arduino 板上复制相同的功能?
例如,下图的棋盘是风景,如果旋转 90 度,它将是肖像。
这篇文章描述的很详细https://www.safaribooksonline.com/library/view/basic-sensors-in/9781449309480/ch04.html
相关代码片段在 Objective-C 中提供,但可以轻松翻译成您需要的任何内容:
float x = -[acceleration x];
float y = [acceleration y];
float angle = atan2(y, x);
if(angle >= −2.25 && angle <= −0.75) {
//OrientationPortrait
} else if(angle >= −0.75 && angle <= 0.75){
//OrientationLandscapeRight
} else if(angle >= 0.75 && angle <= 2.25) {
//OrientationPortraitUpsideDown
} else if(angle <= −2.25 || angle >= 2.25) {
//OrientationLandscapeLeft];
}
解释:对于任何不等于零的实数 x 和 y,atan2(y, x) 是正 x 轴之间的弧度角一个平面及其上指定坐标给出的点。逆时针角度为正,顺时针角度为负。
移动设备如何确定它处于横向模式还是纵向模式?
如果可以使用相同的传感器,是否可以在 Arduino 板上复制相同的功能?
例如,下图的棋盘是风景,如果旋转 90 度,它将是肖像。
这篇文章描述的很详细https://www.safaribooksonline.com/library/view/basic-sensors-in/9781449309480/ch04.html
相关代码片段在 Objective-C 中提供,但可以轻松翻译成您需要的任何内容:
float x = -[acceleration x];
float y = [acceleration y];
float angle = atan2(y, x);
if(angle >= −2.25 && angle <= −0.75) {
//OrientationPortrait
} else if(angle >= −0.75 && angle <= 0.75){
//OrientationLandscapeRight
} else if(angle >= 0.75 && angle <= 2.25) {
//OrientationPortraitUpsideDown
} else if(angle <= −2.25 || angle >= 2.25) {
//OrientationLandscapeLeft];
}
解释:对于任何不等于零的实数 x 和 y,atan2(y, x) 是正 x 轴之间的弧度角一个平面及其上指定坐标给出的点。逆时针角度为正,顺时针角度为负。