### Project

Nitendon Switch Tetris Bot

### Switch Bot开发日记系列

Nintendo Switch Reverse Engineering 这个Repo是在探索的过程中非常有用的一个，作者把Joy-Cons反编译到的东西都放进里面，介绍了通过物理连接和蓝牙连接连接到Switch的原理图。

On June 20, 2017, Nintendo released System Update v3.0.0 for the Nintendo Switch. Along with a number of additional features that were advertised or noted in the changelog, additional hidden features were added. One of those features allows for the use of compatible USB controllers on the Nintendo Switch, such as the Pokken Tournament Pro Pad.

Unlike the Wii U, which handles these controllers on a ‘per-game’ basis, the Switch treats the Pokken controller as if it was a Switch Pro Controller. Along with having the icon for the Pro Controller, it functions just like it in terms of using it in other games, apart from the lack of physical controls such as analog sticks, the buttons for the stick clicks, or other system buttons such as Home or Capture.

The original version of the code that this repo is based off of emulated the Pokken Tournament Pro Pad, but changes have been made to support the HORIPAD wired controller for Nintendo Switch instead. In addition, many additional features/improvements have been added.

### 重新实现Controller过程

    DDRB = 0b10001111;
DDRC = 0b10111111;
DDRD = 0b01101111;
DDRE = 0b10111111;


        // button 1
if((PIND & (1<<PIND4))){
PORTF |= (1<<PORTF7);
}
// button 2
else if (PINC & (1<<PINC6)){
PORTF &= ~(1<<PORTF7);
}


### 图像识别

（虽然最后全部推倒重来了，因为有时候最简单的方法才是最有效的方法）

（图一）黑点是点的坐标，不同颜色的线是不同的hierarchy，红点是分析到的Block，红框里的block是分析错的，绿框是没分析到的block。现在的算法是在过长的两点之间添加关键点，分析每个block四个顶点的坐标附近有没有黑点，如果四个点都有则就是结果，如果没分析到会进一步分析block内点的数量，大于一个阈值则也是结果，比如（图二）这几个。bug就出在这里，它也满足四个顶点的条件（图三蓝框），但并不应该是符合条件的block。而这几个缺少几个顶点（图四篮框）。图五图六是另外两个样本，红框是分析错的，篮框是没分析到的。这个顶点是cv的canny之后找到的contours，所以有层级关系。

            boolean iFlag = true;
for (int i = 0; i < 4; i++) {
if (!(getBlue(image, nextBlockIX + i * nextBlockLength, nextBlockIY) > nextColorThreshold)) {
iFlag = false;
}
}
if (iFlag) {
result[0] = new BlockI();
break;
}