Commit d41368b8 authored by Julien Leduc's avatar Julien Leduc

Initial GPLv3 code base for the DCES project

parent 0017a3d0
--------------------
dces-dtrhf-ser1ch-v1
--------------------
Here is what the name stands for:
Data Center Environmental Sensor sensing Dust, Temperature, Relative Humidity and Fan speed sending results on SERial 1 CHannel - version 1.
This is a long name, but it precisely defines what is implemented in the project.
Compiling and flashing the firmware
-----------------------------------
Some prerequites, to compile this firmware, you need to install the ``arduino-core`` package, the full IDE is not needed, because it is simple an faster to build from the commandline:
::
$ yum instal arduino-core
Before going any further you need to adapt the configuration file to your setup: edit ``dces-dtrhf-ser1ch-v1.conf`` in ``dces-dtrhf-ser1ch-v1/arduino/``.
Then to build and flash the arduino board, simply run ``build.sh`` from command line:
::
$ cd dces-dtrhf-ser1ch-v1/arduino
$ ./build.sh
It is only going to build ``dces-dtrhf-ser1ch-v1/arduino/final/final.ino`` using ``dces-dtrhf-ser1ch-v1/arduino/final/Makefile``.
To clean the project, just ``make clean`` from ``dces-dtrhf-ser1ch-v1/arduino/final`` directory.
If ``UPLOAD=true`` in ``dces-dtrhf-ser1ch-v1.conf``, the firmware is going to be flashed on the arduino board.
Configuring the firmware
------------------------
Firmware configuration is done from the arduino serial port using ``SerialCommand`` library.
When the firmware starts it displays measured data by default:
::
$ picocom /dev/ttyACM0
port is : /dev/ttyACM0
flowcontrol : none
baudrate is : 9600
parity is : none
databits are : 8
escape is : C-a
local echo is : no
noinit is : no
noreset is : no
nolock is : no
send_cmd is : sz -vv
receive_cmd is : rz -vv
imap is :
omap is :
emap is : crcrlf,delbs,
Terminal ready
8471, 8384, 446, 26.69, 31.37, 196, 0, 960
8419, 8448, 163, 26.69, 31.44, 196, 0, 960
...
#!/bin/bash
##############################################################################
#
# This file is part of the DCES project.
# See http://www.ohwr.org/projects/dces-dtrhf-ser1ch-v1
#
# Copyright (C) 2016 CERN
# All rights not expressly granted are reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# @author DCES Dev team, dces-dev@cern.ch
#############################################################################
. ./dces-dtrhf-ser1ch-v1.conf 2>/dev/null
cd final
if [[ $UPLOAD = "true" ]]
then
echo "Compiling and upgrading arduino firmware"
make BOARD=$BOARD SERIALDEV=$DEVICE upload
else
echo "Compiling only"
make BOARD=$BOARD SERIALDEV=$DEVICE
fi
###
# Example configuration file for dust sensor firmware update
###
###
# Configuration section for firmware compilation/upgrade
###
[MAKE_SECTION]
# UPLOAD="true" automatically build and upload firmware upon rpm installation
UPLOAD="false"
# Arduino board type for dust sensor as specified in /usr/share/arduino/hardware/arduino/boards.txt
BOARD="mega2560"
# TTY device for serial communications
DEVICE="/dev/ttyACM0"
#_______________________________________________________________________________
#
# edam's Arduino makefile
#_______________________________________________________________________________
# version 0.5
#
# Copyright (C) 2011, 2012, 2013 Tim Marston <tim@ed.am>.
#
# 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.
#
#_______________________________________________________________________________
#
#
# This is a general purpose makefile for use with Arduino hardware and
# software. It works with the arduino-1.0 and later software releases. It
# should work GNU/Linux and OS X. To download the latest version of this
# makefile visit the following website where you can also find documentation on
# it's use. (The following text can only really be considered a reference.)
#
# http://ed.am/dev/make/arduino-mk
#
# This makefile can be used as a drop-in replacement for the Arduino IDE's
# build system. To use it, just copy arduino.mk in to your project directory.
# Or, you could save it somewhere (I keep mine at ~/src/arduino.mk) and create
# a symlink to it in your project directory, named "Makefile". For example:
#
# $ ln -s ~/src/arduino.mk Makefile
#
# The Arduino software (version 1.0 or later) is required. On GNU/Linux you
# can probably install the software from your package manager. If you are
# using Debian (or a derivative), try `apt-get install arduino`. Otherwise,
# you can download the Arduino software manually from http://arduino.cc/. It
# is suggested that you install it at ~/opt/arduino (or /Applications on OS X)
# if you are unsure.
#
# If you downloaded the Arduino software manually and unpacked it somewhere
# other than ~/opt/arduino (or /Applications), you will need to set up the
# ARDUINODIR environment variable to be the path where you unpacked it. (If
# unset, ARDUINODIR defaults to some sensible places). You could set this in
# your ~/.profile by adding something like this:
#
# export ARDUINODIR=~/somewhere/arduino-1.0
#
# For each project, you will also need to set BOARD to the type of Arduino
# you're building for. Type `make boards` for a list of acceptable values.
# For example:
#
# $ export BOARD=uno
# $ make
#
# You may also need to set SERIALDEV if it is not detected correctly.
#
# The presence of a .ino (or .pde) file causes the arduino.mk to automatically
# determine values for SOURCES, TARGET and LIBRARIES. Any .c, .cc and .cpp
# files in the project directory (or any "util" or "utility" subdirectories)
# are automatically included in the build and are scanned for Arduino libraries
# that have been #included. Note, there can only be one .ino (or .pde) file in
# a project directory and if you want to be compatible with the Arduino IDE, it
# should be called the same as the directory name.
#
# Alternatively, if you want to manually specify build variables, create a
# Makefile that defines SOURCES and LIBRARIES and then includes arduino.mk.
# (There is no need to define TARGET). You can also specify the BOARD here, if
# the project has a specific one. Here is an example Makefile:
#
# SOURCES := main.cc other.cc
# LIBRARIES := EEPROM
# BOARD := pro5v
# include ~/src/arduino.mk
#
# Here is a complete list of configuration parameters:
#
# ARDUINODIR The path where the Arduino software is installed on your system.
#
# ARDUINOCONST The Arduino software version, as an integer, used to define the
# ARDUINO version constant. This defaults to 100 if undefined.
#
# AVRDUDECONF The avrdude.conf to use. If undefined, this defaults to a guess
# based on where avrdude is. If set empty, no avrdude.conf is
# passed to avrdude (so the system default is used).
#
# AVRDUDEFLAGS Specify any additional flags for avrdude. The usual flags,
# required to build the project, will be appended to this.
#
# AVRTOOLSPATH A space-separated list of directories that is searched in order
# when looking for the avr build tools. This defaults to PATH,
# followed by subdirectories in ARDUINODIR.
#
# BOARD Specify a target board type. Run `make boards` to see available
# board types.
#
# CPPFLAGS Specify any additional flags for the compiler. The usual flags,
# required to build the project, will be appended to this.
#
# LINKFLAGS Specify any additional flags for the linker. The usual flags,
# required to build the project, will be appended to this.
#
# LIBRARIES A list of Arduino libraries to build and include. This is set
# automatically if a .ino (or .pde) is found.
#
# LIBRARYPATH A space-separated list of directories that is searched in order
# when looking for Arduino libraries. This defaults to "libs",
# "libraries" (in the project directory), then your sketchbook
# "libraries" directory, then the Arduino libraries directory.
#
# SERIALDEV The POSIX device name of the serial device that is the Arduino.
# If unspecified, an attempt is made to guess the name of a
# connected Arduino's serial device, which may work in some cases.
#
# SOURCES A list of all source files of whatever language. The language
# type is determined by the file extension. This is set
# automatically if a .ino (or .pde) is found.
#
# TARGET The name of the target file. This is set automatically if a
# .ino (or .pde) is found, but it is not necessary to set it
# otherwise.
#
# This makefile also defines the following goals for use on the command line
# when you run make:
#
# all This is the default if no goal is specified. It builds the
# target.
#
# target Builds the target.
#
# upload Uploads the target (building it, as necessary) to an attached
# Arduino.
#
# clean Deletes files created during the build.
#
# boards Display a list of available board names, so that you can set the
# BOARD environment variable appropriately.
#
# monitor Start `screen` on the serial device. This is meant to be an
# equivalent to the Arduino serial monitor.
#
# size Displays size information about the built target.
#
# bootloader Burns the bootloader for your board to it.
#
# <file> Builds the specified file, either an object file or the target,
# from those that that would be built for the project.
#_______________________________________________________________________________
#
# project specific vars
PROJECTDIR := $(shell echo $(abspath $(lastword $(MAKEFILE_LIST))) |\
sed -Ee 's/^(.*dces-dtrhf-ser1ch-v1).*$$/\1/')
# default arduino software directory, check software exists
ifndef ARDUINODIR
ARDUINODIR := $(firstword $(wildcard ~/opt/arduino /usr/share/arduino \
/Applications/Arduino.app/Contents/Resources/Java \
$(HOME)/Applications/Arduino.app/Contents/Resources/Java))
endif
ifeq "$(wildcard $(ARDUINODIR)/hardware/arduino/boards.txt)" ""
$(error ARDUINODIR is not set correctly; arduino software not found)
endif
# default arduino version
ARDUINOCONST ?= 100
# default path for avr tools
AVRTOOLSPATH ?= $(subst :, , $(PATH)) $(ARDUINODIR)/hardware/tools \
$(ARDUINODIR)/hardware/tools/avr/bin
# default path to find libraries
LIBRARYPATH ?= $(PROJECTDIR)/arduino/libraries libraries libs $(SKETCHBOOKDIR)/libraries $(ARDUINODIR)/libraries
# default serial device to a poor guess (something that might be an arduino)
SERIALDEVGUESS := 0
ifndef SERIALDEV
#SERIALDEV := $(firstword $(wildcard \
/dev/ttyACM? /dev/ttyUSB? /dev/tty.usbserial* /dev/tty.usbmodem*))
SERIALDEVGUESS := 1
endif
# no board?
ifndef BOARD
ifneq "$(MAKECMDGOALS)" "boards"
ifneq "$(MAKECMDGOALS)" "clean"
$(error BOARD is unset. Type 'make boards' to see possible values)
endif
endif
endif
# obtain board parameters from the arduino boards.txt file
BOARDSFILE := $(ARDUINODIR)/hardware/arduino/boards.txt
readboardsparam = $(shell sed -ne "s/$(BOARD).$(1)=\(.*\)/\1/p" $(BOARDSFILE))
BOARD_BUILD_MCU := $(call readboardsparam,build.mcu)
BOARD_BUILD_FCPU := $(call readboardsparam,build.f_cpu)
BOARD_BUILD_VARIANT := $(call readboardsparam,build.variant)
BOARD_UPLOAD_SPEED := $(call readboardsparam,upload.speed)
BOARD_UPLOAD_PROTOCOL := $(call readboardsparam,upload.protocol)
BOARD_USB_VID := $(call readboardsparam,build.vid)
BOARD_USB_PID := $(call readboardsparam,build.pid)
BOARD_BOOTLOADER_UNLOCK := $(call readboardsparam,bootloader.unlock_bits)
BOARD_BOOTLOADER_LOCK := $(call readboardsparam,bootloader.lock_bits)
BOARD_BOOTLOADER_LFUSES := $(call readboardsparam,bootloader.low_fuses)
BOARD_BOOTLOADER_HFUSES := $(call readboardsparam,bootloader.high_fuses)
BOARD_BOOTLOADER_EFUSES := $(call readboardsparam,bootloader.extended_fuses)
BOARD_BOOTLOADER_PATH := $(call readboardsparam,bootloader.path)
BOARD_BOOTLOADER_FILE := $(call readboardsparam,bootloader.file)
# obtain preferences from the IDE's preferences.txt
PREFERENCESFILE := $(firstword $(wildcard \
$(HOME)/.arduino/preferences.txt $(HOME)/Library/Arduino/preferences.txt))
ifneq "$(PREFERENCESFILE)" ""
readpreferencesparam = $(shell sed -ne "s/$(1)=\(.*\)/\1/p" $(PREFERENCESFILE))
SKETCHBOOKDIR := $(call readpreferencesparam,sketchbook.path)
endif
# invalid board?
ifeq "$(BOARD_BUILD_MCU)" ""
ifneq "$(MAKECMDGOALS)" "boards"
ifneq "$(MAKECMDGOALS)" "clean"
$(error BOARD is invalid. Type 'make boards' to see possible values)
endif
endif
endif
# auto mode?
INOFILE := $(wildcard *.ino *.pde)
ifdef INOFILE
ifneq "$(words $(INOFILE))" "1"
$(error There is more than one .pde or .ino file in this directory!)
endif
# automatically determine sources and targeet
TARGET := $(basename $(INOFILE))
SOURCES := $(INOFILE) \
$(wildcard *.c *.cc *.cpp *.C) \
$(wildcard $(addprefix util/, *.c *.cc *.cpp *.C)) \
$(wildcard $(addprefix utility/, *.c *.cc *.cpp *.C))
# automatically determine included libraries
LIBRARIES := $(filter $(notdir $(wildcard $(addsuffix /*, $(LIBRARYPATH)))), \
$(shell sed -ne "s/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p" $(SOURCES)))
endif
# software
findsoftware = $(firstword $(wildcard $(addsuffix /$(1), $(AVRTOOLSPATH))))
CC := $(call findsoftware,avr-gcc)
CXX := $(call findsoftware,avr-g++)
LD := $(call findsoftware,avr-ld)
AR := $(call findsoftware,avr-ar)
OBJCOPY := $(call findsoftware,avr-objcopy)
AVRDUDE := $(call findsoftware,avrdude)
AVRSIZE := $(call findsoftware,avr-size)
# directories
ARDUINOCOREDIR := $(ARDUINODIR)/hardware/arduino/cores/arduino
LIBRARYDIRS := $(foreach lib, $(LIBRARIES), \
$(firstword $(wildcard $(addsuffix /$(lib), $(LIBRARYPATH)))))
LIBRARYDIRS += $(addsuffix /utility, $(LIBRARYDIRS))
# files
TARGET := $(if $(TARGET),$(TARGET),a.out)
OBJECTS := $(addsuffix .o, $(basename $(SOURCES)))
DEPFILES := $(patsubst %, .dep/%.dep, $(SOURCES))
ARDUINOLIB := .lib/arduino.a
ARDUINOLIBOBJS := $(foreach dir, $(ARDUINOCOREDIR) $(LIBRARYDIRS), \
$(patsubst %, .lib/%.o, $(wildcard $(addprefix $(dir)/, *.c *.cpp))))
BOOTLOADERHEX := $(addprefix \
$(ARDUINODIR)/hardware/arduino/bootloaders/$(BOARD_BOOTLOADER_PATH)/, \
$(BOARD_BOOTLOADER_FILE))
# avrdude confifuration
ifeq "$(AVRDUDECONF)" ""
ifeq "$(AVRDUDE)" "$(ARDUINODIR)/hardware/tools/avr/bin/avrdude"
AVRDUDECONF := $(ARDUINODIR)/hardware/tools/avr/etc/avrdude.conf
else
AVRDUDECONF := $(wildcard $(AVRDUDE).conf)
endif
endif
# flags
CPPFLAGS += -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections
CPPFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
CPPFLAGS += -mmcu=$(BOARD_BUILD_MCU)
CPPFLAGS += -DF_CPU=$(BOARD_BUILD_FCPU) -DARDUINO=$(ARDUINOCONST)
CPPFLAGS += -DUSB_VID=$(BOARD_USB_VID) -DUSB_PID=$(BOARD_USB_PID)
CPPFLAGS += -I. -Iutil -Iutility -I $(ARDUINOCOREDIR)
CPPFLAGS += -I $(ARDUINODIR)/hardware/arduino/variants/$(BOARD_BUILD_VARIANT)/
CPPFLAGS += $(addprefix -I , $(LIBRARYDIRS))
CPPDEPFLAGS = -MMD -MP -MF .dep/$<.dep
CPPINOFLAGS := -x c++ -include $(ARDUINOCOREDIR)/Arduino.h
AVRDUDEFLAGS += $(addprefix -C , $(AVRDUDECONF)) -DV
AVRDUDEFLAGS += -p $(BOARD_BUILD_MCU) -P $(SERIALDEV)
AVRDUDEFLAGS += -c $(BOARD_UPLOAD_PROTOCOL) -b $(BOARD_UPLOAD_SPEED)
LINKFLAGS += -Os -Wl,--gc-sections -mmcu=$(BOARD_BUILD_MCU)
# figure out which arg to use with stty (for OS X, GNU and busybox stty)
STTYFARG := $(shell stty --help 2>&1 | \
grep -q 'illegal option' && echo -f || echo -F)
# include dependencies
ifneq "$(MAKECMDGOALS)" "clean"
-include $(DEPFILES)
endif
# default rule
.DEFAULT_GOAL := all
#_______________________________________________________________________________
# RULES
.PHONY: all target upload clean boards monitor size bootloader
all: target
target: $(TARGET).hex
upload: target
@echo "\nUploading to board..."
@test -n "$(SERIALDEV)" || { \
echo "error: SERIALDEV could not be determined automatically." >&2; \
exit 1; }
@test 0 -eq $(SERIALDEVGUESS) || { \
echo "*GUESSING* at serial device:" $(SERIALDEV); \
echo; }
ifeq "$(BOARD_BOOTLOADER_PATH)" "caterina"
stty $(STTYFARG) $(SERIALDEV) speed 1200
sleep 1
else
stty $(STTYFARG) $(SERIALDEV) hupcl
endif
stty $(STTYFARG) $(SERIALDEV) hupcl
sleep 0.1
stty $(STTYFARG) $(SERIALDEV) -hupcl
$(AVRDUDE) $(AVRDUDEFLAGS) -U flash:w:$(TARGET).hex:i
wait
clean:
rm -f $(OBJECTS)
rm -f $(TARGET).elf $(TARGET).hex $(ARDUINOLIB) *~
rm -rf .lib .dep
boards:
@echo "Available values for BOARD:"
@sed -nEe '/^#/d; /^[^.]+\.name=/p' $(BOARDSFILE) | \
sed -Ee 's/([^.]+)\.name=(.*)/\1 \2/' \
-e 's/(.{12}) *(.*)/\1 \2/'
monitor:
@test -n "$(SERIALDEV)" || { \
echo "error: SERIALDEV could not be determined automatically." >&2; \
exit 1; }
@test -n `which screen` || { \
echo "error: can't find GNU screen, you might need to install it." >&2 \
exit 1; }
@test 0 -eq $(SERIALDEVGUESS) || { \
echo "*GUESSING* at serial device:" $(SERIALDEV); \
echo; }
screen $(SERIALDEV)
size: $(TARGET).elf
echo && $(AVRSIZE) --format=avr --mcu=$(BOARD_BUILD_MCU) $(TARGET).elf
bootloader:
@echo "Burning bootloader to board..."
@test -n "$(SERIALDEV)" || { \
echo "error: SERIALDEV could not be determined automatically." >&2; \
exit 1; }
@test 0 -eq $(SERIALDEVGUESS) || { \
echo "*GUESSING* at serial device:" $(SERIALDEV); \
echo; }
stty $(STTYFARG) $(SERIALDEV) hupcl
$(AVRDUDE) $(AVRDUDEFLAGS) -U lock:w:$(BOARD_BOOTLOADER_UNLOCK):m
$(AVRDUDE) $(AVRDUDEFLAGS) -eU lfuse:w:$(BOARD_BOOTLOADER_LFUSES):m
$(AVRDUDE) $(AVRDUDEFLAGS) -U hfuse:w:$(BOARD_BOOTLOADER_HFUSES):m
ifneq "$(BOARD_BOOTLOADER_EFUSES)" ""
$(AVRDUDE) $(AVRDUDEFLAGS) -U efuse:w:$(BOARD_BOOTLOADER_EFUSES):m
endif
ifneq "$(BOOTLOADERHEX)" ""
$(AVRDUDE) $(AVRDUDEFLAGS) -U flash:w:$(BOOTLOADERHEX):i
endif
$(AVRDUDE) $(AVRDUDEFLAGS) -U lock:w:$(BOARD_BOOTLOADER_LOCK):m
# building the target
$(TARGET).hex: $(TARGET).elf
$(OBJCOPY) -O ihex -R .eeprom $< $@
.INTERMEDIATE: $(TARGET).elf
$(TARGET).elf: $(ARDUINOLIB) $(OBJECTS)
$(CC) $(LINKFLAGS) $(OBJECTS) $(ARDUINOLIB) -lm -o $@
%.o: %.c
mkdir -p .dep/$(dir $<)
$(COMPILE.c) $(CPPDEPFLAGS) -o $@ $<
%.o: %.cpp
mkdir -p .dep/$(dir $<)
$(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $<
%.o: %.cc
mkdir -p .dep/$(dir $<)
$(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $<
%.o: %.C
mkdir -p .dep/$(dir $<)
$(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $<
%.o: %.ino
mkdir -p .dep/$(dir $<)
$(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $(CPPINOFLAGS) $<
%.o: %.pde
mkdir -p .dep/$(dir $<)
$(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $(CPPINOFLAGS) $<
# building the arduino library
$(ARDUINOLIB): $(ARDUINOLIBOBJS)
$(AR) rcs $@ $?
.lib/%.c.o: %.c
mkdir -p $(dir $@)
$(COMPILE.c) -o $@ $<
.lib/%.cpp.o: %.cpp
mkdir -p $(dir $@)
$(COMPILE.cpp) -o $@ $<
.lib/%.cc.o: %.cc
mkdir -p $(dir $@)
$(COMPILE.cpp) -o $@ $<
.lib/%.C.o: %.C
mkdir -p $(dir $@)
$(COMPILE.cpp) -o $@ $<
# Local Variables:
# mode: makefile
# tab-width: 4
# End:
/******************************************************************************
*
* This file is part of the DCES project.
* See http://www.ohwr.org/projects/dces-dtrhf-ser1ch-v1
*
* Copyright (C) 2016 CERN
* All rights not expressly granted are reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* @author DCES Dev team, dces-dev@cern.ch
*****************************************************************************/
#include <stdlib.h>
#include <EEPROM.h>
#include <string.h>
#include <lib_hih6120.h>
#include <lib_fan.h>
#include <lib_dust.h>
#include <lib_leds.h>
#include <SoftwareSerial.h>
#include <SerialCommand.h>
#define WAIT_TIME 2500 // Waiting time before restarting loop() in millis
#define NMEAS 1 // Number of dust measures between 2 humidity/temperature measures
#define FSR 0b11111111 // Full Scale Range
// ADDRESSES of config
#define DCYADDR 0
#define RPMTOADDR 2
#define NMEASADDR 4
#define WTIMEADDR 6
#define RPMMINADDR 8
#define RPMMAXADDR 10
// LENGTH of the different FIELDS:
#define TLENGTH 6 // Temperature
#define HLENGTH 6 // Humidity
#define DLENGTH 8 // Dust Level
#define TIMELENGTH 4 // Time
#define SLENGTH 2 // State
#define RPMLENGTH 5 // RPM
// PRECISION of the different FIELDS (change length if you change precision!):
#define TPREC 2 // Temperature
#define HPREC 2 // Humidity
#define VPREC 4 // Voltage
#define DPREC 2 // Dust (unused)
SerialCommand SCmd;
//FUNCTIONS:
void StrAppend(char* dest, char* source, int length, boolean strEnd); // Put string source at the end of string dest.
int readCmd(void);
void command(void);
void EEPROM_write_int(int addr, int val);
int EEPROM_read_int(int addr);
void SET_PWM(void);
void SET_MES(void);
void SET_RTO(void);
void SET_WTT(void);
void SET_RMN(void);
void SET_RMX(void);
void SET_DEF(void);
void GET_PWM(void);
void GET_MES(void);
void GET_RTO(void);
void GET_WTT(void);
void GET_RMN(void);
void GET_RMX(void);
void GET_ALL(void);
void GET_RPM(void);
void EEP_BRN(void);
void EEP_RST(void);
void EEP_PRT(void);
void RUN(void);
void STOP(void);
void HELP(void);
void unrecognized(void);
// COMMON:
unsigned int index=0; // Index of StrAppend() function to locate the position in the dest. string.
char s[16];
char dataStr[TLENGTH+HLENGTH+DLENGTH+DLENGTH+DLENGTH+TIMELENGTH+SLENGTH+RPMLENGTH+10];
unsigned int nMeasConf = NMEAS;
unsigned int nMeasure = nMeasConf-1; // Used to update humidity/temperature every NMEAS dust measures
unsigned int waitTime = WAIT_TIME;
bool bRun = true;
bool bErr = false;
// DUST INITIALIZATION PART:
float avgDust, medDust, stdevDust;
// HUMIDITY/TEMPERATURE INITIALIZATION PART:
float fTemp, fHumi; // Final values in Celsius degrees (temp) and in percent (humi)
float fTempPrev, fHumiPrev; // Save of the previous values
unsigned long deltaTime; // Duration of the humidity/temperature measure in micros
unsigned int hih_state; // HIH sensor state: 0 -> Ok, 1 -> Stale values
// FAN INITIALIZATION PART
unsigned int rpm;
unsigned int rpmTimeout = RPM_TIMEOUT;
unsigned int fanSpeed = FAN_SPEED;
unsigned int rpmMax = RPMMIN;
unsigned int rpmMin = RPMMAX;
//SETUP:
void setup(){
Serial.begin(9600);
initDust();
initI2C();
initFan();
initLEDs();
controlLED14(true);
controlLED15(true);
rpmMin = EEPROM_read_int(RPMMINADDR);
rpmMax = EEPROM_read_int(RPMMAXADDR);
SCmd.addCommand("SET_PWM",SET_PWM);
SCmd.addCommand("SET_MES",SET_MES);
SCmd.addCommand("SET_RTO",SET_RTO);
SCmd.addCommand("SET_WTT",SET_WTT);
SCmd.addCommand("SET_RMN",SET_RMN);
SCmd.addCommand("SET_RMX",SET_RMX);
SCmd.addCommand("SET_DEF",SET_DEF);
SCmd.addCommand("GET_PWM",GET_PWM);
SCmd.addCommand("GET_MES",GET_MES);
SCmd.addCommand("GET_RTO",GET_RTO);
SCmd.addCommand("GET_WTT",GET_WTT);
SCmd.addCommand("GET_RMN",GET_RMN);
SCmd.addCommand("GET_RMX",GET_RMX);
SCmd.addCommand("GET_ALL",GET_ALL);
SCmd.addCommand("GET_RPM",GET_RPM);
SCmd.addCommand("EEP_BRN",EEP_BRN);
SCmd.addCommand("EEP_RST",EEP_RST);
SCmd.addCommand("EEP_PRT",EEP_PRT);
SCmd.addCommand("RUN",RUN);
SCmd.addCommand("STOP",STOP);
SCmd.addCommand("HELP",HELP);
SCmd.addDefaultHandler(unrecognized);
EEP_RST();
delay(1000);
controlLED14(false);
controlLED15(false);
}
//LOOP:
void loop(){