ファイル分割の仕方を確認する。
背景:
スケッチが長くなって来た為、一つのファイルでは見づらくなって来た為、複数ファイルに分けたい。
分割の仕方:
方法1
- Arduino IDE の右上 (タブの右の方)にある▼ を押下し、"新規タブ" を選択。
- 新規ファイルの名前の入力欄が現れる為、ファイル名を入力して OK を押下すると、新しくタブができる。
- ファイル名には拡張子として .h, .cpp, .c をつけることができる。
- 拡張子を付けない場合は、 .ino となる。
- .ino の場合、#include は特に必要無い。.ino 同士は合わせて一緒にコンパイルし、.cpp 等は個別にコンパイルしている感じ。
- スケッチ(.ino ファイル) と同じフォルダに追加するファイルを格納する。
- ファイル(.ino, .h, .cpp, .c) が追加されると Arduino IDE に自動的にタブが追加される。
ベースは「ソフトウェアリセット」のスケッチで 、メインスケッチの強調表示の部分が別ファイル(.ino)に移した部分。
/*
* WiFi LED ON/OFF TEST
* PWM Control
*/
#include <WiFi.h>
const char* ssid = "hogehoge";
const char* password = "hogehogepaswd";
IPAddress ip(192, 168, 1, 32); // for fixed IP Address
IPAddress gateway(192,168, 1, 1); //
IPAddress subnet(255, 255, 255, 0); //
IPAddress DNS(192, 168, 1, 90); //
WiFiServer server(80);
// -- for ledc
byte br_table[11] = {0,25,50,75,100,125,150,175,200,225,255} ;
// -- for sigmaDelta
//byte br_table[6] = {0,150,180,205,230,255} ;
int n = 0;
void setup()
{
Serial.begin(115200);
pinMode(2, OUTPUT); // set the LED pin mode
WiFi.config(ip, gateway, subnet, DNS); // Set fixed IP address
delay(10);
connect_wifi() ;
// for LED PWM Control ---------------------------------------------------
// -- for ledc
ledcSetup(0, 5000, 13); // setup channel 0 with frequency 5000 Hz, 13 bit precission for LEDC timer
ledcAttachPin(2,0); // attach pin 2 to channel 0
ledcWrite(0, 0); // initialize channel 0 to off
// -- for sigmaDelta
//sigmaDeltaSetup(0, 312500); // setup channel 0 with frequency 312500 Hz
//sigmaDeltaAttachPin(2,0); // attach pin 2 to channel 0
//sigmaDeltaWrite(0, 0); // initialize channel 0 to off
n=0 ;
}
void loop(){
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("***** Client access start *****"); // print a message out the serial port
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
String line = client.readStringUntil('\n'); // Get Line data until '\n'
proc_formdata(line) ;
if (line.length() == 1 && line[0] == '\r'){ // end of HTTP request
send_response(client) ; // send response to client
break; // break while loop
}
}
}
delay(1); // give the web browser time to receive the data
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
Serial.println("--------------------------------------------------");
}
}
// ----------------------------------------------------------------------------
// - connect to WiFi ----
// ----------------------------------------------------------------------------
void connect_wifi() {
int lpcnt=0 ;
// We start by connecting to a WiFi network -----------------------------
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
delay(10);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
lpcnt +=1 ;
if (lpcnt>4) {
WiFi.begin(ssid, password);
lpcnt = 0 ;
}
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
// ----------------------------------------------------------------------------
// - フォームデータの処理 ----
// ----------------------------------------------------------------------------
void proc_formdata(String &line) {
Serial.println(line);
if (line.indexOf("GET /?on") != -1) { // Client request was "GET /?on"
n += 1 ; // incliment table no.
if (n>10) n=0 ;
Serial.print("brightness value : ");
Serial.println(br_table[n],DEC);
// -- for ledc
ledcWrite(0, 8191*br_table[n]/255) ; // set PWM value to channel#0
// -- for sigmaDelta
//sigmaDeltaWrite(0, br_table[n]) ;
}
if (line.indexOf("GET /?off") != -1) { // Client request was "GET /?off"
n = 0 ; // clear bright no.
Serial.print("brightness value : ");
Serial.println(br_table[n],DEC);
// -- for ledc
ledcWrite(0, 8191*br_table[n]/255) ; // set PWM value to channel#0
// -- for sigmaDelta
//sigmaDeltaWrite(0, br_table[n]) ;
}
}
// ----------------------------------------------------------------------------
// - send HTML ----
// ----------------------------------------------------------------------------
void send_response(WiFiClient client) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("<!DOCTYPE html><html lang='ja'><head><meta charset='UTF-8'>" ) ;
client.print("<style>input {margin:8px;width:100px;}div {font-size:16pt;text-align:center;width:250px;border:solid 4px #93ff93;}</style>" ) ;
client.print("<title>Color LED Controller</title></head>" ) ;
client.print("<body><div><p>LED ON/OFF</p><form method='get'>" ) ;
client.print("<input type='submit' name='on' value='ON' /><input type='submit' name='off' value='OFF' /></form>" ) ;
client.print("</div></body></html>" ) ;
client.println();
Serial.println( " --- send responce --- ");
}
0 件のコメント:
コメントを投稿