fastspin Basic C class dht22
Rsadeika
Posts: 3,837
in Propeller 1
I decided to try out using the dht22.c program as a class for the fastspin Basic program shown below. As you can see the compile came back with errors. The strange part is, if I comment out 'temp = dht.dht22_getTemp(1)' and 'humid = dht.dht22_getHumidity()', then it compiles without errors. Do not know why it cannot find dht22_getTemp() and dht22_getHumidity(). I guess I need a short tutorial on how to do this correctly.
Ray
dht22.c
dht22.h
Ray
"F:/fastspin/spin2gui/bin/fastspin" -l -O2 -L "./include" "F:/flexbasic/test3/test3.bas"
Propeller Spin/PASM Compiler 'FastSpin' (c) 2011-2019 Total Spectrum Software Inc.
Version 3.9.25 Compiled on: Apr 14 2019
test3.bas
|-dht22.c
warning: : Preprocessor warnings:
F:/flexbasic/test3/dht22.h:123: warning: End of file with no newline, supplemented newline
*/
from F:/flexbasic/test3/dht22.c: 16: #include "dht22.h"
F:/flexbasic/test3/dht22.c:176: warning: End of input with no newline, supplemented newline
*/
F:/flexbasic/test3/test3.bas(9) error: unknown identifier dht22_gettemp in class dht22
F:/flexbasic/test3/test3.bas(9) error: dht22_gettemp is not a member of dht22
F:/flexbasic/test3/test3.bas(12) error: unknown identifier dht22_gethumidity in class dht22
F:/flexbasic/test3/test3.bas(12) error: dht22_gethumidity is not a member of dht22
F:/flexbasic/test3/test3.bas(9) error: unknown identifier dht22_gettemp in class dht22
F:/flexbasic/test3/test3.bas(9) error: dht22_gettemp is not a member of dht22
F:/flexbasic/test3/test3.bas(9) error: unknown identifier dht22_gettemp in class dht22
F:/flexbasic/test3/test3.bas(9) error: dht22_gettemp is not a member of dht22
F:/flexbasic/test3/test3.bas(12) error: unknown identifier dht22_gethumidity in class dht22
F:/flexbasic/test3/test3.bas(12) error: dht22_gethumidity is not a member of dht22
F:/flexbasic/test3/test3.bas(12) error: unknown identifier dht22_gethumidity in class dht22
F:/flexbasic/test3/test3.bas(12) error: dht22_gethumidity is not a member of dht22
child process exited abnormally
'test3.bas dim dht as class using "dht22.c" dim as single temp,humid print "Testing..." dht.dht22_read(2) temp = dht.dht22_getTemp(1) print temp; dht.dht22_read(2) humid = dht.dht22_getHumidity() print humid
dht22.c
/** * @file dht22.h * * @author Matthew Matz * * @version 0.6 * * @copyright * Copyright (C) Parallax, Inc. 2017. All Rights MIT Licensed. * * @brief This Propeller C library was created for the DHT22 temperature * and humidity sensor module. */ #include <propeller.h> #include "dht22.h" static int dht_timeout[32]; static int dht_last_temp, dht_last_humidity, dht_timeout_ignore; void dht22_set_timeout_ignore(int dht_pin, char ignore_timeout) { // Set up a pin mask int dhtpm = (1 << dht_pin); // Set or clear the bit for the specified pin if (ignore_timeout) { dht_timeout_ignore |= dhtpm; } else { dht_timeout_ignore &= ~dhtpm; } } int dht22_getTemp(char temp_units) { int tt = dht_last_temp; if (temp_units == KELVIN) tt += 2732; else if (temp_units == FAHRENHEIT) tt = tt * 9 / 5 + 320; return tt; } int dht22_getHumidity() { return dht_last_humidity; } char dht22_read(int dht_pin) { // Check to make sure the pin is valid if(dht_pin >= 0 && dht_pin < 32) { // Set up a pin mask int dhtpm = (1 << dht_pin); // Has it been less than 0.5 seconds since this function was last called? if (CNT - dht_timeout[dht_pin] < CLKFREQ/2 && dht_timeout[dht_pin] != 0 && !(dht_timeout_ignore & dhtpm)) waitcnt(dht_timeout[dht_pin] + CLKFREQ/2); // If so, wait until 0.5 s has elapsed. // Set up variables to hold incoming data int dhtp[44]; int dhtc = 0, dhtk = 0, dhts = 0, dhto = 0, dhth = 0, dhtt = 0, dhte = 0; // Pull the data pin low OUTA &= ~dhtpm; DIRA |= dhtpm; // Wait 18 ms waitcnt(CLKFREQ/55 + CNT); // Set the data pin to an input DIRA &= ~dhtpm; // Set a 2 second timer for the pulse measurements to prevent lockup dhto = CNT; // Set up a state monitor int dhst = 0; // After each full pulse, store the system clock in an // array until 42 measurements are recorded (1 start pulse + // 5 bytes of data) // Loops enough times to capture data if the sensor is // operating properly for (int j = 0; j < CLKFREQ / 2000; j++) { if (!(INA & dhtpm) && !dhst) dhst = 1; if ((INA & dhtpm) && dhst) { dhst = 0; dhtp[dhtc] = CNT; if (dhtc < 44) { dhtc++; } } } // Measure the length of each pulse to determine if it's a 0 or 1. // Bytes 1 & 2 are % relative humidity for (dhtc = 2; dhtc < 18; dhtc++) { dhth <<= 1; dhth |= (dhtp[dhtc] - dhtp[dhtc-1] > CLKFREQ/9800); } // Bytes 3 & 4 are temperature (Celsius) for (dhtc = 18; dhtc < 34; dhtc++) { dhtt <<= 1; dhtt |= (dhtp[dhtc] - dhtp[dhtc-1] > CLKFREQ/9800); } // Byte 5 is a additive checksum for (dhtc = 34; dhtc < 42; dhtc++) { dhtk <<= 1; dhtk |= (dhtp[dhtc] - dhtp[dhtc-1] > CLKFREQ/9800); } // The MSB of the temp is a sign bit, but it's not 2's-compliment... // So determine it, then remove it dhts = dhtt & 32768; dhtt &= 32767; // If the temp is negative, multiply it by -1 dhtt *= (dhts ? -1 : 1); // Calculate the checksum dhte = (((dhth >> 8) & 255) + (dhth & 255) + ((dhtt >> 8) & 255) + (dhtt & 255)) & 255; // Save the result into a single int // Bits 0-15: (Celsius Temp * 20) + 100 // Bits 16-31: (Humidity) // Checksum // Save the temp and humidity into the passed in variables // Convert to Farenheiht if requested dht_last_humidity = dhth; dht_last_temp = dhtt; // Save the system clock so that the sensor // is not queried again for at least .5 seconds dht_timeout[dht_pin] = CNT; // Return true if the checksum matches return dhte == dhtk; } else { // Invalid pin return 0; } } /** * TERMS OF USE: MIT License * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */
dht22.h
/** * @file dht22.h * * @author Matthew Matz * * @version 0.6 * * @copyright * Copyright (C) Parallax, Inc. 2017. All Rights MIT Licensed. * * @brief This Propeller C library was created for the DHT22 temperature * and humidity sensor module. */ #ifndef __DHT22_SENSOR_H__ // Prevents duplicate #define __DHT22_SENSOR_H__ // declarations #if defined(__cplusplus) // If compiling for C++ extern "C" { // Compile for C #endif // Units of temperature #ifndef CELSIUS #define CELSIUS 0 #endif #ifndef FAHRENHEIT #define FAHRENHEIT 1 #endif #ifndef KELVIN #define KELVIN 2 #endif /** * @brief Triggers a reading of the temperature and relative humidity from the sensor module. * * @details This function instructs the temperature and humidity sensor module * to take a reading. * * @note The sensor can be read every 500ms. If this function is called * before 500ms from the previous call has elapsed, the function waits unless the * dht22_set_timeout_ignore() function was used to instruct modules on that pin to * ignore the 0.5 second minimum timeout. * * @param dht_pin defines which Propeller MCU I/O pin the sensor is attached to. * Requires a 10 kOhm pullup resistor to 3.3V when connecting to the Propeller MCU. * * @returns 1 if a valid checksum was recieved, 0 if the checksum is invalid. */ char dht22_read(int dht_pin); /** * @brief The function is used to set or clear an ignore timeout instruction for * modules connected to the specified pin. * * @param dht_pin defines which pin the sensor is attached to. * * @param ignore_timeout when true (1), the function instructs the Propeller MCU to * ignore the protective timeout that prevents the sensor from being read before * it is ready. This parameter is set to false (0) by default. */ void dht22_set_timeout_ignore(int dht_pin, char ignore_timeout); /** * @brief Retrieves the last temperature reading made by the dht22_read() function. * * @param temp_units allows the temperature unit to be specified in (0) Celsius, * (1) Fahrenheit, or (2) Kelvin. * * @returns the temperature read in degree-tenths of the unit specified. */ int dht22_getTemp(char temp_units); /** * @brief Retrieves the last humidity reading made by the dht22_read() function. * * @returns the last relative humidity read in tenths of a percent. */ int dht22_getHumidity(); #if defined(__cplusplus) } // End compile for C block #endif /* __cplusplus */ #endif // End prevent duplicate forward /* __DHT22_SENSOR_H__ */ // declarations block /** * TERMS OF USE: MIT License * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */
Comments
Program size is 5588 bytes.
WOW, I guess fastspin Optimization really likes C code.
Ray
Ray
I noticed when compiling, I am getting a Preprocessor warning, how do I fix that.
I also noticed that when the program just had a simple do .. loop, the program size was, when optimized, 5588 bytes. Now with just an if loop added, it is up to 9576 bytes optimized. What are some of the big ticket, code wise additions, to be aware of?
The dht22 program was a fairly simple conversion to make to have it run with fastspin Basic. I just did a quick test to see how the SimpleIDE C code for datetime would work. That is a lot more complicated, because it requests a lot of different code that is all over the place. Not sure how to even gather and place it in an area where fastspin could find it all. Will have to do some more experimentation with that one.
Ray
dht22_fs.c
Mike R.
Addendum: Also, if you haven't read the .md files included with fastspin you'll find them interesting. They're terse but full of hints and comments on how this pretty cool compiler works and interacts with interlanguage type operations such as you're contemplating.
This fastspin is a very interesting tool, he has a lot of things going on under the hood. The other thing that I noticed was, because fastspin produces a .pasm file, you could almost use that to learn some things about PASM. The only problem that I see, that would confuse things for me, is the LMM stuff that shows up in the file listing. I know that, in the Propeller manual, there is some PASM code for flashing a pin, hopefully you would have an LED attached, to see the code come to life. To bad there isn't a switch to turn on/off the LMM, that way you could produce some code, and check out some really simple routines.
I am thinking along the lines of using fastspin Basic to produce some prototype code, and then you could review the .pasm file to see if and where you could tweak the code, I think. Since I do not think in PASM, I think producing a line in Basic like 'high(1)' to turn on an LED, and then look at the PASM code, to see what it took do that, might be a way to learn PASM, for me anyway.
Ray