confidential

Dependencies:   Chainable_RGB_LED mbed mbedConnectorInterface mbedEndpointNetwork

Committer:
dscrobonia
Date:
Sat May 09 21:00:59 2015 +0000
Revision:
2:0382d0c13a51
Parent:
1:73f962479f1a
results csv info;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cjwu15 0:02b8d440ded3 1 /**
cjwu15 0:02b8d440ded3 2 * @file LightResource.h
cjwu15 0:02b8d440ded3 3 * @brief mbed CoAP Endpoint Light resource supporting CoAP GET and PUT
cjwu15 0:02b8d440ded3 4 * @author Doug Anson, Michael Koster
cjwu15 0:02b8d440ded3 5 * @version 1.0
cjwu15 0:02b8d440ded3 6 * @see
cjwu15 0:02b8d440ded3 7 *
cjwu15 0:02b8d440ded3 8 * Copyright (c) 2014
cjwu15 0:02b8d440ded3 9 *
cjwu15 0:02b8d440ded3 10 * Licensed under the Apache License, Version 2.0 (the "License");
cjwu15 0:02b8d440ded3 11 * you may not use this file except in compliance with the License.
cjwu15 0:02b8d440ded3 12 * You may obtain a copy of the License at
cjwu15 0:02b8d440ded3 13 *
cjwu15 0:02b8d440ded3 14 * http://www.apache.org/licenses/LICENSE-2.0
cjwu15 0:02b8d440ded3 15 *
cjwu15 0:02b8d440ded3 16 * Unless required by applicable law or agreed to in writing, software
cjwu15 0:02b8d440ded3 17 * distributed under the License is distributed on an "AS IS" BASIS,
cjwu15 0:02b8d440ded3 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
cjwu15 0:02b8d440ded3 19 * See the License for the specific language governing permissions and
cjwu15 0:02b8d440ded3 20 * limitations under the License.
cjwu15 0:02b8d440ded3 21 */
cjwu15 0:02b8d440ded3 22
cjwu15 0:02b8d440ded3 23 #ifndef __TOUCH_RESOURCE_H__
cjwu15 0:02b8d440ded3 24 #define __TOUCH_RESOURCE_H__
cjwu15 0:02b8d440ded3 25
dscrobonia 2:0382d0c13a51 26 #define SIZE_STR_REP 2
dscrobonia 2:0382d0c13a51 27 #define SIZE_STR_TIMESTAMP 10
dscrobonia 2:0382d0c13a51 28 #define SIZE_MAX_ARR 50
dscrobonia 2:0382d0c13a51 29 #define SIZE_MAX_RESULTS 700
dscrobonia 2:0382d0c13a51 30
cjwu15 0:02b8d440ded3 31 #include <stdio.h>
dscrobonia 2:0382d0c13a51 32 #include <time.h>
cjwu15 0:02b8d440ded3 33
cjwu15 0:02b8d440ded3 34 // Base class
cjwu15 0:02b8d440ded3 35 #include "DynamicResource.h"
cjwu15 0:02b8d440ded3 36 #include "mbed.h"
cjwu15 1:73f962479f1a 37 #include "MMA7660.h"
cjwu15 0:02b8d440ded3 38
cjwu15 0:02b8d440ded3 39
cjwu15 1:73f962479f1a 40 DigitalIn button(D2);
cjwu15 1:73f962479f1a 41 MMA7660 accelemeter;
cjwu15 1:73f962479f1a 42
dscrobonia 2:0382d0c13a51 43 int reps[SIZE_MAX_ARR];
dscrobonia 2:0382d0c13a51 44 int times[SIZE_MAX_ARR];
cjwu15 1:73f962479f1a 45 int idx = 0;
cjwu15 1:73f962479f1a 46
dscrobonia 2:0382d0c13a51 47 char result[SIZE_MAX_RESULTS];
cjwu15 1:73f962479f1a 48
cjwu15 1:73f962479f1a 49 void touch_thread(void const *args)
cjwu15 1:73f962479f1a 50 {
cjwu15 1:73f962479f1a 51 float ax, ay, az;
cjwu15 1:73f962479f1a 52 accelemeter.init();
cjwu15 1:73f962479f1a 53 bool up = false;
cjwu15 1:73f962479f1a 54 int rep = 0;
dscrobonia 2:0382d0c13a51 55 int i = 0;
dscrobonia 2:0382d0c13a51 56
cjwu15 1:73f962479f1a 57 for (i = 0; i < 50; i++)
cjwu15 1:73f962479f1a 58 reps[i] = -1;
cjwu15 0:02b8d440ded3 59 while(1) {
cjwu15 1:73f962479f1a 60 if (!button) {
cjwu15 1:73f962479f1a 61 printf("touch detected\n\r");
cjwu15 0:02b8d440ded3 62 LED_set_color("0000FF00");
cjwu15 1:73f962479f1a 63 while(!button) {
cjwu15 1:73f962479f1a 64 accelemeter.getAcceleration(&ax,&ay,&az);
cjwu15 1:73f962479f1a 65 printf("Accleration of X=%2.2fg, Y=%2.2fg, Z=%2.2fg\n\r",ax,ay,az);
cjwu15 1:73f962479f1a 66 if (ay < 0.7f) {
cjwu15 1:73f962479f1a 67 up = true;
cjwu15 1:73f962479f1a 68 }
cjwu15 1:73f962479f1a 69 if (ay > 0.9f) {
cjwu15 1:73f962479f1a 70 if (up)
cjwu15 1:73f962479f1a 71 rep++;
cjwu15 1:73f962479f1a 72 up = false;
cjwu15 1:73f962479f1a 73 }
cjwu15 1:73f962479f1a 74 }
cjwu15 1:73f962479f1a 75
cjwu15 0:02b8d440ded3 76 LED_set_color("00000000");
cjwu15 1:73f962479f1a 77 printf("Reps = %d\n\r", rep);
dscrobonia 2:0382d0c13a51 78 reps[idx] = rep;
dscrobonia 2:0382d0c13a51 79 times[idx++] = (int)time(NULL);
cjwu15 1:73f962479f1a 80 rep = 0;
cjwu15 0:02b8d440ded3 81 }
cjwu15 0:02b8d440ded3 82 }
cjwu15 0:02b8d440ded3 83 }
cjwu15 1:73f962479f1a 84
cjwu15 1:73f962479f1a 85 /** LightResource class
cjwu15 1:73f962479f1a 86 */
cjwu15 1:73f962479f1a 87 class TouchResource : public DynamicResource
cjwu15 1:73f962479f1a 88 {
cjwu15 1:73f962479f1a 89
cjwu15 1:73f962479f1a 90 public:
cjwu15 1:73f962479f1a 91 /**
cjwu15 1:73f962479f1a 92 Default constructor
cjwu15 1:73f962479f1a 93 @param logger input logger instance for this resource
cjwu15 1:73f962479f1a 94 @param name input the Light resource name
cjwu15 1:73f962479f1a 95 @param observable input the resource is Observable (default: FALSE)
cjwu15 1:73f962479f1a 96 */
cjwu15 1:73f962479f1a 97 TouchResource(const Logger *logger,const char *name,const bool observable = false) : DynamicResource(logger,name,"Touch",SN_GRS_GET_ALLOWED,observable) {
cjwu15 1:73f962479f1a 98 }
cjwu15 0:02b8d440ded3 99
cjwu15 1:73f962479f1a 100 /**
cjwu15 1:73f962479f1a 101 Get the value of the LED
cjwu15 1:73f962479f1a 102 @returns string containing the last setting
cjwu15 1:73f962479f1a 103 */
cjwu15 1:73f962479f1a 104 virtual string get() {
cjwu15 1:73f962479f1a 105 int i = 0;
dscrobonia 2:0382d0c13a51 106 memset(result, 0, SIZE_MAX_RESULTS);
cjwu15 1:73f962479f1a 107
dscrobonia 2:0382d0c13a51 108 for (i = 0; i < idx; i++) {
dscrobonia 2:0382d0c13a51 109 sprintf(result+(i*(SIZE_STR_REP + SIZE_STR_TIMESTAMP + 2)), "%2d,", reps[idx]);
dscrobonia 2:0382d0c13a51 110 sprintf(result+(SIZE_STR_REP+1)+(i*(SIZE_STR_REP + SIZE_STR_TIMESTAMP + 2)), "%10d,", times[idx]);
dscrobonia 2:0382d0c13a51 111 }
dscrobonia 2:0382d0c13a51 112
cjwu15 1:73f962479f1a 113 for (i = 0; i < 50; i++)
cjwu15 1:73f962479f1a 114 reps[i] = -1;
cjwu15 1:73f962479f1a 115 idx = 0;
dscrobonia 2:0382d0c13a51 116 printf("results: %s\n", result);
cjwu15 1:73f962479f1a 117
cjwu15 1:73f962479f1a 118 return(result);
cjwu15 1:73f962479f1a 119 }
cjwu15 1:73f962479f1a 120 };
cjwu15 0:02b8d440ded3 121
cjwu15 0:02b8d440ded3 122 #endif // __TOUCH_RESOURCE_H__
cjwu15 0:02b8d440ded3 123