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

Fork of AutoCookieClicker by Takuya Urakawa

Committer:
hsgw
Date:
Fri Sep 20 10:31:16 2013 +0000
Revision:
3:35d8318e31bc
Parent:
2:8e5fb73637e5
1st commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hsgw 3:35d8318e31bc 1 // sw - p30
hsgw 3:35d8318e31bc 2 // servo control - p25
hsgw 2:8e5fb73637e5 3
mbed_official 0:aaf5a9d465fd 4 #include "mbed.h"
hsgw 3:35d8318e31bc 5 #include "rtos.h"
hsgw 2:8e5fb73637e5 6 #include "USBMouse.h"
hsgw 3:35d8318e31bc 7 #include "PinDetect.h"
hsgw 3:35d8318e31bc 8 #include "Servo.h"
hsgw 3:35d8318e31bc 9
hsgw 3:35d8318e31bc 10 Serial pc(USBTX, USBRX);
hsgw 2:8e5fb73637e5 11 USBMouse mouse;
hsgw 2:8e5fb73637e5 12
hsgw 3:35d8318e31bc 13 PinDetect sw(p30);
hsgw 3:35d8318e31bc 14
hsgw 3:35d8318e31bc 15 DigitalOut ledEnable(LED1);
hsgw 3:35d8318e31bc 16 DigitalOut ledClick(LED2);
hsgw 3:35d8318e31bc 17 DigitalOut ledClickContinue(LED3);
hsgw 3:35d8318e31bc 18 DigitalOut ledMbedAlive(LED4);
hsgw 3:35d8318e31bc 19
hsgw 3:35d8318e31bc 20 volatile uint32_t clickCount;
hsgw 3:35d8318e31bc 21 volatile bool clickEnable;
hsgw 3:35d8318e31bc 22
hsgw 3:35d8318e31bc 23 Mutex printMutex;
hsgw 3:35d8318e31bc 24
hsgw 3:35d8318e31bc 25 //servo Thread
hsgw 3:35d8318e31bc 26 void servoMove(void const *arg){
hsgw 3:35d8318e31bc 27 Servo servo(p25);
hsgw 3:35d8318e31bc 28 servo = 0.5;
hsgw 3:35d8318e31bc 29 while(true){
hsgw 3:35d8318e31bc 30 if(clickEnable && clickCount > 0){
hsgw 3:35d8318e31bc 31 ledClick = 1;
hsgw 3:35d8318e31bc 32 servo = 0;
hsgw 3:35d8318e31bc 33 wait(0.2);
hsgw 3:35d8318e31bc 34 servo = 0.3;
hsgw 3:35d8318e31bc 35 wait(0.1);
hsgw 3:35d8318e31bc 36 }else{
hsgw 3:35d8318e31bc 37 servo = 0.5;
hsgw 3:35d8318e31bc 38 ledClick = 0;
hsgw 3:35d8318e31bc 39 }
hsgw 3:35d8318e31bc 40 }
hsgw 3:35d8318e31bc 41 }
hsgw 3:35d8318e31bc 42
hsgw 3:35d8318e31bc 43 //serial Thread
hsgw 3:35d8318e31bc 44 void serialRecieve(void const *arg){
hsgw 3:35d8318e31bc 45 while(true){
hsgw 3:35d8318e31bc 46 if(pc.readable() > 0){
hsgw 3:35d8318e31bc 47 printMutex.lock();
hsgw 3:35d8318e31bc 48 clickCount += pc.getc();
hsgw 3:35d8318e31bc 49 pc.printf("%d\n" , clickCount);
hsgw 3:35d8318e31bc 50 printMutex.unlock();
hsgw 3:35d8318e31bc 51 }
hsgw 3:35d8318e31bc 52 }
hsgw 3:35d8318e31bc 53 }
hsgw 3:35d8318e31bc 54
hsgw 3:35d8318e31bc 55 //sw isr
hsgw 3:35d8318e31bc 56 void swPressedIsr(){
hsgw 3:35d8318e31bc 57 ledEnable = !ledEnable;
hsgw 3:35d8318e31bc 58 //restart click and clear clickCount
hsgw 3:35d8318e31bc 59 if(!clickEnable) clickCount = 0;
hsgw 3:35d8318e31bc 60 clickEnable = !clickEnable;
hsgw 3:35d8318e31bc 61 }
hsgw 3:35d8318e31bc 62
hsgw 3:35d8318e31bc 63 //mbed is alive
hsgw 3:35d8318e31bc 64 void ledBlinkIsr(){
hsgw 3:35d8318e31bc 65 ledMbedAlive = !ledMbedAlive;
hsgw 3:35d8318e31bc 66 }
hsgw 3:35d8318e31bc 67
mbed_official 0:aaf5a9d465fd 68 int main() {
hsgw 2:8e5fb73637e5 69 sw.mode(PullUp);
hsgw 3:35d8318e31bc 70
hsgw 3:35d8318e31bc 71 clickCount = 0;
hsgw 3:35d8318e31bc 72 clickEnable = true;
hsgw 3:35d8318e31bc 73 ledEnable = 1;
hsgw 3:35d8318e31bc 74
hsgw 3:35d8318e31bc 75 //sw debounce
hsgw 3:35d8318e31bc 76 sw.attach_deasserted(&swPressedIsr);
hsgw 3:35d8318e31bc 77 sw.setSampleFrequency();
hsgw 3:35d8318e31bc 78 //led
hsgw 3:35d8318e31bc 79 Ticker ledBlinker;
hsgw 3:35d8318e31bc 80 ledBlinker.attach(&ledBlinkIsr,0.5);
hsgw 3:35d8318e31bc 81
hsgw 3:35d8318e31bc 82 Thread servoThread(servoMove);
hsgw 3:35d8318e31bc 83 Thread serialThread(serialRecieve);
hsgw 3:35d8318e31bc 84 printMutex.lock();
hsgw 3:35d8318e31bc 85 pc.printf("\nStart!\n");
hsgw 3:35d8318e31bc 86 printMutex.unlock();
hsgw 2:8e5fb73637e5 87 while (1) {
hsgw 3:35d8318e31bc 88 if(clickCount > 0 && clickEnable){
hsgw 3:35d8318e31bc 89 ledClickContinue = 1;
hsgw 3:35d8318e31bc 90 printMutex.lock();
hsgw 3:35d8318e31bc 91 pc.printf("click!\n");
hsgw 3:35d8318e31bc 92 printMutex.unlock();
hsgw 2:8e5fb73637e5 93 mouse.click(MOUSE_LEFT);
hsgw 3:35d8318e31bc 94 clickCount--;
hsgw 3:35d8318e31bc 95 Thread::wait(0.02);
hsgw 3:35d8318e31bc 96 }else{
hsgw 3:35d8318e31bc 97 ledClickContinue = 0;
mbed_official 0:aaf5a9d465fd 98 }
mbed_official 0:aaf5a9d465fd 99 }
mbed_official 0:aaf5a9d465fd 100 }