之前介绍过一个 3D 制作软件:CopperCube,它的特点是上手非常简单。这次制作一个例子,可以使得它生成的 EXE和外部发生交互。我们通过外部的两个开关能够控制软件上的两个球体是否发光。 基本的原理是:使用 FireBeetle实现一个 WebServer,然后编写一个 CopperCube的Script,在代码种访问这个 WebServer 来获得信息。 首先看一下FireBeetle的代码:
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <WebServer.h>
- #include <ESPmDNS.h>
-
- const char* ssid = " YOURSSID";
- const char* password = "PASSWORD";
-
- WebServer server(80);
-
- void handleRoot() {
- char str[3];
- if (digitalRead(16)==LOW) {
- str[0]='2';
- } else {
- str[0]='1';
- }
- if (digitalRead(17)==LOW) {
- str[1]='4';
- } else {
- str[1]='3';
- }
- str[2]=0;
- Serial.println(str);
- server.send(200, "text/plain", str);
- }
-
- void handleNotFound() {
- String message = "File Not Found\n\n";
- message += "URI: ";
- message += server.uri();
- message += "\nMethod: ";
- message += (server.method() == HTTP_GET) ? "GET" : "POST";
- message += "\nArguments: ";
- message += server.args();
- message += "\n";
- for (uint8_t i = 0; i < server.args(); i++) {
- message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
- }
- server.send(404, "text/plain", message);
- }
-
- void setup(void) {
- pinMode(16,INPUT_PULLUP);
- pinMode(17,INPUT_PULLUP);
-
- Serial.begin(115200);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- Serial.println("");
-
- // Wait for connection
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to ");
- Serial.println(ssid);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
-
- if (MDNS.begin("esp32")) {
- Serial.println("MDNS responder started");
- }
-
- server.on("/", handleRoot);
-
- server.on("/inline", []() {
- server.send(200, "text/plain", "this works as well");
- });
-
- server.onNotFound(handleNotFound);
-
- server.begin();
- Serial.println("HTTP server started");
-
-
- }
-
- void loop(void) {
- server.handleClient();
- delay(2);//allow the cpu to switch to other tasks
- }
复制代码
上面的代码会根据当前按钮状态输出不同的数据,通过浏览器可以访问到,这样调试也很方便。
- var HttpRequest=256;
- var Frame=0;
-
- function finishedRequest(dataReceived)
- {
- //print("finished request! Data size:" + dataReceived.length);
- //print("contains:[" + dataReceived+"]");
- if (dataReceived.search("1")!=-1) {
- cube = ccbGetSceneNodeFromName("Light1");
- ccbSetSceneNodeProperty(cube, "Color", new vector3d(1, 0, 0));
- }
- if (dataReceived.search("2")!=-1) {
- cube = ccbGetSceneNodeFromName("Light1");
- ccbSetSceneNodeProperty(cube, "Color", new vector3d(1, 1, 1));
- }
- if (dataReceived.search("3")!=-1) {
- cube = ccbGetSceneNodeFromName("Light2");
- ccbSetSceneNodeProperty(cube, "Color", new vector3d(0, 1, 0));
- }
- if (dataReceived.search("4")!=-1) {
- cube = ccbGetSceneNodeFromName("Light2");
- ccbSetSceneNodeProperty(cube, "Color", new vector3d(1, 1, 1));
- }
- }
-
- function onFrameDrawing()
- {
- if (Frame==100) {
- HttpRequest=ccbDoHTTPRequest("http://192.168.1.24/", finishedRequest);
- Frame=0;
- } else { Frame++; }
- if (HttpRequest==0) {
- //
- }
-
- }
-
- ccbRegisterOnFrameEvent(onFrameDrawing);
复制代码
需要注意的是代码中访问了一个 IP,这个是 Firebeetle 运行时路由器分配给它的IP,具体可以在串口调试信息中看到,每次可能会有所不同。
- ccbDoHTTPRequest("http://192.168.1.24/", finishedRequest);
复制代码
最终,测试的视频在:
对应的代码如下:
|