add pcint buttons

This commit is contained in:
Harald Pichler 2013-04-10 16:05:31 +02:00
parent 03b7dc8719
commit 617dfba4cb
4 changed files with 197 additions and 0 deletions

View file

@ -12,6 +12,8 @@ PROJECT_SOURCEFILES += static-routing.c
endif
endif
endif
# pcintkey
PROJECT_SOURCEFILES += pcintkey.c
# variable for root Makefile.include
WITH_UIP6=1

View file

@ -59,6 +59,7 @@
#endif
#include "erbium.h"
#include "pcintkey.h"
#include "dev/led.h"
#if defined (PLATFORM_HAS_BUTTON)
@ -135,7 +136,61 @@ info_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_
}
#endif
// pcintkey_ext
/*A simple actuator example. read the key button status*/
RESOURCE(extbutton, METHOD_GET | METHOD_PUT , "sensors/extbutton", "title=\"ext.Button\";rt=\"Text\"");
void
extbutton_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
static char bname1[17]="button1";
static char bname2[17]="button2";
int success = 1;
char temp[100];
int index = 0;
int length = 0; /* |<-------->| */
const char *name = NULL;
size_t len = 0;
switch(REST.get_method_type(request)){
case METHOD_GET:
// jSON Format
index += sprintf(temp + index,"{\n \"%s\" : ",bname1);
if(is_button_ext1())
index += sprintf(temp + index,"\"on\",\n");
else
index += sprintf(temp + index,"\"off\",\n");
index += sprintf(temp + index," \"%s\" : ",bname2);
if(is_button_ext2())
index += sprintf(temp + index,"\"on\"\n");
else
index += sprintf(temp + index,"\"off\"\n");
index += sprintf(temp + index,"}\n");
length = strlen(temp);
memcpy(buffer, temp,length );
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
REST.set_response_payload(response, buffer, length);
break;
case METHOD_PUT:
if (success && (len=REST.get_post_variable(request, "name", &name))) {
PRINTF("name %s\n", name);
memcpy(bname1, name,len);
bname1[len]=0;
} else {
success = 0;
}
break;
default:
success = 0;
}
if (!success) {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
/*A simple actuator example, post variable mode, relay is activated or deactivated*/
RESOURCE(led1, METHOD_GET | METHOD_PUT , "actuators/led1", "title=\"Led1\";rt=\"led\"");
void
@ -510,6 +565,7 @@ PROCESS_THREAD(rest_server_example, ev, data)
/* Activate the application-specific resources. */
rest_activate_resource(&resource_led1);
rest_activate_resource(&resource_extbutton);
#if REST_RES_INFO
rest_activate_resource(&resource_info);
#endif

View file

@ -0,0 +1,88 @@
/*
* Copyright (c) 2010 harald pichler
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* \file
*
* \brief
* This file provides Raven KEY support.
*
* \author
* Harald Pichler harald@the-develop.net
*
*/
#include "key.h"
/*---------------------------------------------------------------------------*/
/**
* \brief This will intialize the KEY for button readings.
*/
void
key_init(void)
{
// ext1
DDRB |= (0<<DDB5); // Set pin as input
PORTB |= (1<<PORTB5); // Set port PORTE bint 5 with pullup resistor
// ext2
DDRB |= (0<<DDB6); // Set pin as input
PORTB |= (1<<PORTB6); // Set port PORTE bint 5 with pullup resistor
}
/*---------------------------------------------------------------------------*/
/**
* \brief This will poll run key_task() to determine if a button has been pressed.
*
* \retval True if button is pressed
* \retval False if button is not pressed
*/
uint8_t
is_button_ext1(void)
{
/* Return true if button has been pressed. */
if ( PINB & (1<<PINB5) ) {
return 0;
}
else{
return 1;
}
}
uint8_t
is_button_ext2(void)
{
/* Return true if button has been pressed. */
if ( PINB & (1<<PINB6) ) {
return 0;
}
else{
return 1;
}
}

View file

@ -0,0 +1,51 @@
/*
* Copyright (c) 2010 Harald Pichler
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* \file
*
* \brief
* This file provides Raven Key support.
*
* \author
* Harald Pixhlwe harald@the-develop.net
*
*/
#ifndef __KEY_H__
#define __KEY_H__
#include <stdint.h>
#include <avr/io.h>
void key_init(void);
uint8_t is_button_ext1(void);
uint8_t is_button_ext2(void);
#endif /* __KEY_H__ */