confidential

Dependencies:   Chainable_RGB_LED mbed mbedConnectorInterface mbedEndpointNetwork

mbedEndpointResources/Touch.h

Committer:
cjwu15
Date:
2015-05-09
Revision:
1:73f962479f1a
Parent:
0:02b8d440ded3
Child:
2:0382d0c13a51

File content as of revision 1:73f962479f1a:

/**
 * @file    LightResource.h
 * @brief   mbed CoAP Endpoint Light resource supporting CoAP GET and PUT
 * @author  Doug Anson, Michael Koster
 * @version 1.0
 * @see
 *
 * Copyright (c) 2014
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __TOUCH_RESOURCE_H__
#define __TOUCH_RESOURCE_H__

#include <stdio.h>

// Base class
#include "DynamicResource.h"
#include "mbed.h"
#include "MMA7660.h"


DigitalIn button(D2);
MMA7660 accelemeter;

int reps[50];
int idx = 0;

char result[200];

void touch_thread(void const *args)
{
    float ax, ay, az;
    accelemeter.init();
    bool up = false;
    int rep = 0;
    for (i = 0; i < 50; i++)
        reps[i] = -1;
    while(1) {
        if (!button) {
            printf("touch detected\n\r");
            count++;
            LED_set_color("0000FF00");
            while(!button) {
                accelemeter.getAcceleration(&ax,&ay,&az);
                printf("Accleration of X=%2.2fg, Y=%2.2fg, Z=%2.2fg\n\r",ax,ay,az);
                if (ay < 0.7f) {
                    up = true;
                }
                if (ay > 0.9f) {
                    if (up)
                        rep++;
                    up = false;
                }
            }

            LED_set_color("00000000");
            printf("Reps = %d\n\r", rep);
            reps[idx++] = rep;
            rep = 0;
        }
    }
}

    /** LightResource class
     */
    class TouchResource : public DynamicResource
    {

    public:
        /**
        Default constructor
        @param logger input logger instance for this resource
        @param name input the Light resource name
        @param observable input the resource is Observable (default: FALSE)
        */
        TouchResource(const Logger *logger,const char *name,const bool observable = false) : DynamicResource(logger,name,"Touch",SN_GRS_GET_ALLOWED,observable) {
        }

        /**
        Get the value of the LED
        @returns string containing the last setting
        */
        virtual string get() {
            int i = 0;
            memset(result, 0, 200);
            
            for (i = 0; i < idx; i++)
            sprintf(result+(i*4), "%2d, ", reps[idx]);

            for (i = 0; i < 50; i++)
                reps[i] = -1;
            idx = 0;
            
            return(result);
        }
    };

#endif // __TOUCH_RESOURCE_H__