Skip to content

Instantly share code, notes, and snippets.

@Tomokatsu-Sakamoto
Created December 21, 2017 09:52
Show Gist options
  • Save Tomokatsu-Sakamoto/a382bdb776690f346069d70349adcc97 to your computer and use it in GitHub Desktop.
Save Tomokatsu-Sakamoto/a382bdb776690f346069d70349adcc97 to your computer and use it in GitHub Desktop.
ESP-WROOM-02 を用いたサイコロ
/**
@file AXIS_KXP84.cpp
@brief This is a library for the KXP84-2050 3AXIS I2C Brick.
@author Tomokatsu Sakamoto <tomokatsu.sakamoto@gmail.com>
*/
#include "AXIS_KXP84.h"
/**
@brief Constructor
*/
Kxp84::Kxp84(uint8_t addr)
{
_i2caddr = addr;
Wire.begin( );
}
/**
* @brief Set Config
*/
void Kxp84::configuration()
{
writeI2c( CTRL_REGC, 0 );
writeI2c( CTRL_REGB, 0 );
writeI2c( CTRL_REGB, KXP84_B_FFIen | KXP84_B_MOTIen ); // Recommended Value
writeI2c( FF_DELAY, 0x14 ); // Recommended Value
writeI2c( MOT_DELAY, 0x14 ); // Recommended Value
writeI2c( FF_INT, 0x14 ); // Recommended Value
writeI2c( MOT_INT, 0x4D ); // Recommended Value
}
/**
* @brief Read ADXL345
* @param [out] *x : X-axis Data
* @param [out] *y : Y-axis Data
* @param [out] *z : Z-axis Data
*/
void Kxp84::readXYZ(int *x, int *y, int *z)
{
int i = 0;
uint8_t length = 6;
uint8_t axis_buff[ 6 ];
readI2c( XOUT_H, length, axis_buff );
*x = ((((int)axis_buff[ 0 ]) << 8) | axis_buff[ 1 ] ) >> 4;
*y = ((((int)axis_buff[ 2 ]) << 8) | axis_buff[ 3 ] ) >> 4;
*z = ((((int)axis_buff[ 4 ]) << 8) | axis_buff[ 5 ] ) >> 4;
}
/**
* @brief Write I2C Data
* @param [in] register_addr : Write Register Address
* @param [in] value : Write Data
*/
void Kxp84::writeI2c(uint8_t register_addr, uint8_t value)
{
Wire.beginTransmission(_i2caddr);
Wire.write(register_addr);
Wire.write(value);
Wire.endTransmission();
}
/**
* @brief Read I2C Data
* @param [in] register_addr : register address
* @param [in] num : Data Length
* @param [out] *buf : Read Data
*/
void Kxp84::readI2c(uint8_t register_addr, uint8_t num, uint8_t buffer[])
{
Wire.beginTransmission( _i2caddr );
Wire.write( register_addr + 0x80 );
Wire.endTransmission( );
Wire.requestFrom( _i2caddr, num );
int i = 0;
while( Wire.available( ) )
{
buffer[ i ] = Wire.read( );
i++;
}
}
/**
@file AXIS_KXP84.h
@brief This is a library for the KXP84-2050 3AXIS I2C Brick.
@author Tomokatsu Sakamoto <tomokatsu.sakamoto@gmail.com>
*/
#ifndef AXIS_KXP84_H
#define AXIS_KXP84_H
#include <Arduino.h>
#include <Wire.h>
/** SLAVE_ADDRESS */
#define KXP84_SLAVE_ADDRESS 0x18
/** Who_am_i register */
#define KXP84_DEVID_REG 0x00
/** Device id */
#define KXP84_DEVICE 0xe5
// Addresses
#define XOUT_H 0x00
#define XOUT_L 0x01
#define YOUT_H 0x02
#define YOUT_L 0x03
#define ZOUT_H 0x04
#define ZOUT_L 0x05
#define FF_INT 0x06
#define FF_DELAY 0x07
#define MOT_INT 0x08
#define MOT_DELAY 0x09
#define CTRL_REGC 0x0A
#define CTRL_REGB 0x0B
#define CTRL_REGA 0x0C
// CTRL_REGA
#define KXP84_A_Parity 0x04
#define KXP84_A_MOTI 0x02
#define KXP84_A_FFI 0x01
// CTRL_REGB
#define KXP84_B_CLKhld 0x80
#define KXP84_B_nEnable 0x40
#define KXP84_B_ST 0x20
#define KXP84_B_MOTDen 0x10
#define KXP84_B_FFDen 0x08
#define KXP84_B_MOTIen 0x04
#define KXP84_B_FFIen 0x02
#define KXP84_B_FFMOTI 0x01
/**
* @class FaBo3Axis
* @brief 3Axis Control
*/
class Kxp84
{
public:
Kxp84(uint8_t addr = KXP84_SLAVE_ADDRESS);
void configuration(void);
void readXYZ(int *x, int *y, int *z);
private:
uint8_t _i2caddr;
void writeI2c(uint8_t register_addr, uint8_t value);
void readI2c(uint8_t register_addr, uint8_t num, uint8_t *buf);
};
#endif // AXIS_KXP84_H
// ESP8266 サンプル⑤ WebベースでLCD+加速度センサーを使用
/*
This sketch demonstrates how to set up a simple HTTP-like server.
The server will set a GPIO pin depending on the request
http://server_ip/gpio/0 will set the GPIO2 low,
http://server_ip/gpio/1 will set the GPIO2 high
server_ip is the IP address of the ESP8266 module, will be
printed to Serial when the module is connected.
*/
#include <WiFiClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <Wire.h>
#include <ST7032.h>
#include <AXIS_KXP84.h>
#include "MyWiFi.h" // このファイルの中では ssid と password を定義
#ifndef ssid // 他の場所で SSID が設定されていなかった時は、ここで...
#define ssid "SSID"
#endif
#ifndef password // 他の場所で Password が設定されていなかった時は、ここで...
#define password "password"
#endif
#define LED_01 16
#define LED_02 15
#define LED_03 14
#define LED_04 13
ST7032 lcd;
Kxp84 Kxp;
char lineBuff[ 16 ]; // LCDの1行分バッファ
ESP8266WebServer server(80);
const char *sDice[ ] = {
"--------",
"One ---1",
"Two ---2",
"Three -3",
"Four --4",
"Five --5",
"Six ---6",
};
int iDice = 1;
int x, y, z;
void onroot() {
String msg ;
msg += "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"><title>LED Button</title></head><body>";
msg += "<script>";
msg += "function sendOn(){" ;
msg += "send(\"/on/\");";
msg += "document.getElementById(\"LEDstatus\").innerHTML=\"ON!\";";
msg += "}";
msg += "function sendOff(){";
msg += "send(\"/off/\");";
msg += "document.getElementById(\"LEDstatus\").innerHTML=\"OFF!\";";
msg += "}";
msg += "function send(url){";
msg += "var xhr = new XMLHttpRequest();";
msg += "xhr.open(\"GET\", url, true);";
msg += "xhr.send();";
msg += "}";
msg += "</script>";
msg += "<button id=\"on\" onClick=sendOn()>LED ON</button> ";
msg += "<button id=\"off\" onClick=sendOff()>LED OFF</button>";
msg += "<p id=\"LEDstatus\"></p>";
msg += "<p id=\"AXISstatus\">";
sprintf( lineBuff, "X:%4X, ", x );
msg += lineBuff;
sprintf( lineBuff, "Y:%4X, ", y );
msg += lineBuff;
sprintf( lineBuff, "Z:%4X [", z );
msg += lineBuff;
msg += sDice[ iDice ];
msg += "]</p>";
msg += "</body></html>";
server.send(200, "text/html", msg);
}
void LedOn() {
Serial.println("ON");
digitalWrite(LED_01, HIGH);
server.send(200, "text/html", "OK");
}
void LedOff() {
Serial.println("OFF");
digitalWrite(LED_01, LOW);
server.send(200, "text/html", "OK");
}
void setup() {
pinMode(LED_01, OUTPUT);
pinMode(LED_02, OUTPUT);
pinMode(LED_03, OUTPUT);
pinMode(LED_04, OUTPUT);
Serial.begin(115200);
Serial.println("");
Serial.println("ESP8266 Wifi test");
lcd.begin(8, 2);
lcd.setContrast(30); // コントラスト設定
lcd.print("Wi-Fi ");
lcd.setCursor( 0, 1 );
lcd.print("connect.");
lcd.blink( );
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected as ");
Serial.println(WiFi.localIP());
Serial.print("NETMASK: ");
Serial.println(WiFi.subnetMask());
Serial.print("GATEWAY: ");
Serial.println(WiFi.gatewayIP());
lcd.clear( );
sprintf( lineBuff, "%02X%02X%02X%02X",
WiFi.localIP()[0], WiFi.localIP()[1],
WiFi.localIP()[2], WiFi.localIP()[3] );
lcd.print(lineBuff);
lcd.setCursor( 0, 1 );
sprintf( lineBuff, "IP 3:%3d", WiFi.localIP()[3] );
lcd.print(lineBuff);
delay( 2000 );
// Web server setting
server.on("/", onroot);
server.on("/on/", LedOn);
server.on("/off/", LedOff);
server.begin();
Kxp.configuration( );
}
void loop() {
static int iMode = 0;
static int iCount = 0;
int xx, yy, zz;
iCount++;
iMode++;
iMode %= 3;
switch ( iMode ) {
default:
case 0:
digitalWrite(LED_02, HIGH);
digitalWrite(LED_03, LOW);
digitalWrite(LED_04, LOW);
break;
case 1:
digitalWrite(LED_02, LOW);
digitalWrite(LED_03, HIGH);
digitalWrite(LED_04, LOW);
break;
case 2:
digitalWrite(LED_02, LOW);
digitalWrite(LED_03, LOW);
digitalWrite(LED_04, HIGH);
break;
}
Kxp.readXYZ( &x, &y, &z );
Serial.print("X: ");
Serial.print( x, HEX );
Serial.print(", Y: ");
Serial.print( y, HEX );
Serial.print(", Z: ");
Serial.print( z, HEX );
Serial.print(", (");
Serial.print( iCount, HEX );
Serial.println(")");
x /= 16;
y /= 16;
z /= 16;
#define THRESHOLD1 0x55
#define THRESHOLD2 0xA0
if ( x < THRESHOLD1 ) {
xx = 0;
}
else if ( x > THRESHOLD2 ) {
xx = 2;
}
else {
xx = 1;
}
if ( y < THRESHOLD1 ) {
yy = 0;
}
else if ( y > THRESHOLD2 ) {
yy = 2;
}
else {
yy = 1;
}
if ( z < THRESHOLD1 ) {
zz = 0;
}
else if ( z > THRESHOLD2 ) {
zz = 2;
}
else {
zz = 1;
}
switch ( xx * 100 + yy * 10 + zz ) {
default: iDice = 0; break;
case 112: iDice = 1; break;
case 101: iDice = 2; break;
case 211: iDice = 3; break;
case 11: iDice = 4; break;
case 121: iDice = 5; break;
case 110: iDice = 6; break;
}
lcd.clear( );
lcd.print( sDice[ iDice ] );
lcd.setCursor( 0, 1 );
sprintf( lineBuff, "%02X-%02X-%02X", x, y, z );
lcd.print(lineBuff);
delay( 500 );
server.handleClient();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment