inital commit
This commit is contained in:
18
src/items/auxitem.cpp
Normal file
18
src/items/auxitem.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
#include "auxitem.h"
|
||||
|
||||
AuxItem::AuxItem(Microcontroller* micro, uint32_t itemIdIn, QString name, uint8_t value, QObject* parent): Item(itemIdIn, name, value, parent), micro_(micro)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void AuxItem::setValue(uint8_t value)
|
||||
{
|
||||
Item::setValue(value);
|
||||
micro_->setAuxPwm(value);
|
||||
}
|
||||
|
||||
void AuxItem::store(QJsonObject &json)
|
||||
{
|
||||
json["Type"] = "Aux";
|
||||
Item::store(json);
|
||||
}
|
20
src/items/auxitem.h
Normal file
20
src/items/auxitem.h
Normal file
@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "item.h"
|
||||
#include "../microcontroller.h"
|
||||
|
||||
class AuxItem: public Item
|
||||
{
|
||||
Q_OBJECT
|
||||
private:
|
||||
Microcontroller* micro_;
|
||||
|
||||
public slots:
|
||||
|
||||
virtual void setValue(uint8_t value);
|
||||
|
||||
public:
|
||||
AuxItem(Microcontroller* micro, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", uint8_t value = 0, QObject* parent = nullptr);
|
||||
|
||||
virtual void store(QJsonObject& json);
|
||||
};
|
62
src/items/item.cpp
Normal file
62
src/items/item.cpp
Normal file
@ -0,0 +1,62 @@
|
||||
#include "item.h"
|
||||
#include "../microcontroller.h"
|
||||
|
||||
#include <QJsonArray>
|
||||
|
||||
ItemData::ItemData(uint32_t itemIdIn, QString name, uint8_t value): name_(name), value_(value), itemId_(itemIdIn)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QString ItemData::getName() const
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
|
||||
void ItemData::setName(QString name)
|
||||
{
|
||||
name_ = name;
|
||||
}
|
||||
|
||||
uint8_t ItemData::getValue() const
|
||||
{
|
||||
return value_;
|
||||
}
|
||||
|
||||
uint32_t ItemData::id() const
|
||||
{
|
||||
return itemId_;
|
||||
}
|
||||
|
||||
|
||||
//item
|
||||
|
||||
Item::Item(uint32_t itemIdIn, QString name, uint8_t value, QObject *parent): QObject(parent), ItemData (itemIdIn, name, value)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Item::Item(const ItemData& itemData, QObject *parent): QObject(parent), ItemData(itemData)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Item::~Item()
|
||||
{
|
||||
}
|
||||
|
||||
void Item::setFunction(uint8_t function, bool on)
|
||||
{
|
||||
functionChanged(function, on);
|
||||
}
|
||||
|
||||
void Item::setValue(uint8_t value)
|
||||
{
|
||||
value_ = value;
|
||||
valueChanged(value_);
|
||||
}
|
||||
|
||||
void Item::informValue(uint8_t value)
|
||||
{
|
||||
Item::setValue(value);
|
||||
}
|
58
src/items/item.h
Normal file
58
src/items/item.h
Normal file
@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
#include <QObject>
|
||||
#include <vector>
|
||||
#include <QRandomGenerator>
|
||||
#include <QSettings>
|
||||
#include <QJsonObject>
|
||||
#include <memory>
|
||||
|
||||
class Actor;
|
||||
|
||||
class ItemData
|
||||
{
|
||||
protected:
|
||||
QString name_;
|
||||
uint8_t value_;
|
||||
uint32_t itemId_;
|
||||
|
||||
public:
|
||||
|
||||
ItemData(uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "Item", uint8_t value = 0);
|
||||
|
||||
inline bool operator==(const ItemData& in) const{ return itemId_==in.itemId_; }
|
||||
inline bool operator!=(const ItemData& in) const{ return itemId_!=in.itemId_; }
|
||||
|
||||
uint32_t id() const;
|
||||
|
||||
void setName(QString name);
|
||||
uint8_t getValue() const;
|
||||
virtual QString getName() const;
|
||||
};
|
||||
|
||||
|
||||
class Item: public QObject, public ItemData
|
||||
{
|
||||
Q_OBJECT
|
||||
private:
|
||||
|
||||
signals:
|
||||
|
||||
void valueChanged(uint8_t value);
|
||||
void functionChanged(uint8_t function, bool on);
|
||||
|
||||
public slots:
|
||||
|
||||
virtual void setValue(uint8_t value);
|
||||
virtual void setFunction(uint8_t funciton, bool value);
|
||||
|
||||
public:
|
||||
|
||||
Item(uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "Item", uint8_t value = 0, QObject *parent = nullptr);
|
||||
Item(const ItemData& itemData, QObject *parent = nullptr);
|
||||
|
||||
virtual ~Item();
|
||||
|
||||
void informValue(uint8_t value);
|
||||
|
||||
};
|
||||
|
59
src/items/itemstore.cpp
Normal file
59
src/items/itemstore.cpp
Normal file
@ -0,0 +1,59 @@
|
||||
#include "itemstore.h"
|
||||
#include <QJsonArray>
|
||||
|
||||
ItemStore::ItemStore(QObject *parent): QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void ItemStore::addItem(std::shared_ptr<Item> item)
|
||||
{
|
||||
bool mached = false;
|
||||
for(unsigned i = 0; i < items_.size(); i++ ) if(*items_[i] == *item) mached = true;
|
||||
if(!mached)
|
||||
{
|
||||
items_.push_back(std::shared_ptr<Item>(item));
|
||||
itemAdded(std::weak_ptr<Item>(items_.back()));
|
||||
}
|
||||
}
|
||||
|
||||
void ItemStore::addItems(const std::vector<std::shared_ptr<Item>>& itemIn)
|
||||
{
|
||||
for(unsigned j = 0; j < itemIn.size(); j++)
|
||||
addItem(itemIn[j]);
|
||||
|
||||
}
|
||||
|
||||
void ItemStore::removeItem(const ItemData& item)
|
||||
{
|
||||
for(unsigned j = 0; j < items_.size(); j++)
|
||||
{
|
||||
if(item == *items_[j])
|
||||
{
|
||||
items_.erase(items_.begin()+j);
|
||||
--j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ItemStore::clear()
|
||||
{
|
||||
for(size_t i = 0; i < items_.size(); ++i) itemDeleted(*items_[i]);
|
||||
items_.clear();
|
||||
}
|
||||
|
||||
|
||||
void ItemStore::itemStateChanged(const ItemData& item)
|
||||
{
|
||||
|
||||
for(unsigned i = 0; i < items_.size(); i++ )
|
||||
{
|
||||
if(items_[i]->operator==(item))
|
||||
{
|
||||
|
||||
if(items_[i]->getValue() != item.getValue())items_[i]->informValue(item.getValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
34
src/items/itemstore.h
Normal file
34
src/items/itemstore.h
Normal file
@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include "item.h"
|
||||
|
||||
#include <QJsonObject>
|
||||
|
||||
class ItemStore: public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
private:
|
||||
std::vector< std::shared_ptr<Item> > items_;
|
||||
|
||||
public:
|
||||
|
||||
ItemStore(QObject *parent = nullptr);
|
||||
virtual ~ItemStore(){}
|
||||
|
||||
inline std::vector< std::shared_ptr<Item> >* getItems(){ return &items_; }
|
||||
|
||||
void clear();
|
||||
|
||||
signals:
|
||||
|
||||
void itemDeleted(ItemData item);
|
||||
void itemAdded(std::weak_ptr<Item> Item);
|
||||
|
||||
public slots:
|
||||
|
||||
void removeItem(const ItemData& item);
|
||||
void addItem(std::shared_ptr<Item> item);
|
||||
void addItems(const std::vector<std::shared_ptr<Item>>& itemsIn);
|
||||
void itemStateChanged(const ItemData& item);
|
||||
};
|
32
src/items/train.cpp
Normal file
32
src/items/train.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
#include "train.h"
|
||||
|
||||
Train::Train(uint8_t id, uint8_t address, uint8_t functionMask):
|
||||
functionMask_(functionMask),
|
||||
train_id_(id)
|
||||
{
|
||||
itemId_ = address;
|
||||
name_ = QString("Train ")+QString::number(id);
|
||||
}
|
||||
|
||||
Microcontroller *Train::micro = nullptr;
|
||||
|
||||
void Train::setFunction(uint8_t funciton, bool value)
|
||||
{
|
||||
Item::setFunction(funciton, value);
|
||||
if(micro)
|
||||
micro->itemSetFunction(train_id_, funciton, value);
|
||||
}
|
||||
|
||||
void Train::setValue(uint8_t value)
|
||||
{
|
||||
Item::setValue(value);
|
||||
if(micro)
|
||||
micro->itemSetSpeed(train_id_, value);
|
||||
}
|
||||
|
||||
void Train::reverse()
|
||||
{
|
||||
if(micro)
|
||||
micro->itemReverse(train_id_);
|
||||
}
|
||||
|
25
src/items/train.h
Normal file
25
src/items/train.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef TRAIN_H
|
||||
#define TRAIN_H
|
||||
|
||||
#include "item.h"
|
||||
#include "../microcontroller.h"
|
||||
|
||||
class Train : public Item
|
||||
{
|
||||
Q_OBJECT
|
||||
uint8_t functionMask_;
|
||||
uint8_t train_id_;
|
||||
public:
|
||||
static Microcontroller *micro;
|
||||
|
||||
Train(uint8_t id = 0, uint8_t address = 0, uint8_t functionMask = 0);
|
||||
|
||||
uint8_t getFunctionMask() {return functionMask_;}
|
||||
|
||||
public slots:
|
||||
void reverse();
|
||||
virtual void setFunction(uint8_t function, bool on);
|
||||
virtual void setValue(uint8_t value);
|
||||
};
|
||||
|
||||
#endif // TRAIN_H
|
Reference in New Issue
Block a user