Skip to content

Commit

Permalink
add gpio device
Browse files Browse the repository at this point in the history
works with z21
  • Loading branch information
danie1kr committed Jan 27, 2022
1 parent defc07b commit 2fc2e24
Show file tree
Hide file tree
Showing 23 changed files with 299 additions and 130 deletions.
14 changes: 13 additions & 1 deletion libwinston/Winston.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,20 @@
namespace winston
{
Logger logger;
RuntimeHardwareState runtimeHardwareState;

GPIOPinDevice::GPIOPinDevice(const Pin pin)
: Shared_Ptr<GPIOPinDevice>(), pin(pin)
{ }

GPIODigitalPinOutputDevice::GPIODigitalPinOutputDevice(const Pin pin, const State initial)
: GPIOPinDevice(pin), Shared_Ptr<GPIODigitalPinOutputDevice>()
{ }

GPIODigitalPinInputDevice::GPIODigitalPinInputDevice(const Pin pin, const Mode direction)
: GPIOPinDevice(pin), Shared_Ptr<GPIODigitalPinInputDevice>()
{ }

RuntimeHardwareState runtimeHardwareState;
RuntimeHardwareState::RuntimeHardwareState() : state(0) { };

void logRuntimeStatus()
Expand Down
1 change: 1 addition & 0 deletions libwinston/WinstonConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define WINSTON_STATISTICS
#define WINSTON_STATISTICS_SECONDS_PER_PRINT (5)

//#define WINSTON_REALWORLD
#ifndef WINSTON_REALWORLD
#define WINSTON_RAILWAY_DEBUG_INJECTOR
#endif
Expand Down
50 changes: 50 additions & 0 deletions libwinston/WinstonTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,56 @@ namespace winston
protected:
bool skip;
};

class GPIOPinDevice : public Shared_Ptr<GPIOPinDevice>
{
public:
enum class State : unsigned char
{
Low = 0,
High = 255
};

using Pin = unsigned char;

GPIOPinDevice(const Pin pin);
protected:
const Pin pin;
};

class GPIODigitalPinOutputDevice : public GPIOPinDevice, public Shared_Ptr<GPIODigitalPinOutputDevice>
{
public:
GPIODigitalPinOutputDevice(const Pin pin, const State initial = State::Low);
virtual void set(const State value) = 0;

using Shared_Ptr<GPIODigitalPinOutputDevice>::Shared;
using Shared_Ptr<GPIODigitalPinOutputDevice>::make;
};

class GPIODigitalPinInputDevice : public GPIOPinDevice, public Shared_Ptr<GPIODigitalPinInputDevice>
{
public:
enum class Mode : unsigned char
{
Input,
InputPullup
};

GPIODigitalPinInputDevice(const Pin pin, const Mode mode = Mode::Input);
virtual const State read() = 0;

using Shared_Ptr<GPIODigitalPinInputDevice>::Shared;
using Shared_Ptr<GPIODigitalPinInputDevice>::make;
};

class GPIODevice : public Shared_Ptr<GPIODevice>
{
public:
virtual GPIODigitalPinInputDevice::Shared getInputPinDevice(GPIODigitalPinInputDevice::Pin pin, GPIODigitalPinInputDevice::Mode) = 0;
virtual GPIODigitalPinOutputDevice::Shared getOutputPinDevice(GPIODigitalPinOutputDevice::Pin pin) = 0;
};

class RuntimeHardwareState
{
private:
Expand Down
1 change: 0 additions & 1 deletion winston-teensy/__vm/.winston-teensy.vsarduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#define __HARDWARE_imxrt1062__
#define __HARDWARE_IMXRT1062__
#define _VMDEBUG 1
#define BR_USE_UNIX_TIME 0
#define TCB_SPAN_NAMESPACE_NAME std
#define WINSTON_PLATFORM_TEENSY
#define TL_NAMESPACE std
Expand Down
4 changes: 2 additions & 2 deletions winston-teensy/__vm/Compile.vmps.xml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions winston-teensy/__vm/Upload.vmps.xml

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions winston-teensy/winston-hal-teensy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,18 @@ const winston::Result Arduino_SPIDevice::send(const std::span<DataType> data)
return winston::Result::OK;
}

