Francis CHATAIN / Mbed 2 deprecated MSNV2-Terminal_V1-6

Dependencies:   DmTftLibrary eeprom SX1280Lib filesystem mbed

Fork of MSNV2-Terminal_V1-5 by Francis CHATAIN

COMPONENTS/generic_component.hpp

Committer:
lo
Date:
2018-10-03
Revision:
31:2b8b98f3feed
Parent:
26:271d2d510f6c
Child:
32:3bef9b81f639

File content as of revision 31:2b8b98f3feed:

/*
 * generic_component.hpp
 *
 *  Created on: 18 sept. 2018
 *      Author: hoel
 */

#ifndef COMPONENTS_GENERIC_COMPONENT_HPP_
#define COMPONENTS_GENERIC_COMPONENT_HPP_

#include <iostream>
#include <cstdlib>
#include "mbed.h"
#include "Context.h"
#include "Service.hpp"
#include "Component.hpp"

using namespace std;
using namespace misnet;

typedef union{
	float val;
	char bytes[4];
} FLOATUNION_t;

class generic_component : public Component{

	public:
	generic_component(COMPONENT_ID id, vector<Service*>& services,PinName sda, PinName scl, char slave_adr):

	    i2c_p(new I2C(sda, scl)),
	    i2c(*i2c_p),
	    address(slave_adr)
	{
		i2c.frequency(100000);
	}
	~generic_component();
	void init(void){

	}
	
	void readValues(void){
		for (std::vector<misnet::Service*>::iterator
				srvIt = this->getServices().begin();
				srvIt != this->getServices().end();
				srvIt++) {
			(*srvIt)->readValue();
		}
	}	
	
	InterruptIn interrupt(D9);

	private:
    I2C         *i2c_p;
    I2C         &i2c;
    char        address;

    class generic_value : public Service{

		public:
    	generic_value(
				DEVICE_TYPE type,
				MISNET_CODE misnet_code,
				STATE state,
				ACCESS_TYPE access_type,
				REQUEST_MODE request_mode,
				UP_MODE up_mode,
				ACCESS_PIN access_pins[6],
				uint32_t subsample_rate,
				ACTION action,
				OUTPUT_MODE output_mode,
				string comment,
				generic_component* parent
				)
		{
			this->parent = parent;
			this->setDeviceType(type);
			this->setMisnetCode(misnet_code);
			this->setState(state);
			this->setAccessType(access_type);
			this->setRequestMode(request_mode);
			this->setUpMode(up_mode);
			this->setAction(action);
			this->setOutputMode(output_mode);
			this->setComment(comment);
			this->setSubsampleRate(subsample_rate);
		}
    	virtual ~generic_value() {}
    	Value readValue(void){

        	misnet::Value val;
        	FLOATUNION_t flt;
        	char bfr[8];

        	bfr[0] = this->getMisnetCode(); 	// service code
        	bfr[1] = 0x01; 						// request value command
            i2c.write(address, bfr, 2, false);
            i2c.read(address, &flt.bytes[0], 4);
            val.setFloatValue( flt.val);
            DEBUG("received from I2C : %3f - 0x%02x 0x%02x 0x%02x 0x%02x\n",flt.val, flt.bytes[0],flt.bytes[1],flt.bytes[2],flt.bytes[3]);
        	return val;
        }

        bool setIrqParams(Service* service, Value threshold, Value up, Value down ){

        	char bfr[16];
        	misnet::Value val;
        	FLOATUNION_t flt;

        	bfr[0] = service->getMisnetCode(); 	// service code
        	bfr[1] = 0x02; 						// threshold cmd
        	i2c.write(address, bfr, 2, false);
        	flt.val = threshold.getFloatValue();
        	i2c.write(address, flt.bytes, 4, false);
            bfr[0] = 0x03; 						// value up cmd
            i2c.write(address, bfr, 1, false);
            i2c.write(address, flt.bytes, 4, false);
            bfr[0] = 0x04; 						// value down cmd
            i2c.write(address, bfr, 1, false);
            i2c.write(address, flt.bytes, 4, true);
            //i2c.read(address, bfr[0], 1); 	// ack
            //if (bfr[0] = 0x11) return 0
            //else return 1;
            return 0;
        }

        void setIrq(Callback <void()> cbck, bool enable){

        	//parent->interrupt.rise(cbck);
        	//if (enable) parent->interrupt.enable_irq();
        	//else parent->interrupt.disable_irq();
        }
		private:
        generic_component *parent;

    };
};

#endif /* COMPONENTS_GENERIC_COMPONENT_HPP_ */