Andy A / Mbed 2 deprecated buttonTest

Dependencies:   USBDevice mbed

Fork of randomSearch by Andy A

main.cpp

Committer:
AndyA
Date:
2015-12-10
Revision:
0:499d030c3e22
Child:
1:f2747e954f10

File content as of revision 0:499d030c3e22:

#include "mbed.h"
#include "USBMouseKeyboard.h"

// number of search terms
const int searchCount = 6;
// search terms. Should be url encoded (e.g. space becomes %20)
const char *searches[] = {"hot%20cheese",
                          "big%20show",
                          "hot%20cheese",
                          "magic%20mike%20xxl",
                          "hot%20wet%20cheese",
                          "hot%20wet%20-cheese"
                          };
// pause between searches in seconds
const float delayBetweenSearches = 120;

// start of the search string
const char searchPrefix[] = "https://www.google.co.uk/search?q=";

// end of the seach string (an image search with safe search switched off)
const char searchSuffix[] = "&safe=off&tbm=isch";


// turn off all LEDs (some boards they glow a bit if you don't).
// you may need to remove some depending on the board.
BusOut LEDs(LED1, LED2, LED3, LED4, LED5, LED6, LED7);

USBMouseKeyboard keyboard;

void sendString(const char *str)
{
    while (*str != 0) {
        keyboard.putc(*str);
        str++;
    }
}

void drawCircle(float radius, float time, bool dir=true)
{

    const float timeStep = 0.05;
    float currentx = 0;
    float currenty = radius;
    int steps = time/timeStep;
    float angleDelta = 2*3.14159265359 / steps;
    float xPos,yPos;

    if (!dir)
        angleDelta = -angleDelta;


    for (int i = 0; i<steps; i++) {
        xPos = radius*sin(angleDelta*i);
        yPos = radius*cos(angleDelta*i);
        int xDelta = ((xPos - currentx) > 0)?(xPos - currentx) + 0.5 : (xPos - currentx) - 0.5 ;
        int yDelta = ((yPos - currenty) > 0)?(yPos - currenty) + 0.5 : (yPos - currenty) - 0.5 ;
        keyboard.move(xDelta,yDelta);
        currentx += xDelta;
        currenty += yDelta;
        wait(timeStep);
    }
}


void drawLine(float length, float time, bool horizontal=true, bool direction=true)
{

    const float timeStep = 0.05;
    float currentPos = 0;
    int steps = time/timeStep;
    float stepSize = length / steps;
    float movement;

    if (!direction)
        stepSize = -stepSize;

    for (int i = 0; i<steps; i++) {

        movement = stepSize*i - currentPos;
        int moveDelta;
        if (movement>0)
            moveDelta = movement+0.5;
        else
            moveDelta = movement-0.5;

        if (horizontal)
            keyboard.move(moveDelta,0);
        else
            keyboard.move(0,moveDelta);

        currentPos +=  moveDelta;
        wait(timeStep);
    }
}

void doNextSearch()
{
    static int searchIndex = 0;
    keyboard.keyCode('r',KEY_LOGO);  // windows key an R
    wait(0.1); // delay to give the run box a chance to come up.
    sendString(searchPrefix);
    sendString( searches[searchIndex++] );
    sendString(searchSuffix);
    wait(0.05); // delay to give the run box a chance to come up.
    keyboard.putc(0x0a); // hit enter
    if (searchIndex == searchCount)
        searchIndex = 0;
}


main()
{
    LEDs = 0;
    wait(10);
    while (true) {
        doNextSearch();
        
        drawCircle(45,delayBetweenSearches/12);
        drawCircle(45,delayBetweenSearches/12,false);
        wait(delayBetweenSearches/4);
        drawCircle(60,delayBetweenSearches/6,false);
        drawCircle(80,delayBetweenSearches/6);
        wait(delayBetweenSearches/4);
    }
}