Arduino_GPIOOutputPin::Arduino_GPIOOutputPin(const Pin pin, const State initial)
: winston::GPIODigitalPinOutputDevice(pin, initial), winston::Shared_Ptr<Arduino_GPIOOutputPin>()
{
pinMode(pin, OUTPUT);
digitalWriteFast(pin, initial == State::Low ? LOW : HIGH);
}

void Arduino_GPIOOutputPin::Arduino_GPIOOutputPin::set(const State value)
{
digitalWriteFast(pin, value == State::Low ? LOW : HIGH);
}

void teensyMAC(uint8_t* mac) { // there are 2 MAC addresses each 48bit
uint32_t m1 = HW_OCOTP_MAC1;
uint32_t m2 = HW_OCOTP_MAC0;
Expand Down
12 changes: 11 additions & 1 deletion winston-teensy/winston-hal-teensy.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,18 @@ class Arduino_SPIDevice : public winston::hal::SPIDevice<unsigned char>, public
private:
SPISettings spiSettings;
};
using SignalInterfaceDevice = Arduino_SPIDevice;

class Arduino_GPIOOutputPin : public winston::GPIODigitalPinOutputDevice, public winston::Shared_Ptr<Arduino_GPIOOutputPin>
{
public:
Arduino_GPIOOutputPin(const Pin pin, const State initial = State::Low);
void set(const State value);

using winston::Shared_Ptr<Arduino_GPIOOutputPin>::Shared;
using winston::Shared_Ptr<Arduino_GPIOOutputPin>::make;
};

