Aleksandrs Gumenuks / MaximInterface_Extended

Dependents:   mbed_DS28EC20_GPIO

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RunCommand.hpp Source File

RunCommand.hpp

00001 /*******************************************************************************
00002 * Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 *******************************************************************************/
00032 
00033 #ifndef MaximInterface_RunCommand
00034 #define MaximInterface_RunCommand
00035 
00036 #include <MaximInterface/Utilities/Export.h>
00037 #include <MaximInterface/Utilities/Function.hpp>
00038 #include <MaximInterface/Utilities/span.hpp>
00039 #include <MaximInterface/Utilities/system_error.hpp>
00040 #include "SelectRom.hpp"
00041 
00042 namespace MaximInterface {
00043 
00044 class I2CMaster;
00045 class OneWireMaster;
00046 class Sleep;
00047 
00048 typedef Function<error_code(span<const uint_least8_t>, int,
00049                             span<uint_least8_t> &)>
00050     RunCommand;
00051 
00052 class RunCommandWithOneWireMaster {
00053 public:
00054   typedef RunCommand::result_type result_type;
00055 
00056   enum ErrorValue { CrcError = 1, InvalidResponseError };
00057   MaximInterface_EXPORT static const error_category & errorCategory();
00058 
00059   RunCommandWithOneWireMaster(Sleep & sleep, OneWireMaster & master,
00060                               const SelectRom & selectRom)
00061       : selectRom(selectRom), master(&master), sleep(&sleep) {}
00062 
00063   void setSleep(Sleep & sleep) { this->sleep = &sleep; }
00064   
00065   void setMaster(OneWireMaster & master) { this->master = &master; }
00066   
00067   void setSelectRom(const SelectRom & selectRom) {
00068     this->selectRom = selectRom;
00069   }
00070 
00071   MaximInterface_EXPORT error_code
00072   operator()(span<const uint_least8_t> command, int delayTime,
00073              span<uint_least8_t> & response) const;
00074 
00075 private:
00076   SelectRom selectRom;
00077   OneWireMaster * master;
00078   const Sleep * sleep;
00079 };
00080 
00081 inline error_code make_error_code(RunCommandWithOneWireMaster::ErrorValue e) {
00082   return error_code(e, RunCommandWithOneWireMaster::errorCategory());
00083 }
00084 
00085 class RunCommandWithI2CMaster {
00086 public:
00087   typedef RunCommand::result_type result_type;
00088 
00089   enum ErrorValue { InvalidResponseError = 1 };
00090   MaximInterface_EXPORT static const error_category & errorCategory();
00091 
00092   RunCommandWithI2CMaster(Sleep & sleep, I2CMaster & master,
00093                           uint_least8_t address)
00094       : sleep(&sleep), master(&master), address_(address & 0xFE) {}
00095 
00096   void setSleep(Sleep & sleep) { this->sleep = &sleep; }
00097   
00098   void setMaster(I2CMaster & master) { this->master = &master; }
00099   
00100   uint_least8_t address() const { return address_; }
00101   
00102   void setAddress(uint_least8_t address) { address_ = address & 0xFE; }
00103 
00104   MaximInterface_EXPORT error_code
00105   operator()(span<const uint_least8_t> command, int delayTime,
00106              span<uint_least8_t> & response) const;
00107 
00108 private:
00109   const Sleep * sleep;
00110   I2CMaster * master;
00111   uint_least8_t address_;
00112 };
00113 
00114 inline error_code make_error_code(RunCommandWithI2CMaster::ErrorValue e) {
00115   return error_code(e, RunCommandWithI2CMaster::errorCategory());
00116 }
00117 
00118 } // namespace MaximInterface
00119 
00120 #endif