gearmulator

Emulation of classic VA synths of the late 90s/2000s that are based on Motorola 56300 family DSPs
Log | Files | Refs | Submodules | README | LICENSE

i2c.h (1176B)


      1 #pragma once
      2 
      3 #include <cstdint>
      4 #include <optional>
      5 
      6 namespace hwLib
      7 {
      8 	class I2c
      9 	{
     10 	public:
     11 		enum class State
     12 		{
     13 			Stop,
     14 			Start,
     15 		};
     16 		enum BitPos : int8_t
     17 		{
     18 			Bit7 = 7,
     19 			Bit6 = 6,
     20 			Bit5 = 5,
     21 			Bit4 = 4,
     22 			Bit3 = 3,
     23 			Bit2 = 2,
     24 			Bit1 = 1,
     25 			Bit0 = 0,
     26 			BitAck = -1,
     27 			BitInvalid = -2,
     28 		};
     29 
     30 		I2c() = default;
     31 
     32 		virtual ~I2c() = default;
     33 
     34 		void masterWrite(bool _sda, bool _scl);
     35 		virtual std::optional<bool> masterRead(bool _scl);
     36 		std::optional<bool> setSdaWrite(bool _write);
     37 
     38 		bool sda() const { return m_sda; }
     39 		bool scl() const { return m_scl; }
     40 		uint8_t byte() const { return m_byte; }
     41 
     42 	protected:
     43 		virtual void onStateChanged(State _state);
     44 		virtual void onStartCondition();
     45 		virtual void onStopCondition();
     46 		virtual void onByteWritten();
     47 		virtual std::optional<bool> onAck() { return {}; }
     48 		virtual uint8_t onReadByte() { return 0; }
     49 
     50 	private:
     51 		void sdaFlip(bool _sda);
     52 		void sclFlip(bool _scl);
     53 		void setState(State _state);
     54 
     55 		bool m_sdaWrite = true;	// true = write
     56 		bool m_sda = false;
     57 		bool m_scl = false;
     58 		State m_state = State::Stop;
     59 		int8_t m_nextBit = BitInvalid;
     60 		uint8_t m_byte;
     61 		bool m_ackBit = true;
     62 	};
     63 }