node.jsとmbedを使ってインターネット越しに多人数でマウスをクリックする

Fork of AutoCookieClicker by Takuya Urakawa

Files at this revision

API Documentation at this revision

Comitter:
hsgw
Date:
Fri Sep 20 10:31:16 2013 +0000
Parent:
2:8e5fb73637e5
Commit message:
1st commit

Changed in this revision

PinDetect.lib Show annotated file Show diff for this revision Revisions of this file
Servo.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PinDetect.lib	Fri Sep 20 10:31:16 2013 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.lib	Fri Sep 20 10:31:16 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/Servo/#36b69a7ced07
--- a/main.cpp	Wed Sep 18 12:00:07 2013 +0000
+++ b/main.cpp	Fri Sep 20 10:31:16 2013 +0000
@@ -1,21 +1,100 @@
-//sw connect p30 and gnd
+// sw - p30
+// servo control - p25
 
 #include "mbed.h"
+#include "rtos.h"
 #include "USBMouse.h"
- 
+#include "PinDetect.h"
+#include "Servo.h"
+
+Serial pc(USBTX, USBRX);
 USBMouse mouse;
 
-DigitalIn sw(p30);
-DigitalOut led(LED1);
- 
+PinDetect sw(p30);
+
+DigitalOut ledEnable(LED1);
+DigitalOut ledClick(LED2);
+DigitalOut ledClickContinue(LED3);
+DigitalOut ledMbedAlive(LED4);
+
+volatile uint32_t clickCount;
+volatile bool clickEnable;
+
+Mutex printMutex;
+
+//servo Thread
+void servoMove(void const *arg){
+    Servo servo(p25);
+    servo = 0.5;
+    while(true){
+        if(clickEnable && clickCount > 0){
+            ledClick = 1;
+            servo = 0;
+            wait(0.2);
+            servo = 0.3;
+            wait(0.1);
+        }else{
+            servo = 0.5;
+            ledClick = 0;
+        }
+     }
+}
+
+//serial Thread
+void serialRecieve(void const *arg){
+    while(true){
+        if(pc.readable() > 0){
+            printMutex.lock();
+            clickCount += pc.getc();
+            pc.printf("%d\n" , clickCount);
+            printMutex.unlock();
+        }
+    }
+}
+
+//sw isr
+void swPressedIsr(){
+    ledEnable = !ledEnable;
+    //restart click and clear clickCount
+    if(!clickEnable) clickCount = 0;
+    clickEnable = !clickEnable;
+}
+
+//mbed is alive
+void ledBlinkIsr(){
+    ledMbedAlive = !ledMbedAlive;
+}
+
 int main() {
     sw.mode(PullUp);
- 
+    
+    clickCount = 0;
+    clickEnable = true;
+    ledEnable = 1;
+    
+    //sw debounce
+    sw.attach_deasserted(&swPressedIsr);
+    sw.setSampleFrequency();
+    //led
+    Ticker ledBlinker;
+    ledBlinker.attach(&ledBlinkIsr,0.5);
+    
+    Thread servoThread(servoMove);
+    Thread serialThread(serialRecieve);
+    printMutex.lock();
+    pc.printf("\nStart!\n");
+    printMutex.unlock();
     while (1) {
-        if(sw.read()!=1) {
-            led = !led;
+        if(clickCount > 0 && clickEnable){
+            ledClickContinue = 1;
+            printMutex.lock();
+            pc.printf("click!\n");
+            printMutex.unlock();
             mouse.click(MOUSE_LEFT);
+            clickCount--;
+            Thread::wait(0.02);
+        }else{
+            ledClickContinue = 0;
         }
-        wait(0.25);
     }
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Fri Sep 20 10:31:16 2013 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed-rtos/#ee87e782d34f