using SignalSPIDevice = Arduino_SPIDevice;
/*
#ifdef WINSTON_WITH_WEBSOCKET
class WebServerTeensy : public winston::WebServer<WebsocketsClient>
Expand Down
4 changes: 2 additions & 2 deletions winston-teensy/winston-teensy.vcxproj

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions winston-tests/winston-tests-signaldevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace winstontests
{
template<typename T>
class Test_SendDevice : public winston::SendDevice<T>, public winston::Shared_Ptr<Test_SendDevice<T>>
class Test_SendDevice : public winston::SendDevice<T>, public winston::GPIODigitalPinOutputDevice, public winston::Shared_Ptr<Test_SendDevice<T>>
{
public:
Test_SendDevice()
: GPIODigitalPinOutputDevice(0) // we don't care
{

}

const winston::Result send(const std::span<T> data)
{
this->_data = data;
Expand All @@ -19,6 +25,8 @@ namespace winstontests

const std::span<T> data() { return this->_data; };

void set(const State value) { }; // we don't care

std::span<T> _data;

using winston::Shared_Ptr<Test_SendDevice<T>>::Shared;
Expand All @@ -31,12 +39,12 @@ namespace winstontests
TEST_METHOD(DevInit)
{
Test_SendDevice<unsigned char>::Shared sendDevice = Test_SendDevice<unsigned char>::make();
TLC5947_SignalDevice::Shared signalDevice = TLC5947_SignalDevice::make(1, 24, sendDevice);
TLC5947_SignalDevice::Shared signalDevice = TLC5947_SignalDevice::make(1, 24, sendDevice, sendDevice);
}
TEST_METHOD(SignalUpdatePort_0_0)
{
Test_SendDevice<unsigned char>::Shared sendDevice = Test_SendDevice<unsigned char>::make();
TLC5947_SignalDevice::Shared signalDevice = TLC5947_SignalDevice::make(1, 24, sendDevice);
TLC5947_SignalDevice::Shared signalDevice = TLC5947_SignalDevice::make(1, 24, sendDevice, sendDevice);

winston::Port port(0, 0);
winston::Signal::Shared signal = winston::SignalKS::make([](const winston::Signal::Aspects aspect) -> const winston::State { return winston::State::Finished; }, 0, port);
Expand Down
25 changes: 13 additions & 12 deletions winston-tests/winston-tests-spi-tlc5947-signal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "..\libwinston\Winston.h"

#include "..\winston-hal-x64.h"
#include "..\winston\FT232_SPIDevice.h"
#include "..\winston\FT232_Device.h"
#include "..\winston\TLC5947_SignalDevice.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;
Expand All @@ -12,16 +12,17 @@ namespace winstontests
{
TEST_CLASS(TLC5947SPI)
{
SignalSPIDevice::Shared signalSPIDevice;
SignalInterfaceDevice::Shared signalInterfaceDevice;
TLC5947_SignalDevice::Shared signalDevice;

public:

TEST_METHOD_INITIALIZE(init)
{
this->signalSPIDevice = SignalSPIDevice::make(3, 5000000);
this->signalSPIDevice->init();
this->signalDevice = TLC5947_SignalDevice::make(1, 24, this->signalSPIDevice);
this->signalInterfaceDevice = SignalInterfaceDevice::make(3, 5000000);
auto TLC5947Off = this->signalInterfaceDevice->getOutputPinDevice(4);
this->signalInterfaceDevice->init();
this->signalDevice = TLC5947_SignalDevice::make(1, 24, this->signalInterfaceDevice, TLC5947Off);
}

TEST_METHOD_CLEANUP(cleanup)
Expand All @@ -36,14 +37,14 @@ namespace winstontests
// all off
{
std::vector<unsigned char> data((1 * 24 * 12 / 8) / sizeof(unsigned char), 0);
this->signalSPIDevice->send(data);
this->signalInterfaceDevice->send(data);
winston::hal::delay(200);
}

// all on
{
std::vector<unsigned char> data((1 * 24 * 12 / 8) / sizeof(unsigned char), 0xFF);
this->signalSPIDevice->send(data);
this->signalInterfaceDevice->send(data);
winston::hal::delay(200);
}

Expand All @@ -52,7 +53,7 @@ namespace winstontests
std::vector<unsigned char> data((1 * 24 * 12 / 8) / sizeof(unsigned char), 0);
data[data.size() - 1] = 0xFF;
data[data.size() - 2] = 0x0F;
this->signalSPIDevice->send(data);
this->signalInterfaceDevice->send(data);
winston::hal::delay(200);
}

Expand All @@ -64,7 +65,7 @@ namespace winstontests
data[data.size() - 3] = 0x00;
data[data.size() - 4] = 0xFF;
data[data.size() - 5] = 0x0F;
this->signalSPIDevice->send(data);
this->signalInterfaceDevice->send(data);
winston::hal::delay(200);
}

Expand All @@ -82,7 +83,7 @@ namespace winstontests
[] 5 4 3 2 1
0F FF 7F F0 00
*/
this->signalSPIDevice->send(data);
this->signalInterfaceDevice->send(data);
winston::hal::delay(200);
}

Expand All @@ -94,7 +95,7 @@ namespace winstontests
data[data.size() - 3] = 0x80;
data[data.size() - 4] = 0x08;
data[data.size() - 5] = 0x00;
this->signalSPIDevice->send(data);
this->signalInterfaceDevice->send(data);
winston::hal::delay(200);
}
}
Expand All @@ -107,7 +108,7 @@ namespace winstontests
// all off
{
std::vector<unsigned char> data((1 * 24 * 12 / 8) / sizeof(unsigned char), 0);
this->signalSPIDevice->send(data);
this->signalInterfaceDevice->send(data);
winston::hal::delay(200);
}

Expand Down
2 changes: 1 addition & 1 deletion winston-tests/winston-tests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\winston\FT232_SPIDevice.cpp" />
<ClCompile Include="..\winston\FT232_Device.cpp" />
<ClCompile Include="..\winston\railways.cpp" />
<ClCompile Include="..\winston\winston-hal-x64.cpp" />
<ClCompile Include="winston-tests-mini.cpp" />
Expand Down
2 changes: 1 addition & 1 deletion winston-tests/winston-tests.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<ClCompile Include="winston-tests-spi-tlc5947-signal.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\winston\FT232_SPIDevice.cpp">
<ClCompile Include="..\winston\FT232_Device.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
Expand Down
Loading

0 comments on commit 2fc2e24

Please sign in to comment.