Core Base Classes for the Light Endpoints
Dependents: mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet ... more
PhilipsLight.cpp@87:e9d77e9f9eae, 2014-03-12 (annotated)
- Committer:
- ansond
- Date:
- Wed Mar 12 23:47:30 2014 +0000
- Revision:
- 87:e9d77e9f9eae
- Parent:
- 33:339eb862a4c4
- Child:
- 91:8732d54328ae
updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 29:9a99f076129a | 1 | /* Copyright C2013 Doug Anson, MIT License |
ansond | 29:9a99f076129a | 2 | * |
ansond | 29:9a99f076129a | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
ansond | 29:9a99f076129a | 4 | * and associated documentation files the "Software", to deal in the Software without restriction, |
ansond | 29:9a99f076129a | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
ansond | 29:9a99f076129a | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
ansond | 29:9a99f076129a | 7 | * furnished to do so, subject to the following conditions: |
ansond | 29:9a99f076129a | 8 | * |
ansond | 29:9a99f076129a | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
ansond | 29:9a99f076129a | 10 | * substantial portions of the Software. |
ansond | 29:9a99f076129a | 11 | * |
ansond | 29:9a99f076129a | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
ansond | 29:9a99f076129a | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ansond | 29:9a99f076129a | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
ansond | 29:9a99f076129a | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ansond | 29:9a99f076129a | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
ansond | 29:9a99f076129a | 17 | */ |
ansond | 29:9a99f076129a | 18 | |
ansond | 29:9a99f076129a | 19 | #include "PhilipsLight.h" |
ansond | 29:9a99f076129a | 20 | #include "HTTPTransport.h" |
ansond | 29:9a99f076129a | 21 | |
ansond | 29:9a99f076129a | 22 | // default constructor |
ansond | 29:9a99f076129a | 23 | PhilipsLight::PhilipsLight(int id,void *transport,ErrorHandler *error_handler) { |
ansond | 29:9a99f076129a | 24 | this->m_id = id; |
ansond | 29:9a99f076129a | 25 | this->m_state = PL_DEFAULT_STATE; |
ansond | 29:9a99f076129a | 26 | this->m_dim = PL_DEFAULT_DIM; |
ansond | 29:9a99f076129a | 27 | this->m_transport = transport; |
ansond | 29:9a99f076129a | 28 | this->m_error_handler = error_handler; |
ansond | 29:9a99f076129a | 29 | memset(this->m_url,0,PL_URL_BUFFER_LEN+1); |
ansond | 29:9a99f076129a | 30 | memset(this->m_payload,0,PL_PAYLOAD_BUFFER_LEN+1); |
ansond | 29:9a99f076129a | 31 | memset(this->m_response,0,PL_RESPONSE_BUFFER_LEN+1); |
ansond | 29:9a99f076129a | 32 | } |
ansond | 29:9a99f076129a | 33 | |
ansond | 29:9a99f076129a | 34 | // destructor |
ansond | 29:9a99f076129a | 35 | PhilipsLight::~PhilipsLight() { |
ansond | 29:9a99f076129a | 36 | } |
ansond | 29:9a99f076129a | 37 | |
ansond | 29:9a99f076129a | 38 | // turn the light on |
ansond | 29:9a99f076129a | 39 | void PhilipsLight::on() { this->m_state = 1; this->update(); } |
ansond | 29:9a99f076129a | 40 | |
ansond | 29:9a99f076129a | 41 | // turn the light off |
ansond | 29:9a99f076129a | 42 | void PhilipsLight::off() { this->m_state = 0; this->update(); } |
ansond | 29:9a99f076129a | 43 | |
ansond | 87:e9d77e9f9eae | 44 | // blink the light |
ansond | 87:e9d77e9f9eae | 45 | void PhilipsLight::blink() { this->m_state = 2; this->doblink(); } |
ansond | 87:e9d77e9f9eae | 46 | |
ansond | 29:9a99f076129a | 47 | // dim the light |
ansond | 29:9a99f076129a | 48 | void PhilipsLight::dim(int dim) { this->m_dim = dim; this->update(); } |
ansond | 29:9a99f076129a | 49 | |
ansond | 87:e9d77e9f9eae | 50 | // blinking loop |
ansond | 87:e9d77e9f9eae | 51 | void PhilipsLight::doblink() { |
ansond | 87:e9d77e9f9eae | 52 | for(int i=0;i<5;++i) { |
ansond | 87:e9d77e9f9eae | 53 | this->on(); |
ansond | 87:e9d77e9f9eae | 54 | wait_ms(1000); |
ansond | 87:e9d77e9f9eae | 55 | this->off(); |
ansond | 87:e9d77e9f9eae | 56 | } |
ansond | 87:e9d77e9f9eae | 57 | } |
ansond | 87:e9d77e9f9eae | 58 | |
ansond | 29:9a99f076129a | 59 | // set the light state |
ansond | 29:9a99f076129a | 60 | void PhilipsLight::update() { |
ansond | 29:9a99f076129a | 61 | // create the state string |
ansond | 29:9a99f076129a | 62 | char *str_state = "false"; |
ansond | 29:9a99f076129a | 63 | if (this->m_state == 1) str_state = "true"; |
ansond | 29:9a99f076129a | 64 | |
ansond | 29:9a99f076129a | 65 | // make the URL |
ansond | 29:9a99f076129a | 66 | sprintf(this->m_url,PL_URL_TEMPLATE,PL_GW_ADDRESS,this->m_id); |
ansond | 29:9a99f076129a | 67 | |
ansond | 29:9a99f076129a | 68 | // make the payload |
ansond | 29:9a99f076129a | 69 | sprintf(this->m_payload,PL_ON_COMMAND_TEMPLATE,str_state,this->m_dim); |
ansond | 29:9a99f076129a | 70 | |
ansond | 29:9a99f076129a | 71 | // get the transport |
ansond | 29:9a99f076129a | 72 | HTTPTransport *transport = (HTTPTransport *)this->m_transport; |
ansond | 29:9a99f076129a | 73 | |
ansond | 29:9a99f076129a | 74 | // DEBUG |
ansond | 29:9a99f076129a | 75 | this->logger()->log("Setting Philips light: %d payload: %s",this->m_id,this->m_payload); |
ansond | 29:9a99f076129a | 76 | |
ansond | 29:9a99f076129a | 77 | // issue the PUT |
ansond | 33:339eb862a4c4 | 78 | bool success = false; |
ansond | 33:339eb862a4c4 | 79 | if (transport != NULL) transport->httpPut(this->m_url,this->m_payload,strlen(this->m_payload),this->m_response,PL_RESPONSE_BUFFER_LEN); |
ansond | 29:9a99f076129a | 80 | |
ansond | 29:9a99f076129a | 81 | // DEBUG |
ansond | 29:9a99f076129a | 82 | if (success) this->logger()->log("Philips Light: %d updated successfully",this->m_id); |
ansond | 29:9a99f076129a | 83 | else this->logger()->log("Philips Light: %d update FAILED",this->m_id); |
ansond | 29:9a99f076129a | 84 | } |
ansond | 29:9a99f076129a | 85 | |
ansond | 29:9a99f076129a | 86 | // get the error handler |
ansond | 29:9a99f076129a | 87 | ErrorHandler *PhilipsLight::logger() { return this->m_error_handler; } |