int Dust::get(char *buf, size_t size) { item_t res; unsigned long t; const int interval = 30000; if (0 == m_start) m_start = millis(); t = millis(); if (m_start > t) m_start = t; m_occupancy += pulseIn(m_pin, LOW); if(t - m_start >= interval) { float ratio = m_occupancy / (interval * 10.0); float concentration = 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62; m_result = (int)(concentration * 0.0207916725464941); m_occupancy = 0; m_start = t; } res = itemNew("dust", String(m_result)); return itemCopy(res, buf, size); }
bool mySqlGetItems(MySQL *self, Item **_items) { MYSQL_ROW row; Item *items = *_items; dbg("mySqlGetItems count: %d", mysql_num_rows(self->result)); uint64_t itemId; uint32_t itemType; uint32_t amount; uint8_t inventoryIndex; for (int i = 0; (row = mysql_fetch_row(self->result)); i++) { dbg("preparing item %d", i); itemId = strtoull(row[MYSQL_ITEM_FIELD_item_id], NULL, 10); itemType = strtoul(row[MYSQL_ITEM_FIELD_item_type], NULL, 10); amount = strtoul(row[MYSQL_ITEM_FIELD_amount], NULL, 10); inventoryIndex = strtoul(row[MYSQL_ITEM_FIELD_position_in_inventory], NULL, 10); Item *item = itemNew(itemId, itemType, amount, inventoryIndex); dbg("item %d initialized", i); switch (itemId) { case 1: { item->itemCategory = INVENTORY_CAT_ARMOR; break; } case 2: { item->itemCategory = INVENTORY_CAT_WEAPON; break; } case 3: { item->itemCategory = INVENTORY_CAT_ARMOR; break; } } items[i] = *item; } return true; }
int HT::get(char *buf, size_t size) { item_t res; uint8_t cnt = 7; uint8_t idx = 0; uint8_t data[5]; unsigned int loopCnt = 10000; for(int i = 0; i < 5; i++) data[i] = 0; pinMode(m_pin, OUTPUT); digitalWrite(m_pin, LOW); delay(18); digitalWrite(m_pin, HIGH); delayMicroseconds(40); pinMode(m_pin, INPUT); loopCnt = 10000; while(digitalRead(m_pin) == LOW) if (loopCnt-- == 0) return 0; loopCnt = 10000; while(digitalRead(m_pin) == HIGH) if (loopCnt-- == 0) return 0; for(int bits = 0; bits < 40; bits++) { loopCnt = 10000; while(digitalRead(m_pin) == LOW) if (loopCnt-- == 0) return 0; unsigned long t = micros(); loopCnt = 10000; while(digitalRead(m_pin) == HIGH) if (loopCnt-- == 0) return 0; if ((micros() - t) > 40) data[idx] |= (1 << cnt); if (cnt == 0) { cnt = 7; idx++; } else cnt--; } delayMicroseconds(100); pinMode(m_pin, OUTPUT); digitalWrite(m_pin, HIGH); if(data[4] != data[0] + data[2]) return 0; res = itemNew("humidity", String(data[0])); res += itemNext("temperature", String(data[2])); return itemCopy(res, buf, size); }