Temperature sensor library for Sequana BLE Lab.
Hs3001Driver.cpp@1:b99360c421a7, 2019-03-22 (annotated)
- Committer:
- lru
- Date:
- Fri Mar 22 10:08:18 2019 +0000
- Revision:
- 1:b99360c421a7
- Parent:
- 0:680362180482
Corrected TempSensor API description.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lru | 0:680362180482 | 1 | /* |
lru | 0:680362180482 | 2 | * Copyright (c) 2017-2019 Future Electronics |
lru | 0:680362180482 | 3 | * |
lru | 0:680362180482 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
lru | 0:680362180482 | 5 | * you may not use this file except in compliance with the License. |
lru | 0:680362180482 | 6 | * You may obtain a copy of the License at |
lru | 0:680362180482 | 7 | * |
lru | 0:680362180482 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
lru | 0:680362180482 | 9 | * |
lru | 0:680362180482 | 10 | * Unless required by applicable law or agreed to in writing, software |
lru | 0:680362180482 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
lru | 0:680362180482 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
lru | 0:680362180482 | 13 | * See the License for the specific language governing permissions and |
lru | 0:680362180482 | 14 | * limitations under the License. |
lru | 0:680362180482 | 15 | */ |
lru | 0:680362180482 | 16 | |
lru | 0:680362180482 | 17 | #include <mbed.h> |
lru | 0:680362180482 | 18 | #include "Hs3001Driver.h" |
lru | 0:680362180482 | 19 | |
lru | 0:680362180482 | 20 | |
lru | 0:680362180482 | 21 | Hs3001Driver::Status Hs3001Driver::read(uint16_t& humidity, int16_t& temperature) |
lru | 0:680362180482 | 22 | { |
lru | 0:680362180482 | 23 | static uint8_t buffer[4]; |
lru | 0:680362180482 | 24 | uint32_t val; |
lru | 0:680362180482 | 25 | |
lru | 0:680362180482 | 26 | _i2c.read(_address, reinterpret_cast<char*>(buffer), 1); |
lru | 0:680362180482 | 27 | |
lru | 0:680362180482 | 28 | if ((buffer[0] & 0xC0) == 0x40) { |
lru | 0:680362180482 | 29 | return STATUS_STALLED; |
lru | 0:680362180482 | 30 | } |
lru | 0:680362180482 | 31 | |
lru | 0:680362180482 | 32 | _i2c.read(_address, reinterpret_cast<char*>(buffer), 4); |
lru | 0:680362180482 | 33 | val = (((uint32_t)buffer[0] & 0x3f) << 8) | (uint32_t)buffer[1]; |
lru | 0:680362180482 | 34 | humidity = (uint16_t)((val * 100 + 0x2000) / 0x3fff); |
lru | 0:680362180482 | 35 | |
lru | 0:680362180482 | 36 | val = (((uint32_t)buffer[2]) << 6) | (((uint32_t)buffer[3]) >> 2); |
lru | 0:680362180482 | 37 | temperature = (int16_t)(val * 16500 / 0x3fff) - 4000; |
lru | 0:680362180482 | 38 | |
lru | 0:680362180482 | 39 | // printf("hs3001: raw=%08lx, h=0x%04x, t=0x%04x\n", *reinterpret_cast<uint32_t*>(buffer), humidity, temperature); |
lru | 0:680362180482 | 40 | return STATUS_OK; |
lru | 0:680362180482 | 41 | } |
lru | 0:680362180482 | 42 | |
lru | 0:680362180482 | 43 | void Hs3001Driver::start_conversion(void) |
lru | 0:680362180482 | 44 | { |
lru | 0:680362180482 | 45 | static char buffer = 0; |
lru | 0:680362180482 | 46 | _i2c.write(_address, &buffer, 1); |
lru | 0:680362180482 | 47 | } |
lru | 0:680362180482 | 48 |