diff --git a/src/dolp.cpp b/src/dolp.cpp index 2ccfb31..9759187 100644 --- a/src/dolp.cpp +++ b/src/dolp.cpp @@ -31,10 +31,10 @@ void Dolp::deleteEntity(PEntity entity) } // TODO -//void Dolp::clear() +// void Dolp::clear() //{ - //// Clear the world - //// Clear all the drivers' state: timeouts, notes... +//// Clear the world +//// Clear all the drivers' state: timeouts, notes... //} Dolp dolp; diff --git a/src/dolp.h b/src/dolp.h index 871816c..4a2349c 100644 --- a/src/dolp.h +++ b/src/dolp.h @@ -9,7 +9,8 @@ #define GAME_LOOP_BEGIN (dolp.loopBegin) #define GAME_LOOP_END (dolp.loopEnd) -class Dolp { +class Dolp +{ public: Dolp() {} virtual ~Dolp() {} diff --git a/src/drivers/Display.cpp b/src/drivers/Display.cpp index fa7e2e2..eb5b9f8 100644 --- a/src/drivers/Display.cpp +++ b/src/drivers/Display.cpp @@ -24,12 +24,12 @@ void Display::draw(uint8_t x, uint8_t y, DisplayColor color) hwDisplayDraw(x, y, color); } -void Display::drawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t* image) +void Display::drawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *image) { hwDisplayDrawImage(x, y, w, h, image); } -void Display::drawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16_t* image) +void Display::drawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16_t *image) { hwDisplayDrawImage(x, y, w, h, image); } diff --git a/src/drivers/Display.h b/src/drivers/Display.h index 562dd05..8d246e1 100644 --- a/src/drivers/Display.h +++ b/src/drivers/Display.h @@ -4,7 +4,8 @@ #include #include "hardware/HwDisplay.h" -class Display { +class Display +{ public: Display(); virtual ~Display(); @@ -14,8 +15,8 @@ class Display { void send(); void draw(uint8_t x, uint8_t y, DisplayColor color); - void drawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t* image); - void drawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16_t* image); + void drawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *image); + void drawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16_t *image); void drawRectangle(uint8_t x, uint8_t y, uint8_t width, uint8_t height, DisplayColor color); void fill(DisplayColor color); diff --git a/src/drivers/DriverLayer.h b/src/drivers/DriverLayer.h index 8fe6813..7de2ee1 100644 --- a/src/drivers/DriverLayer.h +++ b/src/drivers/DriverLayer.h @@ -11,7 +11,8 @@ extern Joystick joystick; extern NotePlayer notePlayer; extern Timer timer; -class DriverLayer { +class DriverLayer +{ public: DriverLayer(); virtual ~DriverLayer(); diff --git a/src/drivers/Joystick.cpp b/src/drivers/Joystick.cpp index f70a853..1922be7 100644 --- a/src/drivers/Joystick.cpp +++ b/src/drivers/Joystick.cpp @@ -1,13 +1,12 @@ #include #include -Joystick::Joystick() : - _buttonStateOld(0x00), - _buttonPressed(0x00) +Joystick::Joystick() : _buttonStateOld(0x00), + _buttonPressed(0x00) { } -Joystick::~Joystick() +Joystick::~Joystick() { } @@ -23,7 +22,7 @@ void Joystick::update() ButtonStateType buttonStateCurrent = hwJoystickState(); /* Check whether some button has been pressed */ _buttonPressed = (_buttonStateOld ^ buttonStateCurrent) & (buttonStateCurrent ^ 0x00); - /* Update old button values */ + /* Update old button values */ _buttonStateOld = buttonStateCurrent; } diff --git a/src/drivers/Joystick.h b/src/drivers/Joystick.h index 9586aca..2e64c98 100644 --- a/src/drivers/Joystick.h +++ b/src/drivers/Joystick.h @@ -4,7 +4,8 @@ #include #include "hardware/HwJoystick.h" -class Joystick { +class Joystick +{ public: Joystick(); virtual ~Joystick(); diff --git a/src/drivers/NotePlayer.cpp b/src/drivers/NotePlayer.cpp index 65834b7..e6c8d87 100644 --- a/src/drivers/NotePlayer.cpp +++ b/src/drivers/NotePlayer.cpp @@ -1,11 +1,11 @@ #include #include -NotePlayer::NotePlayer() +NotePlayer::NotePlayer() { } -NotePlayer::~NotePlayer() +NotePlayer::~NotePlayer() { } @@ -19,28 +19,28 @@ void NotePlayer::stop() hwNotePlayerStop(); } -void NotePlayer::play(Song &song) +void NotePlayer::play(Song &song) { hwNotePlayerStop(); hwNotePlayerSong(song.notes, song.size, 0, false); hwNotePlayerStart(); } -void NotePlayer::play(Song &song, uint16_t startIndex) +void NotePlayer::play(Song &song, uint16_t startIndex) { hwNotePlayerStop(); hwNotePlayerSong(song.notes, song.size, startIndex, false); hwNotePlayerStart(); } -void NotePlayer::play(Song &song, bool loopEn) +void NotePlayer::play(Song &song, bool loopEn) { hwNotePlayerStop(); hwNotePlayerSong(song.notes, song.size, 0, loopEn); hwNotePlayerStart(); } -void NotePlayer::play(Song &song, uint16_t startIndex, bool loopEn) +void NotePlayer::play(Song &song, uint16_t startIndex, bool loopEn) { hwNotePlayerStop(); hwNotePlayerSong(song.notes, song.size, startIndex, loopEn); diff --git a/src/drivers/NotePlayer.h b/src/drivers/NotePlayer.h index b2fdde1..c2ef7ea 100644 --- a/src/drivers/NotePlayer.h +++ b/src/drivers/NotePlayer.h @@ -4,7 +4,8 @@ #include #include "hardware/HwNotePlayer.h" -class NotePlayer { +class NotePlayer +{ public: NotePlayer(); virtual ~NotePlayer(); diff --git a/src/drivers/Timer.cpp b/src/drivers/Timer.cpp index 02cf05c..4ab33fc 100644 --- a/src/drivers/Timer.cpp +++ b/src/drivers/Timer.cpp @@ -1,17 +1,17 @@ #include #include -Timer::Timer() : - _tick(0), - _secTick(0), - _seconds(0) +Timer::Timer() : _tick(0), + _secTick(0), + _seconds(0) { - for (int i = 0; i < NUM_TIMEOUTS; i++) { + for (int i = 0; i < NUM_TIMEOUTS; i++) + { _timeouts[i].startTicks = 0; _timeouts[i].ticks = 0; _timeouts[i].assigned = false; _timeouts[i].reset = false; - } + } } Timer::~Timer() @@ -40,7 +40,8 @@ void Timer::waitEndFrame() // Update ticks and time counters ++_tick; _milliseconds += FRAME_MSEC; - if (++_secTick == FPS) { + if (++_secTick == FPS) + { _secTick = 0; ++_seconds; } @@ -51,8 +52,10 @@ void Timer::waitEndFrame() TimeoutId Timer::newTimeout() { // Timeout 0 is reserved - for (int i = 1; i < NUM_TIMEOUTS; i++) { - if (!_timeouts[i].assigned) { + for (int i = 1; i < NUM_TIMEOUTS; i++) + { + if (!_timeouts[i].assigned) + { _timeouts[i].assigned = true; _timeouts[i].reset = false; return i; @@ -64,8 +67,10 @@ TimeoutId Timer::newTimeout() TimeoutId Timer::newTimeout(uint64_t time) { // Timeout 0 is reserved - for (int i = 1; i < NUM_TIMEOUTS; i++) { - if (!_timeouts[i].assigned) { + for (int i = 1; i < NUM_TIMEOUTS; i++) + { + if (!_timeouts[i].assigned) + { _timeouts[i].startTicks = time / FRAME_MSEC + 1; _timeouts[i].ticks = _timeouts[i].startTicks; _timeouts[i].assigned = true; @@ -78,7 +83,8 @@ TimeoutId Timer::newTimeout(uint64_t time) void Timer::deleteTimeout(TimeoutId id) { - if (_timeouts[id].assigned) { + if (_timeouts[id].assigned) + { _timeouts[id].ticks = 0; _timeouts[id].startTicks = 0; _timeouts[id].assigned = false; @@ -88,13 +94,17 @@ void Timer::deleteTimeout(TimeoutId id) void Timer::updateTimeout() { - for (int i = 1; i < NUM_TIMEOUTS; i++) { - if (_timeouts[i].assigned) { + for (int i = 1; i < NUM_TIMEOUTS; i++) + { + if (_timeouts[i].assigned) + { - if (_timeouts[i].ticks > 1) { + if (_timeouts[i].ticks > 1) + { _timeouts[i].ticks -= 1; - - } else if ( (_timeouts[i].ticks == 1) && _timeouts[i].reset) { + } + else if ((_timeouts[i].ticks == 1) && _timeouts[i].reset) + { _timeouts[i].ticks = _timeouts[i].startTicks; _timeouts[i].reset = false; } @@ -104,7 +114,8 @@ void Timer::updateTimeout() void Timer::setTimeout(TimeoutId id, uint64_t time) { - if (_timeouts[id].assigned) { + if (_timeouts[id].assigned) + { _timeouts[id].startTicks = time / FRAME_MSEC + 1; _timeouts[id].ticks = _timeouts[id].startTicks; _timeouts[id].reset = false; @@ -114,7 +125,8 @@ void Timer::setTimeout(TimeoutId id, uint64_t time) bool Timer::checkTimeout(TimeoutId id) { bool ret = (_timeouts[id].ticks == 1); - if (ret) { + if (ret) + { _timeouts[id].reset = true; } return ret; diff --git a/src/drivers/Timer.h b/src/drivers/Timer.h index 7b4490b..cf2e8e4 100644 --- a/src/drivers/Timer.h +++ b/src/drivers/Timer.h @@ -7,7 +7,8 @@ #define NUM_TIMEOUTS 200 #endif -struct Timeout { +struct Timeout +{ // Timeout id corresponds to its index in _timeouts array // Id 0 is reserved uint8_t startTicks; @@ -18,7 +19,8 @@ struct Timeout { typedef uint8_t TimeoutId; -class Timer { +class Timer +{ public: Timer(); virtual ~Timer(); diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index 990e2e3..fb3cc41 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -2,8 +2,7 @@ #include "drivers/DriverLayer.h" -Engine::Engine() : - _world(NULL) +Engine::Engine() : _world(NULL) { } @@ -18,7 +17,8 @@ void Engine::setup() void Engine::loopBegin() { // Update world - if (_world) { + if (_world) + { _world->update(); } } @@ -26,15 +26,16 @@ void Engine::loopBegin() void Engine::loopEnd() { // Draw world - //clearDisplay(); + // clearDisplay(); display.fill(BLACK_COLOR); - if (_world) { + if (_world) + { _world->render(); } } // setActiveWorld -void Engine::setWorld(World& world) +void Engine::setWorld(World &world) { _world = &world; } diff --git a/src/engine/Engine.h b/src/engine/Engine.h index 75833e0..f661475 100644 --- a/src/engine/Engine.h +++ b/src/engine/Engine.h @@ -6,19 +6,20 @@ #include "Entity.h" #include "World.h" -class Engine { - public: - Engine(); - ~Engine(); +class Engine +{ +public: + Engine(); + ~Engine(); - void setup(); - void loopBegin(); - void loopEnd(); + void setup(); + void loopBegin(); + void loopEnd(); - void setWorld(World& world); + void setWorld(World &world); - private: - World* _world; +private: + World *_world; }; extern Engine engine; diff --git a/src/engine/Entity.cpp b/src/engine/Entity.cpp index 7c891f6..d86fa94 100644 --- a/src/engine/Entity.cpp +++ b/src/engine/Entity.cpp @@ -4,14 +4,13 @@ int Entity::_classId = 0; -Entity::Entity() : - _world(NULL), - _bodyComponent(NULL), - _renderComponent(NULL), - _movementComponent(NULL), - _updateComponent(NULL), - _stateComponent(NULL), - _collisionComponent(NULL) +Entity::Entity() : _world(NULL), + _bodyComponent(NULL), + _renderComponent(NULL), + _movementComponent(NULL), + _updateComponent(NULL), + _stateComponent(NULL), + _collisionComponent(NULL) { } @@ -19,7 +18,7 @@ Entity::~Entity() { } -void Entity::init(World* world) +void Entity::init(World *world) { // Set Entity ID ++_classId; @@ -29,7 +28,7 @@ void Entity::init(World* world) // Init components // At the moment this order is mandatory, because of how components are injected // (here body component is injected into render component at its initialization) - addBodyComponent(); + addBodyComponent(); addRenderComponent(); _movementComponent = NULL; _updateComponent = NULL; @@ -41,12 +40,18 @@ void Entity::deinit() { // _world = NULL //useless. entities belong to only one world // Delete components - if (_renderComponent) removeRenderComponent(); - if (_bodyComponent) removeBodyComponent(); - if (_movementComponent) removeMovementComponent(); - if (_updateComponent) removeUpdateComponent(); - if (_stateComponent) removeStateComponent(); - if (_collisionComponent) removeCollisionComponent(); + if (_renderComponent) + removeRenderComponent(); + if (_bodyComponent) + removeBodyComponent(); + if (_movementComponent) + removeMovementComponent(); + if (_updateComponent) + removeUpdateComponent(); + if (_stateComponent) + removeStateComponent(); + if (_collisionComponent) + removeCollisionComponent(); } void Entity::addBodyComponent() @@ -93,7 +98,7 @@ void Entity::addTimeComponent() void Entity::removeBodyComponent() { - //if (_bodyComponent) { // Checked by the caller + // if (_bodyComponent) { // Checked by the caller _bodyComponent->deinit(); _world->deleteBodyComponent(_bodyComponent); _bodyComponent = NULL; @@ -102,7 +107,7 @@ void Entity::removeBodyComponent() void Entity::removeRenderComponent() { - //if (_renderComponent) { // Checked by the caller + // if (_renderComponent) { // Checked by the caller _renderComponent->deinit(); _world->deleteRenderComponent(_renderComponent); _renderComponent = NULL; @@ -111,7 +116,7 @@ void Entity::removeRenderComponent() void Entity::removeMovementComponent() { - //if (_renderComponent) { // Checked by the caller + // if (_renderComponent) { // Checked by the caller _movementComponent->deinit(); _world->deleteMovementComponent(_movementComponent); _movementComponent = NULL; @@ -120,7 +125,7 @@ void Entity::removeMovementComponent() void Entity::removeUpdateComponent() { - //if (_updateComponent) { // Checked by the caller + // if (_updateComponent) { // Checked by the caller _updateComponent->deinit(); _world->deleteUpdateComponent(_updateComponent); _updateComponent = NULL; @@ -129,7 +134,7 @@ void Entity::removeUpdateComponent() void Entity::removeStateComponent() { - //if (_stateComponent) { // Checked by the caller + // if (_stateComponent) { // Checked by the caller _stateComponent->deinit(); _world->deleteStateComponent(_stateComponent); _stateComponent = NULL; @@ -138,7 +143,7 @@ void Entity::removeStateComponent() void Entity::removeCollisionComponent() { - //if (_collisionComponent) { // Checked by the caller + // if (_collisionComponent) { // Checked by the caller _collisionComponent->deinit(); _world->deleteCollisionComponent(_collisionComponent); _collisionComponent = NULL; @@ -150,7 +155,7 @@ void Entity::removeTimeComponent() _timeComponent.deinit(); } -BodyComponent* Entity::getBodyComponent() +BodyComponent *Entity::getBodyComponent() { return _bodyComponent; } diff --git a/src/engine/Entity.h b/src/engine/Entity.h index 2895f15..1961d6c 100644 --- a/src/engine/Entity.h +++ b/src/engine/Entity.h @@ -21,109 +21,113 @@ // Other possible components // STATE COMPONENT // COLLISION COMPONENT -// UPDATE COMPONENT +// UPDATE COMPONENT class PEntity; -// class Entity - // is the internal Entity class (the storage, containing pointers to components). - // Its life cycle is managed by the World it lives in, allocated in a pool. - // the user will use class PEntity that will contain only the pointer to the attached Entity (internal one) - // and the functions to access it +// class Entity +// is the internal Entity class (the storage, containing pointers to components). +// Its life cycle is managed by the World it lives in, allocated in a pool. +// the user will use class PEntity that will contain only the pointer to the attached Entity (internal one) +// and the functions to access it class World; -class Entity : public Poolable { +class Entity : public Poolable +{ // Protect constructor ? this class should not be used by users. that's the purpose of PEntity - //protected: - //friend class Pool; - //friend class EntityPool; - public: - Entity(); - virtual ~Entity(); - - void addBodyComponent(); - void addRenderComponent(); - void addMovementComponent(); - void addUpdateComponent(UpdateCallback onUpdate); - void addStateComponent(); - void addCollisionComponent(); - void addTimeComponent(); - - void removeBodyComponent(); - void removeRenderComponent(); - void removeMovementComponent(); - void removeUpdateComponent(); - void removeStateComponent(); - void removeCollisionComponent(); - void removeTimeComponent(); - - BodyComponent* getBodyComponent(); - //RenderComponent* getRenderComponent(); - - // These way of handling components is generic but CPU expensive - // because an array of components should be traversed. still, it would be a possibility - //void addComponent(BaseComponent* component); - //void removeComponent(BaseComponent* component); - //Component getComponent(uint8_t componentId); - //BaseComponent* _components[COMPONENTS_PER_ENTITY]; - //int _componentsSize; - - // static Entity* create(); //Alternative creation method.. but it needs to know the world anyway - - public: - // Use the ID to check entities equality - int getId(); - uint8_t getType(); - - protected: - friend class World; - void init(World* world); - void deinit(); - - private: - friend class PEntity; - static int _classId; - int _id; - uint8_t _type; - - // Component pools for each type of components. The size for each pool should be user-specified (default size to be decided) - // The pools should be defined in World class. - // Then, here there should be an array of pointers to Component which will be used to iterate all entity's components. - // When an entity gets destroyed, its components should be destroyed too. - - World* _world; - BodyComponent* _bodyComponent; - RenderComponent* _renderComponent; - MovementComponent* _movementComponent; - UpdateComponent* _updateComponent; - IntStateComponent* _stateComponent; - CollisionComponent* _collisionComponent; - - // Components always allocated - TimeComponent _timeComponent; - // TODO: move BodyComponent from pointer to actual object definition -> it is present in each entity - //BodyComponent _bodyComponent; + // protected: + // friend class Pool; + // friend class EntityPool; +public: + Entity(); + virtual ~Entity(); + + void addBodyComponent(); + void addRenderComponent(); + void addMovementComponent(); + void addUpdateComponent(UpdateCallback onUpdate); + void addStateComponent(); + void addCollisionComponent(); + void addTimeComponent(); + + void removeBodyComponent(); + void removeRenderComponent(); + void removeMovementComponent(); + void removeUpdateComponent(); + void removeStateComponent(); + void removeCollisionComponent(); + void removeTimeComponent(); + + BodyComponent *getBodyComponent(); + // RenderComponent* getRenderComponent(); + + // These way of handling components is generic but CPU expensive + // because an array of components should be traversed. still, it would be a possibility + // void addComponent(BaseComponent* component); + // void removeComponent(BaseComponent* component); + // Component getComponent(uint8_t componentId); + // BaseComponent* _components[COMPONENTS_PER_ENTITY]; + // int _componentsSize; + + // static Entity* create(); //Alternative creation method.. but it needs to know the world anyway + +public: + // Use the ID to check entities equality + int getId(); + uint8_t getType(); + +protected: + friend class World; + void init(World *world); + void deinit(); + +private: + friend class PEntity; + static int _classId; + int _id; + uint8_t _type; + + // Component pools for each type of components. The size for each pool should be user-specified (default size to be decided) + // The pools should be defined in World class. + // Then, here there should be an array of pointers to Component which will be used to iterate all entity's components. + // When an entity gets destroyed, its components should be destroyed too. + + World *_world; + BodyComponent *_bodyComponent; + RenderComponent *_renderComponent; + MovementComponent *_movementComponent; + UpdateComponent *_updateComponent; + IntStateComponent *_stateComponent; + CollisionComponent *_collisionComponent; + + // Components always allocated + TimeComponent _timeComponent; + // TODO: move BodyComponent from pointer to actual object definition -> it is present in each entity + // BodyComponent _bodyComponent; }; // Alternative to component POOLS -// create a container for each component type. the container is an array of fixed size that takes the initialized elements at the head -// of the array. to do this, new elements are initialized one after the other, when an element is eliminated it gets swapped with the -// latest initialized element of the array. When there is a SWAP operation, the entity of the swapped component should update +// create a container for each component type. the container is an array of fixed size that takes the initialized elements at the head +// of the array. to do this, new elements are initialized one after the other, when an element is eliminated it gets swapped with the +// latest initialized element of the array. When there is a SWAP operation, the entity of the swapped component should update // its pointer to the component. to do this, the component should be recognizable from the entity (some unique id is needed) OR it // the component could change the pointer itself. The latter can be done by using an object that points to the component and is pointed // to by the component. (class ComponentPointer) -class EntityPool : public Pool { - public: - void update() +class EntityPool : public Pool +{ +public: + void update() + { + for (int i = 0; i < ENTITIES_PER_WORLD; i++) { - for (int i = 0; i < ENTITIES_PER_WORLD; i++) { - if (_pool[i].isAllocated()) { - //_pool[i].update(); - } + if (_pool[i].isAllocated()) + { + //_pool[i].update(); } } + } }; #endif // ENTITY_H diff --git a/src/engine/PEntity.cpp b/src/engine/PEntity.cpp index fad4168..0a3be38 100644 --- a/src/engine/PEntity.cpp +++ b/src/engine/PEntity.cpp @@ -1,18 +1,15 @@ #include "Common.h" #include "PEntity.h" -PEntity::PEntity() : - _entity(NULL) +PEntity::PEntity() : _entity(NULL) { } -PEntity::PEntity(Entity& entity) : - _entity(&entity) +PEntity::PEntity(Entity &entity) : _entity(&entity) { } -PEntity::PEntity(Entity* entity) : - _entity(entity) +PEntity::PEntity(Entity *entity) : _entity(entity) { } @@ -22,14 +19,16 @@ PEntity::~PEntity() void PEntity::configure(int x, int y, int width, int height) { - if (_entity) { + if (_entity) + { _entity->_bodyComponent->configure(x, y, width, height); } } int PEntity::getId() { - if (_entity) { + if (_entity) + { return _entity->_id; } return -1; @@ -37,7 +36,8 @@ int PEntity::getId() uint8_t PEntity::getType() { - if (_entity) { + if (_entity) + { return _entity->_type; } return 0xFF; @@ -45,15 +45,18 @@ uint8_t PEntity::getType() void PEntity::setType(uint8_t entityType) { - if (_entity) { + if (_entity) + { _entity->_type = entityType; } } void PEntity::moveTo(int x, int y, int velocity) { - if (_entity && _entity->_movementComponent) _entity->_movementComponent->configure(x, y, velocity); - else if (_entity) { + if (_entity && _entity->_movementComponent) + _entity->_movementComponent->configure(x, y, velocity); + else if (_entity) + { _entity->addMovementComponent(); _entity->_movementComponent->configure(x, y, velocity); } @@ -61,7 +64,8 @@ void PEntity::moveTo(int x, int y, int velocity) Vect2 PEntity::getMovement() { - if (_entity && _entity->_movementComponent) { + if (_entity && _entity->_movementComponent) + { return _entity->_movementComponent->getMovement(); } return (Vect2(0, 0)); @@ -75,26 +79,32 @@ bool PEntity::isMoving() void PEntity::update(UpdateCallback onUpdate) { - if (_entity && _entity->_updateComponent) _entity->_updateComponent->config(onUpdate); - else if (_entity) _entity->addUpdateComponent(onUpdate); + if (_entity && _entity->_updateComponent) + _entity->_updateComponent->config(onUpdate); + else if (_entity) + _entity->addUpdateComponent(onUpdate); } void PEntity::collision() { - if (_entity && _entity->_collisionComponent == NULL) _entity->addCollisionComponent(); + if (_entity && _entity->_collisionComponent == NULL) + _entity->addCollisionComponent(); } void PEntity::collision(CollisionCallback onCollision) { - if (_entity) { - if (_entity->_collisionComponent == NULL) _entity->addCollisionComponent(); + if (_entity) + { + if (_entity->_collisionComponent == NULL) + _entity->addCollisionComponent(); _entity->_collisionComponent->configure(onCollision); } } bool PEntity::collided(PEntity other) { - if (_entity && other._entity) { + if (_entity && other._entity) + { return CollisionComponent::check(_entity, other._entity); } return false; @@ -102,8 +112,10 @@ bool PEntity::collided(PEntity other) int PEntity::getState(uint8_t index) { - if (_entity) { - if (_entity->_stateComponent == NULL) _entity->addStateComponent(); + if (_entity) + { + if (_entity->_stateComponent == NULL) + _entity->addStateComponent(); return _entity->_stateComponent->getState(index); } return 0; @@ -111,44 +123,50 @@ int PEntity::getState(uint8_t index) void PEntity::setState(uint8_t index, int value) { - if (_entity) { - if (_entity->_stateComponent == NULL) _entity->addStateComponent(); + if (_entity) + { + if (_entity->_stateComponent == NULL) + _entity->addStateComponent(); _entity->_stateComponent->setState(index, value); - } + } } -void PEntity::setImage(const uint8_t* image) +void PEntity::setImage(const uint8_t *image) { - if (_entity) { + if (_entity) + { _entity->_renderComponent->setImage(image); } } -void PEntity::setImage(const uint16_t* image) +void PEntity::setImage(const uint16_t *image) { - if (_entity) { + if (_entity) + { _entity->_renderComponent->setImage(image); } } -void PEntity::setAnimation(const Animation& animation) +void PEntity::setAnimation(const Animation &animation) { - if (_entity) { + if (_entity) + { _entity->_renderComponent->setAnimation(animation); } } - void PEntity::newTimeout(uint8_t index, uint64_t time) { - if (_entity) { + if (_entity) + { _entity->_timeComponent.init(index, time); } } bool PEntity::checkTimeout(uint8_t index) { - if (_entity) { + if (_entity) + { return _entity->_timeComponent.checkTimeout(index); } return false; @@ -156,23 +174,26 @@ bool PEntity::checkTimeout(uint8_t index) TimeoutId PEntity::getTimeout(uint8_t index) { - if (_entity) { + if (_entity) + { return _entity->_timeComponent.getTimeout(index); } return 0; } -int PEntity::getX() +int PEntity::getX() { - if (_entity) { + if (_entity) + { return _entity->_bodyComponent->getPosition().x; } return 0; } -int PEntity::getY() +int PEntity::getY() { - if (_entity) { + if (_entity) + { return _entity->_bodyComponent->getPosition().y; } return 0; @@ -180,7 +201,8 @@ int PEntity::getY() Vect2 PEntity::getPosition() { - if (_entity) { + if (_entity) + { return _entity->_bodyComponent->getPosition(); } return Vect2(0, 0); diff --git a/src/engine/PEntity.h b/src/engine/PEntity.h index 55c51ba..cfdd595 100644 --- a/src/engine/PEntity.h +++ b/src/engine/PEntity.h @@ -8,52 +8,53 @@ #include "drivers/Timer.h" // just a reference to Entity -//typedef Entity& PEntity; +// typedef Entity& PEntity; // Entity class is the storage of the entity // PEntity is the interface that is retrieved to the user side application. // Alternative to reference -class PEntity { - public: - virtual ~PEntity(); - PEntity(); - PEntity(Entity& entity); - PEntity(Entity* entity); - - int getId(); - uint8_t getType(); - void setType(uint8_t entityType); - void configure(int x, int y, int width, int height); - - void moveTo(int x, int y, int velocity); - Vect2 getMovement(); - bool isMoving(); - - void update(UpdateCallback onUpdate); - - void collision(); - void collision(CollisionCallback onCollision); - bool collided(PEntity other); - - int getState(uint8_t index); - void setState(uint8_t index, int value); - - void setImage(const uint8_t* image); - void setImage(const uint16_t* image); - void setAnimation(const Animation& animation); - - void newTimeout(uint8_t index, uint64_t time); - TimeoutId getTimeout(uint8_t index); - bool checkTimeout(uint8_t index); - - Vect2 getPosition(); - int getX(); - int getY(); - - private: - friend class World; - Entity* _entity; +class PEntity +{ +public: + virtual ~PEntity(); + PEntity(); + PEntity(Entity &entity); + PEntity(Entity *entity); + + int getId(); + uint8_t getType(); + void setType(uint8_t entityType); + void configure(int x, int y, int width, int height); + + void moveTo(int x, int y, int velocity); + Vect2 getMovement(); + bool isMoving(); + + void update(UpdateCallback onUpdate); + + void collision(); + void collision(CollisionCallback onCollision); + bool collided(PEntity other); + + int getState(uint8_t index); + void setState(uint8_t index, int value); + + void setImage(const uint8_t *image); + void setImage(const uint16_t *image); + void setAnimation(const Animation &animation); + + void newTimeout(uint8_t index, uint64_t time); + TimeoutId getTimeout(uint8_t index); + bool checkTimeout(uint8_t index); + + Vect2 getPosition(); + int getX(); + int getY(); + +private: + friend class World; + Entity *_entity; }; #endif // PENTITY_H diff --git a/src/engine/Pool.h b/src/engine/Pool.h index ec9ddee..37331df 100644 --- a/src/engine/Pool.h +++ b/src/engine/Pool.h @@ -3,131 +3,138 @@ #include "Common.h" -class Poolable { - public: - //virtual void init(); - //virtual void deinit(); - - void setNext(Poolable* next) - { - _next = next; - } - - Poolable* getNext() - { - return _next; - } - - void setAllocated(bool isAllocated) - { - _isAllocated = isAllocated; - } - - bool isAllocated() - { - return _isAllocated; - } - - private: - Poolable* _next; - bool _isAllocated; - -#if defined (POOL_DOUBLE_LINK) - public: - void setPrev(Poolable* prev) - { - _prev = prev; - } - - Poolable* getPrev() - { - return _prev; - } - - private: - Poolable* _prev; +class Poolable +{ +public: + // virtual void init(); + // virtual void deinit(); + + void setNext(Poolable *next) + { + _next = next; + } + + Poolable *getNext() + { + return _next; + } + + void setAllocated(bool isAllocated) + { + _isAllocated = isAllocated; + } + + bool isAllocated() + { + return _isAllocated; + } + +private: + Poolable *_next; + bool _isAllocated; + +#if defined(POOL_DOUBLE_LINK) +public: + void setPrev(Poolable *prev) + { + _prev = prev; + } + + Poolable *getPrev() + { + return _prev; + } + +private: + Poolable *_prev; #endif }; template -class Pool { - public: - - Pool() +class Pool +{ +public: + Pool() + { + // Put all the items in the free list + for (int i = 0; i < (N - 1); i++) { - // Put all the items in the free list - for (int i = 0; i < (N - 1); i++) { - _pool[i].setNext(&_pool[i + 1]); - _pool[i].setAllocated(false); - } - _headFree = &_pool[0]; - _pool[N - 1].setNext(NULL); -#if defined (POOL_DOUBLE_LINK) - // Allocated list is empty - _headAllocated = NULL; -#endif + _pool[i].setNext(&_pool[i + 1]); + _pool[i].setAllocated(false); } - - virtual ~Pool() {} - - T& newItem() - { - // TODO: check if the pool is full - //if (!_headFree) { - //while(1); - //} - - // Remove from the free list - T& item = *_headFree; - _headFree = static_cast(item.getNext()); - //item.setNext(NULL); -#if defined (POOL_DOUBLE_LINK) - // Add to the allocated list - item.setNext(_headAllocated); - item.setPrev(NULL); - if (_headAllocated) _headAllocated->setPrev(&item); - _headAllocated = &item; + _headFree = &_pool[0]; + _pool[N - 1].setNext(NULL); +#if defined(POOL_DOUBLE_LINK) + // Allocated list is empty + _headAllocated = NULL; +#endif + } + + virtual ~Pool() {} + + T &newItem() + { + // TODO: check if the pool is full + // if (!_headFree) { + // while(1); + //} + + // Remove from the free list + T &item = *_headFree; + _headFree = static_cast(item.getNext()); + // item.setNext(NULL); +#if defined(POOL_DOUBLE_LINK) + // Add to the allocated list + item.setNext(_headAllocated); + item.setPrev(NULL); + if (_headAllocated) + _headAllocated->setPrev(&item); + _headAllocated = &item; #endif - item.setAllocated(true); - //item.init() - return item; + item.setAllocated(true); + // item.init() + return item; + } + + void deleteItem(T &item) + { + // TODO: check if the item is already in the free list + // if (!item.isAllocated()) return; + item.setAllocated(false); + // Remove from the allocated list +#if defined(POOL_DOUBLE_LINK) + if (item.getPrev()) + { + item.getPrev()->setNext(item.getNext()); } - - void deleteItem(T& item) + else { - // TODO: check if the item is already in the free list - //if (!item.isAllocated()) return; - item.setAllocated(false); - // Remove from the allocated list -#if defined (POOL_DOUBLE_LINK) - if (item.getPrev()) { - item.getPrev()->setNext(item.getNext()); - } else { - _headAllocated = static_cast(item.getNext()); - } - if (item.getNext()) { - item.getNext()->setPrev(item.getPrev()); - } -#endif - // Add to the free list - item.setNext(_headFree); - _headFree = &item; + _headAllocated = static_cast(item.getNext()); } - - void clear() {} - -#if defined (POOL_DOUBLE_LINK) - T* getItems() + if (item.getNext()) { - return _headAllocated; + item.getNext()->setPrev(item.getPrev()); } #endif + // Add to the free list + item.setNext(_headFree); + _headFree = &item; + } + + void clear() {} + +#if defined(POOL_DOUBLE_LINK) + T *getItems() + { + return _headAllocated; + } +#endif - protected: - T _pool[N]; - T* _headFree; -#if defined (POOL_DOUBLE_LINK) - T* _headAllocated; +protected: + T _pool[N]; + T *_headFree; +#if defined(POOL_DOUBLE_LINK) + T *_headAllocated; #endif }; diff --git a/src/engine/World.cpp b/src/engine/World.cpp index 3236df1..a5a624f 100644 --- a/src/engine/World.cpp +++ b/src/engine/World.cpp @@ -13,7 +13,7 @@ void World::clear() // TODO // Clearing is not enough. They should be deinitialized // A possibility would be to move the init and deinit in each specific Pool - // by overloading the newItem function of each specific pool + // by overloading the newItem function of each specific pool // (and passing the eventually needed init/deinit parameters) //_entities.clear(); //_bodyComponents.clear(); @@ -22,74 +22,74 @@ void World::clear() PEntity World::newEntity() { - Entity& entity = _entities.newItem(); + Entity &entity = _entities.newItem(); entity.init(this); return PEntity(entity); } void World::deleteEntity(PEntity pEntity) { - Entity& entity = *(pEntity._entity); + Entity &entity = *(pEntity._entity); entity.deinit(); _entities.deleteItem(entity); } -BodyComponent* World::newBodyComponent() +BodyComponent *World::newBodyComponent() { return &_bodyComponents.newItem(); } -RenderComponent* World::newRenderComponent() +RenderComponent *World::newRenderComponent() { return &_renderComponents.newItem(); } -MovementComponent* World::newMovementComponent() +MovementComponent *World::newMovementComponent() { return &_movementComponents.newItem(); } -UpdateComponent* World::newUpdateComponent() +UpdateComponent *World::newUpdateComponent() { return &_updateComponents.newItem(); } -IntStateComponent* World::newStateComponent() +IntStateComponent *World::newStateComponent() { return &_stateComponents.newItem(); } -CollisionComponent* World::newCollisionComponent() +CollisionComponent *World::newCollisionComponent() { return &_collisionComponents.newItem(); } -void World::deleteBodyComponent(BodyComponent* component) +void World::deleteBodyComponent(BodyComponent *component) { _bodyComponents.deleteItem(*component); } -void World::deleteRenderComponent(RenderComponent* component) +void World::deleteRenderComponent(RenderComponent *component) { _renderComponents.deleteItem(*component); } -void World::deleteMovementComponent(MovementComponent* component) +void World::deleteMovementComponent(MovementComponent *component) { _movementComponents.deleteItem(*component); } -void World::deleteUpdateComponent(UpdateComponent* component) +void World::deleteUpdateComponent(UpdateComponent *component) { _updateComponents.deleteItem(*component); } -void World::deleteStateComponent(IntStateComponent* component) +void World::deleteStateComponent(IntStateComponent *component) { _stateComponents.deleteItem(*component); } -void World::deleteCollisionComponent(CollisionComponent* component) +void World::deleteCollisionComponent(CollisionComponent *component) { _collisionComponents.deleteItem(*component); } @@ -106,4 +106,3 @@ void World::render() { _renderComponents.render(); } - diff --git a/src/engine/World.h b/src/engine/World.h index 59af5f8..01f5df3 100644 --- a/src/engine/World.h +++ b/src/engine/World.h @@ -10,41 +10,42 @@ #include "components/StateComponent.h" #include "components/CollisionComponent.h" -class World { - public: - World(); - virtual ~World(); - - void clear(); - - PEntity newEntity(); - void deleteEntity(PEntity entity); - - BodyComponent* newBodyComponent(); - RenderComponent* newRenderComponent(); - MovementComponent* newMovementComponent(); - UpdateComponent* newUpdateComponent(); - IntStateComponent* newStateComponent(); - CollisionComponent* newCollisionComponent(); - - void deleteBodyComponent(BodyComponent* component); - void deleteRenderComponent(RenderComponent* component); - void deleteMovementComponent(MovementComponent* component); - void deleteUpdateComponent(UpdateComponent* component); - void deleteStateComponent(IntStateComponent* component); - void deleteCollisionComponent(CollisionComponent* component); - - void update(); - void render(); - - private: - EntityPool _entities; - BodyComponentPool _bodyComponents; - RenderComponentPool _renderComponents; - MovementComponentPool _movementComponents; - UpdateComponentPool _updateComponents; - IntStateComponentPool _stateComponents; - CollisionComponentPool _collisionComponents; +class World +{ +public: + World(); + virtual ~World(); + + void clear(); + + PEntity newEntity(); + void deleteEntity(PEntity entity); + + BodyComponent *newBodyComponent(); + RenderComponent *newRenderComponent(); + MovementComponent *newMovementComponent(); + UpdateComponent *newUpdateComponent(); + IntStateComponent *newStateComponent(); + CollisionComponent *newCollisionComponent(); + + void deleteBodyComponent(BodyComponent *component); + void deleteRenderComponent(RenderComponent *component); + void deleteMovementComponent(MovementComponent *component); + void deleteUpdateComponent(UpdateComponent *component); + void deleteStateComponent(IntStateComponent *component); + void deleteCollisionComponent(CollisionComponent *component); + + void update(); + void render(); + +private: + EntityPool _entities; + BodyComponentPool _bodyComponents; + RenderComponentPool _renderComponents; + MovementComponentPool _movementComponents; + UpdateComponentPool _updateComponents; + IntStateComponentPool _stateComponents; + CollisionComponentPool _collisionComponents; }; #endif // WORLD_H diff --git a/src/engine/components/BodyComponent.cpp b/src/engine/components/BodyComponent.cpp index 472f22e..e1b621b 100644 --- a/src/engine/components/BodyComponent.cpp +++ b/src/engine/components/BodyComponent.cpp @@ -3,33 +3,32 @@ //#define HITBOX_MARGIN (1) -BodyComponent::BodyComponent() : - _entity(NULL) +BodyComponent::BodyComponent() : _entity(NULL) { } -BodyComponent::~BodyComponent() +BodyComponent::~BodyComponent() { } -void BodyComponent::init(Entity* entity) +void BodyComponent::init(Entity *entity) { _entity = entity; _position = Vect2(0, 0); _size = Vect2(0, 0); } -void BodyComponent::init(Entity* entity, int x, int y, int w, int h) +void BodyComponent::init(Entity *entity, int x, int y, int w, int h) { _entity = entity; _position = Vect2(x, y); _size = Vect2(w, h); -#if defined (HITBOX_MARGIN) - _minHitbox = Vect2( (x - w / 2 * HITBOX_MARGIN), (y - h / 2 * HITBOX_MARGIN) ); - _maxHitbox = Vect2( (x + w / 2 * HITBOX_MARGIN), (y + h / 2 * HITBOX_MARGIN) ); +#if defined(HITBOX_MARGIN) + _minHitbox = Vect2((x - w / 2 * HITBOX_MARGIN), (y - h / 2 * HITBOX_MARGIN)); + _maxHitbox = Vect2((x + w / 2 * HITBOX_MARGIN), (y + h / 2 * HITBOX_MARGIN)); #else - _minHitbox = Vect2( (x - w / 2), (y - h / 2) ); - _maxHitbox = Vect2( (x + w / 2), (y + h / 2) ); + _minHitbox = Vect2((x - w / 2), (y - h / 2)); + _maxHitbox = Vect2((x + w / 2), (y + h / 2)); #endif } @@ -42,42 +41,42 @@ void BodyComponent::configure(int x, int y, int width, int height) { _position.set(x, y); _size.set(width, height); -#if defined (HITBOX_MARGIN) - _minHitbox.set( (x - width / 2 * HITBOX_MARGIN), (y - height / 2 * HITBOX_MARGIN) ); - _maxHitbox.set( (x + width / 2 * HITBOX_MARGIN), (y + height / 2 * HITBOX_MARGIN) ); +#if defined(HITBOX_MARGIN) + _minHitbox.set((x - width / 2 * HITBOX_MARGIN), (y - height / 2 * HITBOX_MARGIN)); + _maxHitbox.set((x + width / 2 * HITBOX_MARGIN), (y + height / 2 * HITBOX_MARGIN)); #else - _minHitbox.set( (x - width / 2), (y - height / 2) ); - _maxHitbox.set( (x + width / 2), (y + height / 2) ); + _minHitbox.set((x - width / 2), (y - height / 2)); + _maxHitbox.set((x + width / 2), (y + height / 2)); #endif } void BodyComponent::update() { -#if defined (HITBOX_MARGIN) - _minHitbox.set( _position.x - _size.x / 2 * HITBOX_MARGIN, _position.y - _size.y / 2 * HITBOX_MARGIN); - _maxHitbox.set( _position.x + _size.x / 2 * HITBOX_MARGIN, _position.y + _size.y / 2 * HITBOX_MARGIN); +#if defined(HITBOX_MARGIN) + _minHitbox.set(_position.x - _size.x / 2 * HITBOX_MARGIN, _position.y - _size.y / 2 * HITBOX_MARGIN); + _maxHitbox.set(_position.x + _size.x / 2 * HITBOX_MARGIN, _position.y + _size.y / 2 * HITBOX_MARGIN); #else - _minHitbox.set( (_position.x - _size.x / 2), (_position.y - _size.y / 2) ); - _maxHitbox.set( (_position.x + _size.x / 2), (_position.y + _size.y / 2) ); + _minHitbox.set((_position.x - _size.x / 2), (_position.y - _size.y / 2)); + _maxHitbox.set((_position.x + _size.x / 2), (_position.y + _size.y / 2)); #endif } -Vect2& BodyComponent::getPosition() +Vect2 &BodyComponent::getPosition() { return _position; } -Vect2& BodyComponent::getSize() +Vect2 &BodyComponent::getSize() { return _size; } -Vect2& BodyComponent::getMaxHitbox() +Vect2 &BodyComponent::getMaxHitbox() { return _maxHitbox; } -Vect2& BodyComponent::getMinHitbox() +Vect2 &BodyComponent::getMinHitbox() { return _minHitbox; } diff --git a/src/engine/components/BodyComponent.h b/src/engine/components/BodyComponent.h index 309495b..1033b10 100644 --- a/src/engine/components/BodyComponent.h +++ b/src/engine/components/BodyComponent.h @@ -10,11 +10,12 @@ class Entity; -struct Vect2 { +struct Vect2 +{ Vect2() {} Vect2(int inputX, int inputY) : x(inputX), y(inputY) {} - void set(int inputX, int inputY) + void set(int inputX, int inputY) { x = inputX; y = inputY; @@ -23,52 +24,57 @@ struct Vect2 { int x, y; }; -class BodyComponent : public Poolable { - public: - BodyComponent(); - virtual ~BodyComponent(); +class BodyComponent : public Poolable +{ +public: + BodyComponent(); + virtual ~BodyComponent(); - void configure(int x, int y, int width, int height); - Vect2& getPosition(); - Vect2& getSize(); - Vect2& getMaxHitbox(); - Vect2& getMinHitbox(); + void configure(int x, int y, int width, int height); + Vect2 &getPosition(); + Vect2 &getSize(); + Vect2 &getMaxHitbox(); + Vect2 &getMinHitbox(); - void update(); + void update(); - protected: - friend class Entity; - void init(Entity* entity); - void init(Entity* entity, int x, int y, int w, int h); - void deinit(); +protected: + friend class Entity; + void init(Entity *entity); + void init(Entity *entity, int x, int y, int w, int h); + void deinit(); - private: - Entity* _entity; - Vect2 _position; - Vect2 _size; +private: + Entity *_entity; + Vect2 _position; + Vect2 _size; - Vect2 _minHitbox; - Vect2 _maxHitbox; + Vect2 _minHitbox; + Vect2 _maxHitbox; }; -class BodyComponentPool : public Pool { - public: - void update() +class BodyComponentPool : public Pool +{ +public: + void update() + { +#if defined(POOL_DOUBLE_LINK) + auto component = getItems(); + while (component) { -#if defined (POOL_DOUBLE_LINK) - auto component = getItems(); - while (component) { - component->update(); - component = static_cast(component->getNext()); - } + component->update(); + component = static_cast(component->getNext()); + } #else - for (int i = 0; i < BODY_COMPONENTS_PER_WORLD; i++) { - if (_pool[i].isAllocated()) { - _pool[i].update(); - } + for (int i = 0; i < BODY_COMPONENTS_PER_WORLD; i++) + { + if (_pool[i].isAllocated()) + { + _pool[i].update(); } -#endif } +#endif + } }; -#endif +#endif diff --git a/src/engine/components/CollisionComponent.cpp b/src/engine/components/CollisionComponent.cpp index 4013c7b..26eb9a5 100644 --- a/src/engine/components/CollisionComponent.cpp +++ b/src/engine/components/CollisionComponent.cpp @@ -2,9 +2,8 @@ #include "engine/Entity.h" #include "engine/PEntity.h" -CollisionComponent::CollisionComponent() : - _entity(NULL), - _onCollision(NULL) +CollisionComponent::CollisionComponent() : _entity(NULL), + _onCollision(NULL) { } @@ -12,13 +11,13 @@ CollisionComponent::~CollisionComponent() { } -void CollisionComponent::init(Entity* entity) +void CollisionComponent::init(Entity *entity) { _entity = entity; _onCollision = NULL; } -void CollisionComponent::init(Entity* entity, CollisionCallback onCollision) +void CollisionComponent::init(Entity *entity, CollisionCallback onCollision) { _entity = entity; _onCollision = onCollision; @@ -40,10 +39,10 @@ void CollisionComponent::update() } // axis-aligned bounding boxes (AABB) -bool CollisionComponent::check(Entity* entityOne, Entity* entityTwo) +bool CollisionComponent::check(Entity *entityOne, Entity *entityTwo) { - BodyComponent* bodyOne = entityOne->getBodyComponent(); - BodyComponent* bodyTwo = entityTwo->getBodyComponent(); + BodyComponent *bodyOne = entityOne->getBodyComponent(); + BodyComponent *bodyTwo = entityTwo->getBodyComponent(); Vect2 maxOne = bodyOne->getMaxHitbox(); Vect2 minOne = bodyOne->getMinHitbox(); Vect2 maxTwo = bodyTwo->getMaxHitbox(); @@ -60,31 +59,43 @@ bool CollisionComponent::check(Entity* entityOne, Entity* entityTwo) void CollisionComponentPool::update() { -#if defined (POOL_DOUBLE_LINK) +#if defined(POOL_DOUBLE_LINK) auto first = getItems(); - while (first) { - auto nextFirst = static_cast(first->getNext()); - auto other = static_cast(first->getNext()); - while (other) { - auto nextOther = static_cast(other->getNext()); - if (CollisionComponent::check(first->_entity, other->_entity)) { - if (first->_onCollision) first->_onCollision(PEntity(first->_entity), PEntity(other->_entity)); - if (other->_onCollision) other->_onCollision(PEntity(other->_entity), PEntity(first->_entity)); + while (first) + { + auto nextFirst = static_cast(first->getNext()); + auto other = static_cast(first->getNext()); + while (other) + { + auto nextOther = static_cast(other->getNext()); + if (CollisionComponent::check(first->_entity, other->_entity)) + { + if (first->_onCollision) + first->_onCollision(PEntity(first->_entity), PEntity(other->_entity)); + if (other->_onCollision) + other->_onCollision(PEntity(other->_entity), PEntity(first->_entity)); } other = nextOther; } first = nextFirst; } #else - for (int i = 0; i < COLLISION_COMPONENTS_PER_WORLD - 1; i++) { - if (_pool[i].isAllocated()) { + for (int i = 0; i < COLLISION_COMPONENTS_PER_WORLD - 1; i++) + { + if (_pool[i].isAllocated()) + { auto first = _pool[i]; - for (int j = i + 1; j < COLLISION_COMPONENTS_PER_WORLD; j++) { - if (_pool[j].isAllocated()) { + for (int j = i + 1; j < COLLISION_COMPONENTS_PER_WORLD; j++) + { + if (_pool[j].isAllocated()) + { auto other = _pool[j]; - if (CollisionComponent::check(first._entity, other._entity)) { - if (first._onCollision) first._onCollision(PEntity(first._entity), PEntity(other._entity)); - if (other._onCollision) other._onCollision(PEntity(other._entity), PEntity(first._entity)); + if (CollisionComponent::check(first._entity, other._entity)) + { + if (first._onCollision) + first._onCollision(PEntity(first._entity), PEntity(other._entity)); + if (other._onCollision) + other._onCollision(PEntity(other._entity), PEntity(first._entity)); } } } diff --git a/src/engine/components/CollisionComponent.h b/src/engine/components/CollisionComponent.h index 69012fc..f3b0676 100644 --- a/src/engine/components/CollisionComponent.h +++ b/src/engine/components/CollisionComponent.h @@ -15,33 +15,35 @@ typedef void (*CollisionCallback)(PEntity entity, PEntity other); // TODO: template for layering -> one pool for each different layer of collisions -class CollisionComponent : public Poolable { - public: - CollisionComponent(); - virtual ~CollisionComponent(); +class CollisionComponent : public Poolable +{ +public: + CollisionComponent(); + virtual ~CollisionComponent(); - void configure(CollisionCallback onCollision); + void configure(CollisionCallback onCollision); - static bool check(Entity* entityOne, Entity* entityTwo); + static bool check(Entity *entityOne, Entity *entityTwo); - protected: - friend class Entity; - void init(Entity* entity); - void init(Entity* entity, CollisionCallback onCollision); - void deinit(); +protected: + friend class Entity; + void init(Entity *entity); + void init(Entity *entity, CollisionCallback onCollision); + void deinit(); - void update(); + void update(); - private: - friend class CollisionComponentPool; - friend class Entity; - Entity* _entity; - CollisionCallback _onCollision; +private: + friend class CollisionComponentPool; + friend class Entity; + Entity *_entity; + CollisionCallback _onCollision; }; -class CollisionComponentPool : public Pool { - public: - void update(); +class CollisionComponentPool : public Pool +{ +public: + void update(); }; -#endif +#endif diff --git a/src/engine/components/MovementComponent.cpp b/src/engine/components/MovementComponent.cpp index cf2cee4..7bd4767 100644 --- a/src/engine/components/MovementComponent.cpp +++ b/src/engine/components/MovementComponent.cpp @@ -5,9 +5,8 @@ #define DECIMAL_SAVE 1024 // needed to avoid using float while preserving decimal digits #define VELOCITY_RATE 8 -MovementComponent::MovementComponent() : - _entity(NULL), - _bodyComponent(NULL) +MovementComponent::MovementComponent() : _entity(NULL), + _bodyComponent(NULL) { } @@ -15,14 +14,14 @@ MovementComponent::~MovementComponent() { } -void MovementComponent::init(Entity* entity) +void MovementComponent::init(Entity *entity) { _entity = entity; _bodyComponent = _entity->getBodyComponent(); _movement = Vect2(0, 0); } -void MovementComponent::init(Entity* entity, BodyComponent* bodyComponent) +void MovementComponent::init(Entity *entity, BodyComponent *bodyComponent) { _entity = entity; _bodyComponent = bodyComponent; @@ -43,66 +42,76 @@ void MovementComponent::configure(int x, int y, int velocity) void MovementComponent::configure(Vect2 destination, int velocity) { - if (velocity == 0) { + if (velocity == 0) + { _movement = Vect2(0, 0); return; } // vectorial operators could be implemented and used - Vect2& currentPosition = _bodyComponent->getPosition(); + Vect2 ¤tPosition = _bodyComponent->getPosition(); float distanceX = destination.x - currentPosition.x; float distanceY = destination.y - currentPosition.y; float len = sqrt(distanceX * distanceX + distanceY * distanceY); - if ((int)len == 0) { + if ((int)len == 0) + { _movement = Vect2(0, 0); return; } - _movement = Vect2(distanceX * velocity * DECIMAL_SAVE / VELOCITY_RATE / len, + _movement = Vect2(distanceX * velocity * DECIMAL_SAVE / VELOCITY_RATE / len, distanceY * velocity * DECIMAL_SAVE / VELOCITY_RATE / len); _movedX = currentPosition.x * DECIMAL_SAVE; _movedY = currentPosition.y * DECIMAL_SAVE; - _destination = destination; + _destination = destination; } -void MovementComponent::update() +void MovementComponent::update() { - if (_movement.x == 0 && _movement.y == 0) { - return; + if (_movement.x == 0 && _movement.y == 0) + { + return; } - Vect2& position = _bodyComponent->getPosition(); + Vect2 &position = _bodyComponent->getPosition(); // in this way movement is cleared in the frame following the last position update - if (position.x == _destination.x && position.y == _destination.y) { + if (position.x == _destination.x && position.y == _destination.y) + { _movement = Vect2(0, 0); return; - } + } int nextX = (_movedX + _movement.x) / DECIMAL_SAVE; - if ( (_movement.x > 0 && nextX >= _destination.x) || (_movement.x < 0 && nextX <= _destination.x) ) { + if ((_movement.x > 0 && nextX >= _destination.x) || (_movement.x < 0 && nextX <= _destination.x)) + { _movedX = _destination.x * DECIMAL_SAVE; // uncommenting this will result in clearing movement in the same cycle of the last position update - //_movement.x = 0; - } else { + //_movement.x = 0; + } + else + { _movedX += _movement.x; } int nextY = (_movedY + _movement.y) / DECIMAL_SAVE; - if ( (_movement.y > 0 && nextY >= _destination.y) || (_movement.y < 0 && nextY <= _destination.y) ) { + if ((_movement.y > 0 && nextY >= _destination.y) || (_movement.y < 0 && nextY <= _destination.y)) + { _movedY = _destination.y * DECIMAL_SAVE; // uncommenting this will result in clearing movement in the same cycle of the last position update //_movement.y = 0; - } else { + } + else + { _movedY += _movement.y; } position.set(_movedX / DECIMAL_SAVE, _movedY / DECIMAL_SAVE); } -Vect2& MovementComponent::getMovement() +Vect2 &MovementComponent::getMovement() { return _movement; } diff --git a/src/engine/components/MovementComponent.h b/src/engine/components/MovementComponent.h index 2bc70d2..1783fe6 100644 --- a/src/engine/components/MovementComponent.h +++ b/src/engine/components/MovementComponent.h @@ -11,49 +11,54 @@ class Entity; -class MovementComponent : public Poolable { - public: - MovementComponent(); - virtual ~MovementComponent(); - - void configure(int x, int y, int velocity); - void configure(Vect2 destination, int velocity); - void update(); - - Vect2& getMovement(); - - protected: - friend class Entity; - void init(Entity* entity); - void init(Entity* entity, BodyComponent* bodyComponent); - void deinit(); - - private: - Entity* _entity; - BodyComponent* _bodyComponent; - Vect2 _destination; - Vect2 _movement; - int _movedX, _movedY; +class MovementComponent : public Poolable +{ +public: + MovementComponent(); + virtual ~MovementComponent(); + + void configure(int x, int y, int velocity); + void configure(Vect2 destination, int velocity); + void update(); + + Vect2 &getMovement(); + +protected: + friend class Entity; + void init(Entity *entity); + void init(Entity *entity, BodyComponent *bodyComponent); + void deinit(); + +private: + Entity *_entity; + BodyComponent *_bodyComponent; + Vect2 _destination; + Vect2 _movement; + int _movedX, _movedY; }; -class MovementComponentPool : public Pool { - public: - void update() +class MovementComponentPool : public Pool +{ +public: + void update() + { +#if defined(POOL_DOUBLE_LINK) + auto component = getItems(); + while (component) { -#if defined (POOL_DOUBLE_LINK) - auto component = getItems(); - while (component) { - component->update(); - component = static_cast(component->getNext()); - } + component->update(); + component = static_cast(component->getNext()); + } #else - for (int i = 0; i < MOVEMENT_COMPONENTS_PER_WORLD; i++) { - if (_pool[i].isAllocated()) { - _pool[i].update(); - } + for (int i = 0; i < MOVEMENT_COMPONENTS_PER_WORLD; i++) + { + if (_pool[i].isAllocated()) + { + _pool[i].update(); } -#endif } +#endif + } }; -#endif +#endif diff --git a/src/engine/components/RenderComponent.cpp b/src/engine/components/RenderComponent.cpp index 9100537..3a90255 100644 --- a/src/engine/components/RenderComponent.cpp +++ b/src/engine/components/RenderComponent.cpp @@ -3,26 +3,25 @@ #include "drivers/DriverLayer.h" -RenderComponent::RenderComponent() : - _entity(NULL), - _bodyComponent(NULL), - _imageMonochrome(NULL), - _imageColor(NULL), - _animation(NULL) +RenderComponent::RenderComponent() : _entity(NULL), + _bodyComponent(NULL), + _imageMonochrome(NULL), + _imageColor(NULL), + _animation(NULL) { } -RenderComponent::~RenderComponent() +RenderComponent::~RenderComponent() { } -void RenderComponent::init(Entity* entity) +void RenderComponent::init(Entity *entity) { _entity = entity; _bodyComponent = _entity->getBodyComponent(); } -void RenderComponent::init(Entity* entity, BodyComponent* bodyComponent) +void RenderComponent::init(Entity *entity, BodyComponent *bodyComponent) { _entity = entity; _bodyComponent = bodyComponent; @@ -43,53 +42,62 @@ void RenderComponent::deinit() void RenderComponent::render() { // select display driver and draw sprite - Vect2& position = _bodyComponent->getPosition(); - Vect2& size = _bodyComponent->getSize(); + Vect2 &position = _bodyComponent->getPosition(); + Vect2 &size = _bodyComponent->getSize(); - if (_animation) { - if (timer.checkTimeout(_animationTimeout)) { + if (_animation) + { + if (timer.checkTimeout(_animationTimeout)) + { timer.setTimeout(_animationTimeout, _animation->times[_animationCounter]); - if (_animation->imagesMonochrome) _imageMonochrome = _animation->imagesMonochrome[_animationCounter]; - else if (_animation->imagesColor) _imageColor = _animation->imagesColor[_animationCounter]; + if (_animation->imagesMonochrome) + _imageMonochrome = _animation->imagesMonochrome[_animationCounter]; + else if (_animation->imagesColor) + _imageColor = _animation->imagesColor[_animationCounter]; _animationCounter = (_animationCounter + 1) % _animation->length; } } - if (_imageMonochrome) { + if (_imageMonochrome) + { display.drawImage(position.x, position.y, size.x, size.y, _imageMonochrome); - - } else if (_imageColor) { + } + else if (_imageColor) + { display.drawImage(position.x, position.y, size.x, size.y, _imageColor); - - } else { + } + else + { display.drawRectangle(position.x, position.y, size.x, size.y, WHITE_COLOR); } } -void RenderComponent::setImage(const uint8_t* image) +void RenderComponent::setImage(const uint8_t *image) { removeAnimation(); _imageMonochrome = image; _imageColor = NULL; } -void RenderComponent::setImage(const uint16_t* image) +void RenderComponent::setImage(const uint16_t *image) { removeAnimation(); _imageColor = image; _imageMonochrome = NULL; } -void RenderComponent::setAnimation(const Animation& animation) +void RenderComponent::setAnimation(const Animation &animation) { _animation = &animation; _animationCounter = 1; _animationTimeout = timer.newTimeout(animation.times[0]); - if (animation.imagesColor) { + if (animation.imagesColor) + { _imageColor = animation.imagesColor[0]; _imageMonochrome = NULL; } - else if (animation.imagesMonochrome) { + else if (animation.imagesMonochrome) + { _imageMonochrome = animation.imagesMonochrome[0]; _imageColor = NULL; } @@ -97,7 +105,8 @@ void RenderComponent::setAnimation(const Animation& animation) void RenderComponent::removeAnimation() { - if (_animation) { + if (_animation) + { _animation = NULL; timer.deleteTimeout(_animationTimeout); } diff --git a/src/engine/components/RenderComponent.h b/src/engine/components/RenderComponent.h index de3151c..88a0726 100644 --- a/src/engine/components/RenderComponent.h +++ b/src/engine/components/RenderComponent.h @@ -12,14 +12,14 @@ class Entity; -//typedef uint16_t* ImageColor; -//typedef uint8_t* ImageMonochrome; +// typedef uint16_t* ImageColor; +// typedef uint8_t* ImageMonochrome; -// Alternative for animation +// Alternative for animation //#ifdef ANIMATION_FRAME_ALTERNATIVE -//struct AnimationFrame { - //const uint8_t* image; - //const uint32_t duration; +// struct AnimationFrame { +// const uint8_t* image; +// const uint32_t duration; //}; //#endif // Example of the alternative: @@ -27,66 +27,72 @@ class Entity; // const AnimationFrame monsterFrames[numberFrames] = { AnimationFrame(monsterImageOne, 100), AnimationFrame(monsterImageTwo, 200) }; // const Animation monsterAnimation(monsterFrames, numberFrames); -struct Animation { - //const AnimationFrame* frames; - const uint8_t** imagesMonochrome; - const uint16_t** imagesColor; - const uint32_t* times; +struct Animation +{ + // const AnimationFrame* frames; + const uint8_t **imagesMonochrome; + const uint16_t **imagesColor; + const uint32_t *times; uint8_t length; Animation() : imagesMonochrome(NULL), imagesColor(NULL), times(NULL), length(0) {} - Animation(const uint8_t** img, const uint32_t* tms, uint8_t len) : imagesMonochrome(img), imagesColor(NULL), times(tms), length(len) {} - Animation(const uint16_t** img, const uint32_t* tms, uint8_t len) : imagesMonochrome(NULL), imagesColor(img), times(tms), length(len) {} + Animation(const uint8_t **img, const uint32_t *tms, uint8_t len) : imagesMonochrome(img), imagesColor(NULL), times(tms), length(len) {} + Animation(const uint16_t **img, const uint32_t *tms, uint8_t len) : imagesMonochrome(NULL), imagesColor(img), times(tms), length(len) {} }; -class RenderComponent : public Poolable { - public: - RenderComponent(); - virtual ~RenderComponent(); +class RenderComponent : public Poolable +{ +public: + RenderComponent(); + virtual ~RenderComponent(); - void render(); - //void update(); + void render(); + // void update(); - void setImage(const uint8_t* image); - void setImage(const uint16_t* image); - void setAnimation(const Animation& animation); - void removeAnimation(); + void setImage(const uint8_t *image); + void setImage(const uint16_t *image); + void setAnimation(const Animation &animation); + void removeAnimation(); - protected: - friend class Entity; - void init(Entity* entity); - void init(Entity* entity, BodyComponent* bodyComponent); - void deinit(); +protected: + friend class Entity; + void init(Entity *entity); + void init(Entity *entity, BodyComponent *bodyComponent); + void deinit(); - private: - Entity* _entity; - BodyComponent* _bodyComponent; +private: + Entity *_entity; + BodyComponent *_bodyComponent; - const uint8_t* _imageMonochrome; - const uint16_t* _imageColor; - const Animation* _animation; - TimeoutId _animationTimeout; - uint8_t _animationCounter; + const uint8_t *_imageMonochrome; + const uint16_t *_imageColor; + const Animation *_animation; + TimeoutId _animationTimeout; + uint8_t _animationCounter; }; -class RenderComponentPool : public Pool { - public: - void render() +class RenderComponentPool : public Pool +{ +public: + void render() + { +#if defined(POOL_DOUBLE_LINK) + auto component = getItems(); + while (component) { -#if defined (POOL_DOUBLE_LINK) - auto component = getItems(); - while (component) { - component->render(); - component = static_cast(component->getNext()); - } + component->render(); + component = static_cast(component->getNext()); + } #else - for (int i = 0; i < RENDER_COMPONENTS_PER_WORLD; i++) { - if (_pool[i].isAllocated()) { - _pool[i].render(); - } + for (int i = 0; i < RENDER_COMPONENTS_PER_WORLD; i++) + { + if (_pool[i].isAllocated()) + { + _pool[i].render(); } -#endif } +#endif + } }; -#endif +#endif diff --git a/src/engine/components/StateComponent.h b/src/engine/components/StateComponent.h index c548d74..41b31a5 100644 --- a/src/engine/components/StateComponent.h +++ b/src/engine/components/StateComponent.h @@ -12,47 +12,48 @@ #include "Common.h" #include "engine/Pool.h" -// API functions: monster.getState(MONSTER_LIFE), where an enum MONSTER_STATE() lists -// indexes of state variables +// API functions: monster.getState(MONSTER_LIFE), where an enum MONSTER_STATE() lists +// indexes of state variables // This component should be templated with some macros to ensure more freedom -// Or provide multiple already defined state components +// Or provide multiple already defined state components class Entity; template -class StateComponent : public Poolable { - public: - StateComponent() : _entity(NULL) {} - virtual ~StateComponent() {} - - T getState(uint8_t index) - { - // if (index >= STATES_FOR_ENTITY) { //out of bound } - return _state[index]; - } - - void setState(uint8_t index, T value) - { - _state[index] = value; - } - - protected: - friend class Entity; - void init(Entity* entity) { _entity = entity; } - void deinit() { _entity = NULL; } - - private: - Entity* _entity; - T _state[N]; +class StateComponent : public Poolable +{ +public: + StateComponent() : _entity(NULL) {} + virtual ~StateComponent() {} + + T getState(uint8_t index) + { + // if (index >= STATES_FOR_ENTITY) { //out of bound } + return _state[index]; + } + + void setState(uint8_t index, T value) + { + _state[index] = value; + } + +protected: + friend class Entity; + void init(Entity *entity) { _entity = entity; } + void deinit() { _entity = NULL; } + +private: + Entity *_entity; + T _state[N]; }; -//class IntStateComponentPool : public Pool, STATE_COMPONENTS_PER_WORLD> { +// class IntStateComponentPool : public Pool, STATE_COMPONENTS_PER_WORLD> { typedef StateComponent IntStateComponent; -class IntStateComponentPool : public Pool { - public: - void update() {} +class IntStateComponentPool : public Pool +{ +public: + void update() {} }; - -#endif +#endif diff --git a/src/engine/components/TimeComponent.h b/src/engine/components/TimeComponent.h index 6f55f7d..acb96ed 100644 --- a/src/engine/components/TimeComponent.h +++ b/src/engine/components/TimeComponent.h @@ -11,57 +11,62 @@ class Entity; -class TimeComponent { - public: - TimeComponent() {} - ~TimeComponent() {} +class TimeComponent +{ +public: + TimeComponent() {} + ~TimeComponent() {} - // TODO: Check whether passed indexes are < TIMEOUTS_FOR_ENTITY - // (i.e. from 0 to TIMEOUTS_FOR_ENTITY) + // TODO: Check whether passed indexes are < TIMEOUTS_FOR_ENTITY + // (i.e. from 0 to TIMEOUTS_FOR_ENTITY) - void init() + void init() + { + for (int i = 0; i < TIMEOUTS_FOR_ENTITY; i++) { - for (int i = 0; i < TIMEOUTS_FOR_ENTITY; i++) { - _timeouts[i] = 0; - } + _timeouts[i] = 0; } + } - void init(uint8_t index, uint64_t time) - { - if (!_timeouts[index]) - _timeouts[index] = timer.newTimeout(time); - else - timer.setTimeout(_timeouts[index], time); - } + void init(uint8_t index, uint64_t time) + { + if (!_timeouts[index]) + _timeouts[index] = timer.newTimeout(time); + else + timer.setTimeout(_timeouts[index], time); + } - void deinit() - { - for (int i = 0; i < TIMEOUTS_FOR_ENTITY; i++) { - if (_timeouts[i] > 0) timer.deleteTimeout(_timeouts[i]); - } + void deinit() + { + for (int i = 0; i < TIMEOUTS_FOR_ENTITY; i++) + { + if (_timeouts[i] > 0) + timer.deleteTimeout(_timeouts[i]); } + } - void deinit(uint8_t index) - { - if (_timeouts[index] > 0) timer.deleteTimeout(_timeouts[index]); - } + void deinit(uint8_t index) + { + if (_timeouts[index] > 0) + timer.deleteTimeout(_timeouts[index]); + } - bool checkTimeout(uint8_t index) + bool checkTimeout(uint8_t index) + { + if (_timeouts[index] > 0) { - if (_timeouts[index] > 0) { - return timer.checkTimeout(_timeouts[index]); - } - return false; + return timer.checkTimeout(_timeouts[index]); } + return false; + } - TimeoutId getTimeout(uint8_t index) - { - return _timeouts[index]; - } + TimeoutId getTimeout(uint8_t index) + { + return _timeouts[index]; + } - private: - TimeoutId _timeouts[TIMEOUTS_FOR_ENTITY]; +private: + TimeoutId _timeouts[TIMEOUTS_FOR_ENTITY]; }; - -#endif +#endif diff --git a/src/engine/components/UpdateComponent.cpp b/src/engine/components/UpdateComponent.cpp index 5b0dce5..9f4fa1d 100644 --- a/src/engine/components/UpdateComponent.cpp +++ b/src/engine/components/UpdateComponent.cpp @@ -2,9 +2,8 @@ #include "engine/Entity.h" #include "engine/PEntity.h" -UpdateComponent::UpdateComponent() : - _entity(NULL), - _update(NULL) +UpdateComponent::UpdateComponent() : _entity(NULL), + _update(NULL) { } @@ -12,7 +11,7 @@ UpdateComponent::~UpdateComponent() { } -void UpdateComponent::init(Entity* entity, UpdateCallback callback) +void UpdateComponent::init(Entity *entity, UpdateCallback callback) { _entity = entity; _update = callback; @@ -31,16 +30,19 @@ void UpdateComponent::deinit() void UpdateComponentPool::update() { -#if defined (POOL_DOUBLE_LINK) +#if defined(POOL_DOUBLE_LINK) auto component = getItems(); - while (component) { - auto nextComponent = static_cast(component->getNext()); + while (component) + { + auto nextComponent = static_cast(component->getNext()); component->_update(PEntity(component->_entity)); component = nextComponent; } #else - for (int i = 0; i < UPDATE_COMPONENTS_PER_WORLD; i++) { - if (_pool[i].isAllocated()) { + for (int i = 0; i < UPDATE_COMPONENTS_PER_WORLD; i++) + { + if (_pool[i].isAllocated()) + { _pool[i]._update(PEntity(_pool[i]._entity)); } } diff --git a/src/engine/components/UpdateComponent.h b/src/engine/components/UpdateComponent.h index b13b3eb..434f8f0 100644 --- a/src/engine/components/UpdateComponent.h +++ b/src/engine/components/UpdateComponent.h @@ -13,27 +13,29 @@ class PEntity; typedef void (*UpdateCallback)(PEntity entity); -class UpdateComponent : public Poolable { - public: - UpdateComponent(); - virtual ~UpdateComponent(); - - void config(UpdateCallback callback); - - protected: - friend class Entity; - void init(Entity* entity, UpdateCallback callback); - void deinit(); - - private: - friend class UpdateComponentPool; - Entity* _entity; - UpdateCallback _update; +class UpdateComponent : public Poolable +{ +public: + UpdateComponent(); + virtual ~UpdateComponent(); + + void config(UpdateCallback callback); + +protected: + friend class Entity; + void init(Entity *entity, UpdateCallback callback); + void deinit(); + +private: + friend class UpdateComponentPool; + Entity *_entity; + UpdateCallback _update; }; -class UpdateComponentPool : public Pool { - public: - void update(); +class UpdateComponentPool : public Pool +{ +public: + void update(); }; -#endif +#endif diff --git a/src/hardware/HwDisplay.h b/src/hardware/HwDisplay.h index 07ff5b3..da5abce 100644 --- a/src/hardware/HwDisplay.h +++ b/src/hardware/HwDisplay.h @@ -13,8 +13,8 @@ void hwDisplaySend(); void hwDisplayDraw(uint8_t x, uint8_t y, DisplayColor color); void hwDisplayDrawRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, DisplayColor color); -void hwDisplayDrawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t* image); -void hwDisplayDrawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16_t* image); +void hwDisplayDrawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *image); +void hwDisplayDrawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16_t *image); void hwDisplayFill(DisplayColor color); uint8_t hwDisplayWidth(); diff --git a/src/hardware/HwJoystick.h b/src/hardware/HwJoystick.h index 46a4a96..b7bf095 100644 --- a/src/hardware/HwJoystick.h +++ b/src/hardware/HwJoystick.h @@ -5,7 +5,8 @@ typedef uint8_t ButtonStateType; -enum JoystickButton { +enum JoystickButton +{ ButtonA = 0, ButtonB = 1, ButtonC = 2, diff --git a/src/hardware/HwNotePlayer.h b/src/hardware/HwNotePlayer.h index 57da518..277c3aa 100644 --- a/src/hardware/HwNotePlayer.h +++ b/src/hardware/HwNotePlayer.h @@ -6,7 +6,8 @@ #define LOOP_DISABLED 0 #define LOOP_ENABLED 1 -enum NoteFrequency { +enum NoteFrequency +{ DO_NOTE = 0, RE_NOTE = 1, MI_NOTE = 2, @@ -15,7 +16,7 @@ enum NoteFrequency { LA_NOTE = 5, SI_NOTE = 6, C_NOTE = DO_NOTE, - D_NOTE = RE_NOTE, + D_NOTE = RE_NOTE, E_NOTE = MI_NOTE, F_NOTE = FA_NOTE, G_NOTE = SOL_NOTE, @@ -34,9 +35,8 @@ struct Song { Note *notes; uint16_t size; - Song(Note *notes, uint16_t size) : - notes(notes), - size(size) + Song(Note *notes, uint16_t size) : notes(notes), + size(size) { } }; @@ -47,6 +47,6 @@ void hwNotePlayerStart(); void hwNotePlayerStop(); void hwNotePlayerVolume(uint8_t volume); -void hwNotePlayerSong(Note* notes, uint16_t size, uint16_t startIndex, bool loopEn); +void hwNotePlayerSong(Note *notes, uint16_t size, uint16_t startIndex, bool loopEn); #endif /* HW_NOTE_PLAYER_H_ */ diff --git a/src/target/Arduino/nano33ble/HwDisplay-Lcd.cpp b/src/target/Arduino/nano33ble/HwDisplay-Lcd.cpp index dbbee95..b779c34 100644 --- a/src/target/Arduino/nano33ble/HwDisplay-Lcd.cpp +++ b/src/target/Arduino/nano33ble/HwDisplay-Lcd.cpp @@ -1,5 +1,5 @@ #include "hardware/HwConfiguration.h" -#if defined (ARDUINO_NANO33BLE) +#if defined(ARDUINO_NANO33BLE) #if defined(DISPLAY_ST7735) @@ -16,16 +16,18 @@ uint16_t displayBuffer[DISPLAY_LENGTH] = {0}; void hwDisplayDraw(uint8_t x, uint8_t y, DisplayColor color) { - if (color == BLACK_COLOR) return; + if (color == BLACK_COLOR) + return; // Check display boundaries - if (x >= DISPLAY_WIDTH || y >= DISPLAY_HEIGHT) return; + if (x >= DISPLAY_WIDTH || y >= DISPLAY_HEIGHT) + return; -#if defined (DISPLAY_ASCENDING_Y) +#if defined(DISPLAY_ASCENDING_Y) uint16_t index = ((DISPLAY_HEIGHT - 1 - y) * DISPLAY_WIDTH + x); displayBuffer[index] = color; -#else +#else uint16_t index = (y * DISPLAY_WIDTH + x); displayBuffer[index] = color; #endif @@ -34,30 +36,34 @@ void hwDisplayDraw(uint8_t x, uint8_t y, DisplayColor color) void hwDisplayDrawRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, DisplayColor color) { // Just a proof of concept (highly inefficient) - for (int i = 0 ; i < w; i++) { - for (int j = 0; j < h; j++) { - hwDisplayDraw( (x - w / 2 + i), (y - h / 2 + j), color); + for (int i = 0; i < w; i++) + { + for (int j = 0; j < h; j++) + { + hwDisplayDraw((x - w / 2 + i), (y - h / 2 + j), color); } } } -void hwDisplayDrawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16_t* image) +void hwDisplayDrawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16_t *image) { w = *(image++); h = *(image++); - for (int j = 0; j < h; j++) { - for (int i = 0 ; i < w; i++) { -#if defined (DISPLAY_ASCENDING_Y) - hwDisplayDraw( (x - w / 2 + i), (y + h / 2 - j), *(image++)); + for (int j = 0; j < h; j++) + { + for (int i = 0; i < w; i++) + { +#if defined(DISPLAY_ASCENDING_Y) + hwDisplayDraw((x - w / 2 + i), (y + h / 2 - j), *(image++)); #else - hwDisplayDraw( (x - w / 2 + i), (y - h / 2 + j), *(image++)); + hwDisplayDraw((x - w / 2 + i), (y - h / 2 + j), *(image++)); #endif } } #if 0 // This seems way faster, but profiler doesn't agree - // TODO: perform more significative tests with many images rendered -#if defined (DISPLAY_ASCENDING_Y) + // TODO: perform more significative tests with many images rendered +#if defined(DISPLAY_ASCENDING_Y) // TODO: check boundaries and trim the image accordingly y = DISPLAY_HEIGHT - 1 - y; @@ -69,20 +75,23 @@ void hwDisplayDrawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16 #endif } -void hwDisplayDrawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t* image) +void hwDisplayDrawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *image) { -#if defined (DISPLAY_ASCENDING_Y) +#if defined(DISPLAY_ASCENDING_Y) w = *(image++); h = *(image++); uint8_t imageX = x - w / 2; uint8_t imageY = y + h / 2; - for (uint8_t j = 0; j < h / 8; j++) { - for (uint8_t i = 0; i < w; i++) { + for (uint8_t j = 0; j < h / 8; j++) + { + for (uint8_t i = 0; i < w; i++) + { uint8_t vLine = *(image++); - for (uint8_t k = 0; k < 8; k++) { + for (uint8_t k = 0; k < 8; k++) + { DisplayColor pixel = vLine & (0x01 << k) ? WHITE_COLOR : BLACK_COLOR; - hwDisplayDraw(imageX + i, imageY - k, pixel); + hwDisplayDraw(imageX + i, imageY - k, pixel); } } imageY -= 8; @@ -92,7 +101,7 @@ void hwDisplayDrawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_ void hwDisplayFill(DisplayColor color) { - for(int index = 0; index < DISPLAY_LENGTH; index++) + for (int index = 0; index < DISPLAY_LENGTH; index++) { displayBuffer[index] = color; } @@ -100,12 +109,12 @@ void hwDisplayFill(DisplayColor color) uint8_t hwDisplayWidth() { - return DISPLAY_WIDTH; + return DISPLAY_WIDTH; } uint8_t hwDisplayHeight() { - return DISPLAY_HEIGHT; + return DISPLAY_HEIGHT; } #endif diff --git a/src/target/Arduino/nano33ble/HwDisplay-Oled.cpp b/src/target/Arduino/nano33ble/HwDisplay-Oled.cpp index 8d6658a..7a3cc6f 100644 --- a/src/target/Arduino/nano33ble/HwDisplay-Oled.cpp +++ b/src/target/Arduino/nano33ble/HwDisplay-Oled.cpp @@ -1,7 +1,7 @@ #include "hardware/HwConfiguration.h" -#if defined (ARDUINO_NANO33BLE) +#if defined(ARDUINO_NANO33BLE) -#if defined (DISPLAY_SSD1306) || defined (DISPLAY_SH1106) +#if defined(DISPLAY_SSD1306) || defined(DISPLAY_SH1106) #include "hardware/HwDisplay.h" #include "Arduino.h" @@ -17,51 +17,57 @@ uint8_t displayBuffer[DISPLAY_LENGTH]; void hwDisplayDraw(uint8_t x, uint8_t y, DisplayColor color) { // Check display boundaries - if (x >= DISPLAY_WIDTH || y >= DISPLAY_HEIGHT) return; + if (x >= DISPLAY_WIDTH || y >= DISPLAY_HEIGHT) + return; -#if defined (DISPLAY_ASCENDING_Y) +#if defined(DISPLAY_ASCENDING_Y) uint8_t row = (7 - (uint8_t)y / 8); if (color == WHITE_COLOR) - displayBuffer[(row*128) + (uint8_t)x] |= 1 << (7 - ((uint8_t)y % 8)); + displayBuffer[(row * 128) + (uint8_t)x] |= 1 << (7 - ((uint8_t)y % 8)); else if (color == BLACK_COLOR) - displayBuffer[(row*128) + (uint8_t)x] &= ~ ( 1 << (7 - (uint8_t)y % 8) ); + displayBuffer[(row * 128) + (uint8_t)x] &= ~(1 << (7 - (uint8_t)y % 8)); else if (color == 2) - displayBuffer[(row*128) + (uint8_t)x] ^= ( 1 << (7 - (uint8_t)y % 8) ); -#else + displayBuffer[(row * 128) + (uint8_t)x] ^= (1 << (7 - (uint8_t)y % 8)); +#else uint8_t row = (uint8_t)y / 8; if (color == WHITE_COLOR) - displayBuffer[(row*128) + (uint8_t)x] |= 1 << ((uint8_t)y % 8); + displayBuffer[(row * 128) + (uint8_t)x] |= 1 << ((uint8_t)y % 8); else if (color == BLACK_COLOR) - displayBuffer[(row*128) + (uint8_t)x] &= ~ ( 1 << ((uint8_t)y % 8) ); + displayBuffer[(row * 128) + (uint8_t)x] &= ~(1 << ((uint8_t)y % 8)); #endif } void hwDisplayDrawRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, DisplayColor color) { // Just a proof of concept (highly inefficient) - for (int i = 0 ; i < w; i++) { - for (int j = 0; j < h; j++) { - hwDisplayDraw( (x - w / 2 + i), (y - h / 2 + j), color); + for (int i = 0; i < w; i++) + { + for (int j = 0; j < h; j++) + { + hwDisplayDraw((x - w / 2 + i), (y - h / 2 + j), color); } } } -void hwDisplayDrawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t* image) +void hwDisplayDrawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *image) { // TODO: check boundaries -#if defined (DISPLAY_ASCENDING_Y) +#if defined(DISPLAY_ASCENDING_Y) uint16_t index = 0; w = image[index++]; h = image[index++]; uint8_t imageX = x - w / 2; uint8_t imageY = y + h / 2; - for (uint8_t j = 0; j < h / 8; j++) { - for (uint8_t i = 0; i < w; i++) { + for (uint8_t j = 0; j < h / 8; j++) + { + for (uint8_t i = 0; i < w; i++) + { uint8_t vLine = image[index++]; - for (uint8_t k = 0; k < 8; k++) { + for (uint8_t k = 0; k < 8; k++) + { DisplayColor pixel = vLine & (0x01 << k) ? WHITE_COLOR : BLACK_COLOR; - hwDisplayDraw(imageX + i, imageY - k, pixel); + hwDisplayDraw(imageX + i, imageY - k, pixel); } } imageY -= 8; @@ -69,7 +75,7 @@ void hwDisplayDrawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_ #endif } -void hwDisplayDrawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16_t* image) +void hwDisplayDrawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16_t *image) { // Color images are not supported on monochrome displays w = *(image++); @@ -80,8 +86,9 @@ void hwDisplayDrawImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16 void hwDisplayFill(DisplayColor color) { uint8_t pixels = 0x00; - if (color == WHITE_COLOR) pixels = 0xFF; - for(int index = 0; index < DISPLAY_LENGTH; index++) + if (color == WHITE_COLOR) + pixels = 0xFF; + for (int index = 0; index < DISPLAY_LENGTH; index++) { displayBuffer[index] = pixels; } @@ -89,12 +96,12 @@ void hwDisplayFill(DisplayColor color) uint8_t hwDisplayWidth() { - return DISPLAY_WIDTH; + return DISPLAY_WIDTH; } uint8_t hwDisplayHeight() { - return DISPLAY_HEIGHT; + return DISPLAY_HEIGHT; } #endif diff --git a/src/target/Arduino/nano33ble/HwDisplayI2C-Oled.cpp b/src/target/Arduino/nano33ble/HwDisplayI2C-Oled.cpp index 2246ec4..6a00813 100644 --- a/src/target/Arduino/nano33ble/HwDisplayI2C-Oled.cpp +++ b/src/target/Arduino/nano33ble/HwDisplayI2C-Oled.cpp @@ -1,9 +1,9 @@ #include "hardware/HwConfiguration.h" -#if defined (ARDUINO_NANO33BLE) +#if defined(ARDUINO_NANO33BLE) -#if defined (DISPLAY_SSD1306) || defined (DISPLAY_SH1106) +#if defined(DISPLAY_SSD1306) || defined(DISPLAY_SH1106) -#if defined (DISPLAY_I2C) +#if defined(DISPLAY_I2C) #include "hardware/HwDisplay.h" #include "Arduino.h" @@ -11,10 +11,10 @@ #define DISPLAY_ADDRESS (0x3C) -#if defined (DISPLAY_SSD1306) +#if defined(DISPLAY_SSD1306) #define DISPLAY_COLUMNS (128) #define DISPLAY_STARTCOL (0) -#elif defined (DISPLAY_SH1106) +#elif defined(DISPLAY_SH1106) /* The screen is actually 130x64, so 2 cols must be cleared */ #define DISPLAY_COLUMNS (130) #define DISPLAY_STARTCOL (2) @@ -32,58 +32,59 @@ extern uint8_t displayBuffer[DISPLAY_LENGTH]; * Attention: display is initialize in OFF mode so that it can be cleared before its use */ static uint8_t INIT_COMMANDS[] = {0xAE, 0x20, 0x02, 0xB0, 0xC8, 0x00, - 0x10, 0x40, 0x81, 0xFF//0x3F testing contrast - , 0xA1, 0xA6, 0xA8, 0x3F, - 0xA4, 0xD3, 0x00, 0xD5, 0xF0, 0xD9, 0x22, 0xDA, 0x12, - 0xDB, 0x20, 0x8D, 0x14, 0xAF}; - + 0x10, 0x40, 0x81, 0xFF, // 0x3F testing contrast + 0xA1, 0xA6, 0xA8, 0x3F, + 0xA4, 0xD3, 0x00, 0xD5, 0xF0, 0xD9, 0x22, 0xDA, 0x12, + 0xDB, 0x20, 0x8D, 0x14, 0xAF}; void hwDisplaySetup() { - Wire.begin(); + Wire.begin(); Wire.setClock(10000000); delay(200); /* Send display configuration through I2C */ Wire.beginTransmission(DISPLAY_ADDRESS); - Wire.write((uint8_t)0x00); - Wire.write(INIT_COMMANDS, INIT_COMMANDS_SIZE); + Wire.write((uint8_t)0x00); + Wire.write(INIT_COMMANDS, INIT_COMMANDS_SIZE); Wire.endTransmission(); /* Clear the whole display */ uint8_t tempBuff[DISPLAY_COLUMNS]; - for(int index = 0; index < DISPLAY_COLUMNS; index++) + for (int index = 0; index < DISPLAY_COLUMNS; index++) { tempBuff[index] = 0; - } + } /* Send the black screen to clear the display */ - for(int page = 0; page < 8; page++) { + for (int page = 0; page < 8; page++) + { uint8_t setPageCmd[] = {0xB0 + page, 0x00, 0x10}; Wire.beginTransmission(DISPLAY_ADDRESS); - Wire.write((uint8_t)0x00); - Wire.write(setPageCmd, 3); + Wire.write((uint8_t)0x00); + Wire.write(setPageCmd, 3); Wire.endTransmission(); Wire.beginTransmission(DISPLAY_ADDRESS); - Wire.write((uint8_t)0x40); - Wire.write(tempBuff, DISPLAY_COLUMNS); + Wire.write((uint8_t)0x40); + Wire.write(tempBuff, DISPLAY_COLUMNS); Wire.endTransmission(); } } void hwDisplaySend() { - for(int page = 0; page < 8; page++) { + for (int page = 0; page < 8; page++) + { uint8_t setPageCmd[] = {0xB0 + page, DISPLAY_STARTCOL, 0x10}; Wire.beginTransmission(DISPLAY_ADDRESS); - Wire.write((uint8_t)0x00); - Wire.write(setPageCmd, 3); + Wire.write((uint8_t)0x00); + Wire.write(setPageCmd, 3); Wire.endTransmission(); Wire.beginTransmission(DISPLAY_ADDRESS); - Wire.write((uint8_t)0x40); - Wire.write(displayBuffer + (page * 128), 128); + Wire.write((uint8_t)0x40); + Wire.write(displayBuffer + (page * 128), 128); Wire.endTransmission(); } } diff --git a/src/target/Arduino/nano33ble/HwDisplaySPI-Lcd.cpp b/src/target/Arduino/nano33ble/HwDisplaySPI-Lcd.cpp index 5173f8d..556bf23 100644 --- a/src/target/Arduino/nano33ble/HwDisplaySPI-Lcd.cpp +++ b/src/target/Arduino/nano33ble/HwDisplaySPI-Lcd.cpp @@ -1,5 +1,5 @@ #include "hardware/HwConfiguration.h" -#if defined (ARDUINO_NANO33BLE) +#if defined(ARDUINO_NANO33BLE) #if defined(DISPLAY_ST7735) @@ -10,69 +10,70 @@ #define DISPLAY_HEIGHT 160 #define DISPLAY_LENGTH (DISPLAY_WIDTH * DISPLAY_HEIGHT) -#define CS 9 -#define DC 10 +#define CS 9 +#define DC 10 #define SPI_DATA_MODE() (digitalWrite(DC, HIGH)) #define SPI_COMMAND_MODE() (digitalWrite(DC, LOW)) #include -#define NRF_SPIM (NRF_SPIM_Type *)NRF_SPIM3 +#define NRF_SPIM (NRF_SPIM_Type *)NRF_SPIM3 -struct Command { +struct Command +{ uint8_t opcode; uint8_t dataLength; - uint8_t* data; + uint8_t *data; uint16_t delay; }; -static uint16_t INIT_COMMANDS[] = -{ - 0x3a, 1, 0x05, 0, // pixel format RGB565 - 0xe0, 16, // Gamma Adjustments - 0x09, 0x16, 0x09, 0x20, - 0x21, 0x1B, 0x13, 0x19, - 0x17, 0x15, 0x1E, 0x2B, - 0x04, 0x05, 0x02, 0x0E, - 0, - 0xe1, 16, // Gamma Adjustments - 0x0B, 0x14, 0x08, 0x1E, - 0x22, 0x1D, 0x18, 0x1E, - 0x1B, 0x1A, 0x24, 0x2B, - 0x06, 0x06, 0x02, 0x0F, - 10, - 0x36, 1, 0xc8, 0, // Memory data access control - 0x11, 0, 120, // Sleep out - 0x29, 0, 10, // Display on - 0x2a, 1, 0x00, 0, // Column address set - 0x2b, 1, 0x00, 0, // Row address set - 0x2c, 0, 0 // Memory write +static uint16_t INIT_COMMANDS[] = + { + 0x3a, 1, 0x05, 0, // pixel format RGB565 + 0xe0, 16, // Gamma Adjustments + 0x09, 0x16, 0x09, 0x20, + 0x21, 0x1B, 0x13, 0x19, + 0x17, 0x15, 0x1E, 0x2B, + 0x04, 0x05, 0x02, 0x0E, + 0, + 0xe1, 16, // Gamma Adjustments + 0x0B, 0x14, 0x08, 0x1E, + 0x22, 0x1D, 0x18, 0x1E, + 0x1B, 0x1A, 0x24, 0x2B, + 0x06, 0x06, 0x02, 0x0F, + 10, + 0x36, 1, 0xc8, 0, // Memory data access control + 0x11, 0, 120, // Sleep out + 0x29, 0, 10, // Display on + 0x2a, 1, 0x00, 0, // Column address set + 0x2b, 1, 0x00, 0, // Row address set + 0x2c, 0, 0 // Memory write }; static uint8_t INIT_COMMANDS_LENGTH = 9; extern uint16_t displayBuffer[DISPLAY_LENGTH]; -static void sendCommand(Command& cmd); +static void sendCommand(Command &cmd); void spiInit(uint32_t sck, uint32_t mosi, uint32_t miso) { nrf_gpio_pin_clear(mosi); - nrf_gpio_cfg( mosi, - NRF_GPIO_PIN_DIR_OUTPUT, - NRF_GPIO_PIN_INPUT_DISCONNECT, - NRF_GPIO_PIN_NOPULL, - // Use High Drive because SPIM3 is used - NRF_GPIO_PIN_H0H1, - NRF_GPIO_PIN_NOSENSE); - - nrf_gpio_cfg( sck, - NRF_GPIO_PIN_DIR_OUTPUT, - NRF_GPIO_PIN_INPUT_CONNECT, - NRF_GPIO_PIN_NOPULL, - // Use High Drive because SPIM3 is used - NRF_GPIO_PIN_H0H1, - NRF_GPIO_PIN_NOSENSE); + nrf_gpio_cfg(mosi, + NRF_GPIO_PIN_DIR_OUTPUT, + NRF_GPIO_PIN_INPUT_DISCONNECT, + NRF_GPIO_PIN_NOPULL, + // Use High Drive because SPIM3 is used + NRF_GPIO_PIN_H0H1, + NRF_GPIO_PIN_NOSENSE); + + nrf_gpio_cfg(sck, + NRF_GPIO_PIN_DIR_OUTPUT, + NRF_GPIO_PIN_INPUT_CONNECT, + NRF_GPIO_PIN_NOPULL, + // Use High Drive because SPIM3 is used + NRF_GPIO_PIN_H0H1, + NRF_GPIO_PIN_NOSENSE); nrf_spim_pins_set(NRF_SPIM, sck, mosi, 0xFF); nrf_spim_frequency_set(NRF_SPIM, NRF_SPIM_FREQ_32M); @@ -82,21 +83,23 @@ void spiInit(uint32_t sck, uint32_t mosi, uint32_t miso) nrf_spim_enable(NRF_SPIM); } -void spiTransfer(const uint8_t* data, uint32_t len) +void spiTransfer(const uint8_t *data, uint32_t len) { nrf_spim_event_clear(NRF_SPIM, NRF_SPIM_EVENT_ENDTX); nrf_spim_tx_buffer_set(NRF_SPIM, data, len); nrf_spim_task_trigger(NRF_SPIM, NRF_SPIM_TASK_START); - while (! nrf_spim_event_check(NRF_SPIM, NRF_SPIM_EVENT_ENDTX)) ; + while (!nrf_spim_event_check(NRF_SPIM, NRF_SPIM_EVENT_ENDTX)) + ; } -void sendCommand(Command& cmd) +void sendCommand(Command &cmd) { SPI_COMMAND_MODE(); spiTransfer(&cmd.opcode, 1); SPI_DATA_MODE(); spiTransfer(cmd.data, cmd.dataLength); - if (cmd.delay) delay(cmd.delay); + if (cmd.delay) + delay(cmd.delay); } void hwDisplaySetup() @@ -113,10 +116,12 @@ void hwDisplaySetup() cmd.data = ramData; uint8_t index = 0; - for (uint8_t i = 0; i < INIT_COMMANDS_LENGTH; i++) { + for (uint8_t i = 0; i < INIT_COMMANDS_LENGTH; i++) + { cmd.opcode = INIT_COMMANDS[index++]; cmd.dataLength = INIT_COMMANDS[index++]; - if (cmd.dataLength) { + if (cmd.dataLength) + { memcpy(ramData, &INIT_COMMANDS[index], cmd.dataLength); index += cmd.dataLength; } @@ -129,7 +134,7 @@ void hwDisplaySetup() void hwDisplaySend() { - spiTransfer((uint8_t*)displayBuffer, DISPLAY_LENGTH * 2); + spiTransfer((uint8_t *)displayBuffer, DISPLAY_LENGTH * 2); } #endif diff --git a/src/target/Arduino/nano33ble/HwDisplaySPI-Oled.cpp b/src/target/Arduino/nano33ble/HwDisplaySPI-Oled.cpp index 504917f..7e073ae 100644 --- a/src/target/Arduino/nano33ble/HwDisplaySPI-Oled.cpp +++ b/src/target/Arduino/nano33ble/HwDisplaySPI-Oled.cpp @@ -1,18 +1,18 @@ #include "hardware/HwConfiguration.h" -#if defined (ARDUINO_NANO33BLE) +#if defined(ARDUINO_NANO33BLE) -#if defined (DISPLAY_SPI) +#if defined(DISPLAY_SPI) -#if defined (DISPLAY_SSD1306) || defined (DISPLAY_SH1106) +#if defined(DISPLAY_SSD1306) || defined(DISPLAY_SH1106) #include "hardware/HwDisplay.h" #include "Arduino.h" #include "SPI.h" -#if defined (DISPLAY_SSD1306) +#if defined(DISPLAY_SSD1306) #define DISPLAY_COLUMNS (128) #define DISPLAY_STARTCOL (0) -#elif defined (DISPLAY_SH1106) +#elif defined(DISPLAY_SH1106) /* The screen is actually 130x64, so 2 cols must be cleared */ #define DISPLAY_COLUMNS (130) #define DISPLAY_STARTCOL (2) @@ -37,11 +37,10 @@ extern uint8_t displayBuffer[DISPLAY_LENGTH]; * Attention: display is initialize in OFF mode so that it can be cleared before its use */ static uint8_t INIT_COMMANDS[] = {0xAE, 0x20, 0x02, 0xB0, 0xC8, 0x00, - 0x10, 0x40, 0x81, 0xFF//0x3F testing contrast - , 0xA1, 0xA6, 0xA8, 0x3F, - 0xA4, 0xD3, 0x00, 0xD5, 0xF0, 0xD9, 0x22, 0xDA, 0x12, - 0xDB, 0x20, 0x8D, 0x14, 0xAF}; - + 0x10, 0x40, 0x81, 0xFF, // 0x3F testing contrast + 0xA1, 0xA6, 0xA8, 0x3F, + 0xA4, 0xD3, 0x00, 0xD5, 0xF0, 0xD9, 0x22, 0xDA, 0x12, + 0xDB, 0x20, 0x8D, 0x14, 0xAF}; void hwDisplaySetup() { @@ -53,18 +52,19 @@ void hwDisplaySetup() digitalWrite(RESET_PIN, LOW); digitalWrite(RESET_PIN, HIGH); - // Send display configuration through SPI + // Send display configuration through SPI SPI_COMMAND_MODE(); SPI.transfer(INIT_COMMANDS, INIT_COMMANDS_SIZE); - // Clear the whole display + // Clear the whole display uint8_t tempBuff[DISPLAY_COLUMNS]; - for(int index = 0; index < DISPLAY_COLUMNS; index++) + for (int index = 0; index < DISPLAY_COLUMNS; index++) { tempBuff[index] = 0; - } + } /* Send the black screen to clear the display */ - for(int page = 0; page < 8; page++) { + for (int page = 0; page < 8; page++) + { uint8_t setPageCmd[] = {0xB0 + page, 0x00, 0x10}; SPI_COMMAND_MODE(); SPI.transfer(setPageCmd, 3); @@ -76,7 +76,8 @@ void hwDisplaySetup() void hwDisplaySend() { - for(int page = 0; page < 8; page++) { + for (int page = 0; page < 8; page++) + { uint8_t setPageCmd[] = {0xB0 + page, DISPLAY_STARTCOL, 0x10}; SPI_COMMAND_MODE(); SPI.transfer(setPageCmd, 3); diff --git a/src/target/Arduino/nano33ble/HwJoystick.cpp b/src/target/Arduino/nano33ble/HwJoystick.cpp index 2635923..93a0d14 100644 --- a/src/target/Arduino/nano33ble/HwJoystick.cpp +++ b/src/target/Arduino/nano33ble/HwJoystick.cpp @@ -1,5 +1,5 @@ #include "hardware/HwConfiguration.h" -#if defined (ARDUINO_NANO33BLE) +#if defined(ARDUINO_NANO33BLE) #include "hardware/HwJoystick.h" #include "Arduino.h" @@ -33,43 +33,43 @@ static void pollButtons() static inline void readButton(JoystickButton button, uint8_t buttonPin, uint8_t &buttonCount) { ButtonStateType tempButtonState = digitalRead(buttonPin); - //buttonCount = (buttonCount << 1) | tempButtonState; + // buttonCount = (buttonCount << 1) | tempButtonState; -#if defined (SLOW_DEBOUNCE) - if ( (buttonCount | (uint8_t)(~DEBOUNCE_MASK)) == 0xFF) +#if defined(SLOW_DEBOUNCE) + if ((buttonCount | (uint8_t)(~DEBOUNCE_MASK)) == 0xFF) buttonState |= (1U << button); - else if ( (buttonCount & (uint8_t)(DEBOUNCE_MASK)) == 0x00) - buttonState &= ~ (1U << button); -#elif defined (FAST_DEBOUNCE) + else if ((buttonCount & (uint8_t)(DEBOUNCE_MASK)) == 0x00) + buttonState &= ~(1U << button); +#elif defined(FAST_DEBOUNCE) if (buttonCount == 0xFF) buttonState |= (1U << button); else if (buttonCount == 0x00) - buttonState &= ~ (1U << button); -#elif defined (NO_DEBOUNCE) + buttonState &= ~(1U << button); +#elif defined(NO_DEBOUNCE) if (tempButtonState) buttonState |= (1U << button); else - buttonState &= ~ (1U << button); + buttonState &= ~(1U << button); #endif } void hwJoystickSetup() { - buttonState = 0x00; + buttonState = 0x00; pinMode(BUTTON_A_PIN, INPUT); -#if defined (SLOW_DEBOUNCE) +#if defined(SLOW_DEBOUNCE) debounceTimer.attach(pollButtons, 8ms); -#elif defined (FAST_DEBOUNCE) - debounceTimer.attach(pollButtons, 4ms); +#elif defined(FAST_DEBOUNCE) + debounceTimer.attach(pollButtons, 4ms); #endif } ButtonStateType hwJoystickState() { -#if defined (NO_DEBOUNCE) +#if defined(NO_DEBOUNCE) pollButtons(); #endif - return buttonState; + return buttonState; } #endif diff --git a/src/target/Arduino/nano33ble/HwNotePlayer.cpp b/src/target/Arduino/nano33ble/HwNotePlayer.cpp index a7e6fe1..400c48d 100644 --- a/src/target/Arduino/nano33ble/HwNotePlayer.cpp +++ b/src/target/Arduino/nano33ble/HwNotePlayer.cpp @@ -1,42 +1,44 @@ #include "hardware/HwConfiguration.h" -#if defined (ARDUINO_NANO33BLE) +#if defined(ARDUINO_NANO33BLE) #include "hardware/HwNotePlayer.h" #include "Arduino.h" #define NOTE_PIN 3 -Note* notesList; +Note *notesList; uint16_t notesSize; uint16_t notesIndex; uint16_t notesStartIndex; bool notesLoop; -//mbed::PwmOut* pwm = digitalPinToPwm(NOTE_PIN); +// mbed::PwmOut* pwm = digitalPinToPwm(NOTE_PIN); void hwNotePlayerSetup() -{} +{ +} void playNextNote() { - //if (notesList != NULL) { - - //// end - //if (notesIndex > notesSize) { - //if (notesLoop) { - //notesIndex = notesStartIndex; - //} else { - ////notesList = NULL; - //hwNotePlayerStop(); - //} - //} - ////tone(NOTE_PIN, ); + // if (notesList != NULL) { + + //// end + // if (notesIndex > notesSize) { + // if (notesLoop) { + // notesIndex = notesStartIndex; + //} else { + ////notesList = NULL; + // hwNotePlayerStop(); + //} + //} + ////tone(NOTE_PIN, ); //} } void hwNotePlayerStart() { - if (notesList != NULL) { + if (notesList != NULL) + { playNextNote(); } @@ -44,16 +46,16 @@ void hwNotePlayerStart() void hwNotePlayerStop() { - //noTone(NOTE_PIN); + // noTone(NOTE_PIN); } void hwNotePlayerVolume(uint8_t volume) -{} +{ +} -void hwNotePlayerSong(Note* notes, uint16_t size, uint16_t startIndex, bool loopEn) +void hwNotePlayerSong(Note *notes, uint16_t size, uint16_t startIndex, bool loopEn) { notesList = notes; } - #endif diff --git a/src/target/Arduino/nano33ble/HwRefresh.cpp b/src/target/Arduino/nano33ble/HwRefresh.cpp index cc29175..475e6c6 100644 --- a/src/target/Arduino/nano33ble/HwRefresh.cpp +++ b/src/target/Arduino/nano33ble/HwRefresh.cpp @@ -1,5 +1,5 @@ #include "hardware/HwConfiguration.h" -#if defined (ARDUINO_NANO33BLE) +#if defined(ARDUINO_NANO33BLE) #include "hardware/HwRefresh.h" #include "Arduino.h" @@ -12,7 +12,7 @@ void hwRefreshSetup() void hwRefreshWaitEndFrame() { - static const unsigned long timeout = FRAME_MSEC; + static const unsigned long timeout = FRAME_MSEC; static auto time = millis(); // arduino delay exploits mbed thread sleep state diff --git a/src/target/STM32/duino/HwDisplay.cpp b/src/target/STM32/duino/HwDisplay.cpp index 7cc4ab1..03721d4 100644 --- a/src/target/STM32/duino/HwDisplay.cpp +++ b/src/target/STM32/duino/HwDisplay.cpp @@ -1,15 +1,15 @@ #include "hardware/HwConfiguration.h" -#if defined (_STM32_DUINO_) +#if defined(_STM32_DUINO_) #include "hardware/HwDisplay.h" #include #define DISPLAY_ADDRESS (0x3C) -#if defined (DISPLAY_SSD1306) +#if defined(DISPLAY_SSD1306) #define DISPLAY_COLUMNS (128) #define DISPLAY_STARTCOL (0) -#elif defined (DISPLAY_SH1106) +#elif defined(DISPLAY_SH1106) /* The screen is actually 130x64, so 2 cols must be cleared */ #define DISPLAY_COLUMNS (130) #define DISPLAY_STARTCOL (2) @@ -27,44 +27,45 @@ static uint8_t _buffer[DISPLAY_LENGTH]; * Attention: display is initialize in OFF mode so that it can be cleared before its use */ static uint8_t INIT_COMMANDS[] = {0xAE, 0x20, 0x02, 0xB0, 0xC8, 0x00, - 0x10, 0x40, 0x81, 0xFF//0x3F testing contrast - , 0xA1, 0xA6, 0xA8, 0x3F, - 0xA4, 0xD3, 0x00, 0xD5, 0xF0, 0xD9, 0x22, 0xDA, 0x12, - 0xDB, 0x20, 0x8D, 0x14, 0xAF}; - + 0x10, 0x40, 0x81, 0xFF // 0x3F testing contrast + , + 0xA1, 0xA6, 0xA8, 0x3F, + 0xA4, 0xD3, 0x00, 0xD5, 0xF0, 0xD9, 0x22, 0xDA, 0x12, + 0xDB, 0x20, 0x8D, 0x14, 0xAF}; void hwDisplaySetup() { #ifdef DISPLAY_I2C - Wire.begin(); + Wire.begin(); Wire.setClock(1000000); delay(200); /* Send display configuration through I2C */ Wire.beginTransmission(DISPLAY_ADDRESS); - Wire.write(0x00); - Wire.write(INIT_COMMANDS, INIT_COMMANDS_SIZE); + Wire.write(0x00); + Wire.write(INIT_COMMANDS, INIT_COMMANDS_SIZE); Wire.endTransmission(); /* Clear the whole display */ uint8_t tempBuff[DISPLAY_COLUMNS]; - for(int index = 0; index < DISPLAY_COLUMNS; index++) + for (int index = 0; index < DISPLAY_COLUMNS; index++) { tempBuff[index] = 0; - } + } /* Send the black screen to clear the display */ - for(int page = 0; page < 8; page++) { + for (int page = 0; page < 8; page++) + { uint8_t setPageCmd[] = {0xB0 + page, 0x00, 0x10}; Wire.beginTransmission(DISPLAY_ADDRESS); - Wire.write(0x00); - Wire.write(setPageCmd, 3); + Wire.write(0x00); + Wire.write(setPageCmd, 3); Wire.endTransmission(); Wire.beginTransmission(DISPLAY_ADDRESS); - Wire.write(0x40); - Wire.write(tempBuff, DISPLAY_COLUMNS); + Wire.write(0x40); + Wire.write(tempBuff, DISPLAY_COLUMNS); Wire.endTransmission(); } -#else //DISPLAY_I2C +#else // DISPLAY_I2C #endif } @@ -72,21 +73,22 @@ void hwDisplaySetup() void hwDisplaySend() { #ifdef DISPLAY_I2C - for(int page = 0; page < 8; page++) { + for (int page = 0; page < 8; page++) + { uint8_t setPageCmd[] = {0xB0 + page, DISPLAY_STARTCOL, 0x10}; Wire.beginTransmission(DISPLAY_ADDRESS); - Wire.write(0x00); - Wire.write(setPageCmd, 3); + Wire.write(0x00); + Wire.write(setPageCmd, 3); Wire.endTransmission(); Wire.beginTransmission(DISPLAY_ADDRESS); - Wire.write(0x40); - Wire.write(_buffer + (page * 128), 128); + Wire.write(0x40); + Wire.write(_buffer + (page * 128), 128); Wire.endTransmission(); } #else -#endif //DISPLAY_I2C +#endif // DISPLAY_I2C } void hwDisplayDraw(uint8_t x, uint8_t y, DisplayColor color) @@ -94,25 +96,25 @@ void hwDisplayDraw(uint8_t x, uint8_t y, DisplayColor color) #if defined(DISPLAY_ASCENDING_Y) uint8_t row = (7 - (uint8_t)y / 8); if (color == 0) - _buffer[(row*128) + (uint8_t)x] |= 1 << (7 - ((uint8_t)y % 8)); + _buffer[(row * 128) + (uint8_t)x] |= 1 << (7 - ((uint8_t)y % 8)); else if (color == 1) - _buffer[(row*128) + (uint8_t)x] &= ~ ( 1 << (7 - (uint8_t)y % 8) ); + _buffer[(row * 128) + (uint8_t)x] &= ~(1 << (7 - (uint8_t)y % 8)); else if (color == 2) - _buffer[(row*128) + (uint8_t)x] ^= ( 1 << (7 - (uint8_t)y % 8) ); -#else + _buffer[(row * 128) + (uint8_t)x] ^= (1 << (7 - (uint8_t)y % 8)); +#else uint8_t row = (uint8_t)y / 8; if (color == 0) - _buffer[(row*128) + (uint8_t)x] |= 1 << ((uint8_t)y % 8); + _buffer[(row * 128) + (uint8_t)x] |= 1 << ((uint8_t)y % 8); else if (color == 1) - _buffer[(row*128) + (uint8_t)x] &= ~ ( 1 << ((uint8_t)y % 8) ); + _buffer[(row * 128) + (uint8_t)x] &= ~(1 << ((uint8_t)y % 8)); else if (color == 2) - _buffer[(row*128) + (uint8_t)x] ^= ( 1 << ((uint8_t)y % 8) ); + _buffer[(row * 128) + (uint8_t)x] ^= (1 << ((uint8_t)y % 8)); #endif } void hwDisplayFill(DisplayColor color) { - for(int index = 0; index < DISPLAY_LENGTH; index++) + for (int index = 0; index < DISPLAY_LENGTH; index++) { _buffer[index] = color; } diff --git a/src/target/STM32/duino/HwJoystick.cpp b/src/target/STM32/duino/HwJoystick.cpp index 3e78f3c..ae067c2 100644 --- a/src/target/STM32/duino/HwJoystick.cpp +++ b/src/target/STM32/duino/HwJoystick.cpp @@ -1,5 +1,5 @@ #include "hardware/HwConfiguration.h" -#if defined (_STM32_DUINO_) +#if defined(_STM32_DUINO_) #include #include @@ -12,7 +12,7 @@ #define BUTTON_B 1 #define DEBOUNCE_TIMER (TIM6) -HardwareTimer _debounceTimer (DEBOUNCE_TIMER); +HardwareTimer _debounceTimer(DEBOUNCE_TIMER); void debounceCallback(); @@ -28,22 +28,22 @@ void debounceCallback() if (buttonACount == 0xFF) buttonState |= (1U << BUTTON_A); else if (!buttonACount) - buttonState &= ~ (1U << BUTTON_A); + buttonState &= ~(1U << BUTTON_A); /* Button B debounce */ tempButtonState = digitalRead(BUTTON_B_PIN); buttonBCount = (buttonBCount << 1) | tempButtonState; if (buttonBCount == 0xFF) buttonState |= (1U << BUTTON_B); else if (!buttonBCount) - buttonState &= ~ (1U << BUTTON_B); + buttonState &= ~(1U << BUTTON_B); } void hwJoystickSetup() { - _debounceTimer.setMode(1, TIMER_OUTPUT_COMPARE); - _debounceTimer.attachInterrupt(debounceCallback); + _debounceTimer.setMode(1, TIMER_OUTPUT_COMPARE); + _debounceTimer.attachInterrupt(debounceCallback); _debounceTimer.setPrescaleFactor(8000); - _debounceTimer.setOverflow(10, TICK_FORMAT); + _debounceTimer.setOverflow(10, TICK_FORMAT); _debounceTimer.resume(); pinMode(BUTTON_A_PIN, INPUT); diff --git a/src/target/STM32/duino/HwNotePlayer.cpp b/src/target/STM32/duino/HwNotePlayer.cpp index 4bb46b3..9752a0e 100644 --- a/src/target/STM32/duino/HwNotePlayer.cpp +++ b/src/target/STM32/duino/HwNotePlayer.cpp @@ -1,22 +1,22 @@ #include "hardware/HwConfiguration.h" -#if defined (_STM32_DUINO_) +#if defined(_STM32_DUINO_) #include "Arduino.h" #include #include -uint16_t noteFrequencies[] = { - 523, //DO - 587, //RE - 659, //MI - 698, //FA - 784, //SOL - 880, //LA - 988, //SI - 0 //PAUSE +uint16_t noteFrequencies[] = { + 523, // DO + 587, // RE + 659, // MI + 698, // FA + 784, // SOL + 880, // LA + 988, // SI + 0 // PAUSE }; -Note* _notes; +Note *_notes; uint16_t _size; volatile uint16_t _index; uint16_t _startIndex; @@ -24,19 +24,19 @@ bool _loopEn; uint8_t _volume; volatile uint32_t _duration; -#define NOTE_PIN (11) -// Timer 3, channel 2 are suitable to drive this pin +#define NOTE_PIN (11) +// Timer 3, channel 2 are suitable to drive this pin #define NOTE_TIMER (TIM3) #define NOTE_CHANNEL (2) //#define PAUSE_TIMER (TIM4) #define DURATION_TIMER (TIM4) // Functions to retrieve the correct timer and channel for driving the given pin -//TIM_TypeDef *p_timer = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(NOTE_PIN), PinMap_PWM); -//uint32_t _timerChannel = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(NOTE_PIN), PinMap_PWM)); -//HardwareTimer _noteTimer(p_timer); -HardwareTimer _noteTimer (NOTE_TIMER); -//HardwareTimer _pauseTimer (PAUSE_TIMER); -HardwareTimer _durationTimer (DURATION_TIMER); +// TIM_TypeDef *p_timer = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(NOTE_PIN), PinMap_PWM); +// uint32_t _timerChannel = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(NOTE_PIN), PinMap_PWM)); +// HardwareTimer _noteTimer(p_timer); +HardwareTimer _noteTimer(NOTE_TIMER); +// HardwareTimer _pauseTimer (PAUSE_TIMER); +HardwareTimer _durationTimer(DURATION_TIMER); void timerNoteCallback(void); static void hwNotePlayerNextNote(); @@ -47,7 +47,7 @@ void hwNotePlayerSetup() // clk@80MHz -> 100 us per tick -> overflow = duration*10 ms _noteTimer.setPreloadEnable(true); _durationTimer.setPreloadEnable(true); - _durationTimer.setMode(1, TIMER_OUTPUT_COMPARE); + _durationTimer.setMode(1, TIMER_OUTPUT_COMPARE); _durationTimer.setPrescaleFactor(8000); _durationTimer.attachInterrupt(timerNoteCallback); } @@ -57,12 +57,13 @@ static void hwNotePlayerNextNote() auto nextFrequency = noteFrequencies[_notes[_index].frequency]; auto nextDuration = _notes[_index].duration; _index++; - if (nextFrequency != 0) { + if (nextFrequency != 0) + { _noteTimer.setPWM(NOTE_CHANNEL, NOTE_PIN, nextFrequency, _volume); //_noteTimer.refresh(); - } - //DURATION_TIMER->ARR = nextDuration * 10; /* period = duration ms */ - _durationTimer.setOverflow(nextDuration * 10, TICK_FORMAT); + } + // DURATION_TIMER->ARR = nextDuration * 10; /* period = duration ms */ + _durationTimer.setOverflow(nextDuration * 10, TICK_FORMAT); _durationTimer.refresh(); _durationTimer.resume(); } @@ -70,12 +71,16 @@ static void hwNotePlayerNextNote() void timerNoteCallback(void) { hwNotePlayerStop(); - if (_index >= _size) { + if (_index >= _size) + { _index = _startIndex; - if (_loopEn) { + if (_loopEn) + { hwNotePlayerNextNote(); } - } else { + } + else + { hwNotePlayerNextNote(); } } @@ -98,7 +103,7 @@ void hwNotePlayerVolume(uint8_t volume) _volume = volume; } -void hwNotePlayerSong(Note* notes, uint16_t size, uint16_t startIndex, bool loopEn) +void hwNotePlayerSong(Note *notes, uint16_t size, uint16_t startIndex, bool loopEn) { _notes = notes; _size = size; diff --git a/src/target/STM32/duino/HwRefresh.cpp b/src/target/STM32/duino/HwRefresh.cpp index 0854922..0182bc7 100644 --- a/src/target/STM32/duino/HwRefresh.cpp +++ b/src/target/STM32/duino/HwRefresh.cpp @@ -1,11 +1,11 @@ #include "hardware/HwConfiguration.h" -#if defined (_STM32_DUINO_) +#if defined(_STM32_DUINO_) #include #include #define REFRESH_TIMER (TIM5) -HardwareTimer _refreshTimer (REFRESH_TIMER); +HardwareTimer _refreshTimer(REFRESH_TIMER); volatile bool _refreshFlag = false; void refreshCallback(); @@ -17,15 +17,16 @@ void refreshCallback() void hwRefreshSetup() { - _refreshTimer.setMode(1, TIMER_OUTPUT_COMPARE); + _refreshTimer.setMode(1, TIMER_OUTPUT_COMPARE); _refreshTimer.attachInterrupt(refreshCallback); - _refreshTimer.setOverflow(30, HERTZ_FORMAT); + _refreshTimer.setOverflow(30, HERTZ_FORMAT); _refreshTimer.resume(); } void hwRefreshWaitEndFrame() { - while (!_refreshFlag) ; + while (!_refreshFlag) + ; _refreshFlag = false; } diff --git a/src/target/STM32/hal/HwDisplay.cpp b/src/target/STM32/hal/HwDisplay.cpp index 1ac42e5..ed1b998 100644 --- a/src/target/STM32/hal/HwDisplay.cpp +++ b/src/target/STM32/hal/HwDisplay.cpp @@ -1,5 +1,5 @@ #include "hardware/HwConfiguration.h" -#if defined (_STM32_HAL_) +#if defined(_STM32_HAL_) #include "hardware/HwDisplay.h" @@ -7,10 +7,10 @@ I2C_HandleTypeDef hi2c1; #endif -#if defined (DISPLAY_SSD1306) +#if defined(DISPLAY_SSD1306) #define DISPLAY_COLUMNS (128) #define DISPLAY_STARTCOL (0) -#elif defined (DISPLAY_SH1106) +#elif defined(DISPLAY_SH1106) /* The screen is actually 130x64, so 2 cols must be cleared */ #define DISPLAY_COLUMNS (130) #define DISPLAY_STARTCOL (2) @@ -31,10 +31,11 @@ static uint8_t buffer[DISPLAY_LENGTH]; * Attention: display is initialize in OFF mode so that it can be cleared before its use */ static uint8_t INIT_COMMANDS[] = {0xAE, 0x20, 0x02, 0xB0, 0xC8, 0x00, - 0x10, 0x40, 0x81, 0xFF//0x3F testing contrast - , 0xA1, 0xA6, 0xA8, 0x3F, - 0xA4, 0xD3, 0x00, 0xD5, 0xF0, 0xD9, 0x22, 0xDA, 0x12, - 0xDB, 0x20, 0x8D, 0x14, 0xAF}; + 0x10, 0x40, 0x81, 0xFF // 0x3F testing contrast + , + 0xA1, 0xA6, 0xA8, 0x3F, + 0xA4, 0xD3, 0x00, 0xD5, 0xF0, 0xD9, 0x22, 0xDA, 0x12, + 0xDB, 0x20, 0x8D, 0x14, 0xAF}; uint8_t addressConfig[] = {0x21, 0, 127, 0x22, 0, 7}; @@ -42,23 +43,23 @@ void hwDisplaySetup() { #ifdef DISPLAY_I2C /* I2C Master mode - * Write transfer - * Fast mode (1 MHz) + * Write transfer + * Fast mode (1 MHz) * */ __HAL_RCC_I2C1_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); - /* I2C1 GPIO Configuration - * PB8 ------> I2C1_SCL - * PB9 ------> I2C1_SDA - */ + /* I2C1 GPIO Configuration + * PB8 ------> I2C1_SCL + * PB9 ------> I2C1_SDA + */ GPIO_InitTypeDef GPIO_InitStruct = {0}; - GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; - GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; - GPIO_InitStruct.Pull = GPIO_PULLUP; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF4_I2C1; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF4_I2C1; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); hi2c1.Instance = I2C1; hi2c1.Init.Timing = 0x00301238; hi2c1.Init.OwnAddress1 = 0; @@ -71,18 +72,18 @@ void hwDisplaySetup() if (HAL_I2C_Init(&hi2c1) != HAL_OK) { } - /** Configure Analogue filter - */ + /** Configure Analogue filter + */ if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_DISABLE) != HAL_OK) { } - /** Configure Digital filter - */ + /** Configure Digital filter + */ if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK) { } - /** I2C Fast mode Plus enable - */ + /** I2C Fast mode Plus enable + */ HAL_I2CEx_EnableFastModePlus(I2C_FASTMODEPLUS_I2C1); /* Wait display initialization */ HAL_Delay(200); @@ -90,12 +91,13 @@ void hwDisplaySetup() HAL_I2C_Mem_Write(&hi2c1, DISPLAY_ADDRESS_SHIFTED, 0x00, 1, INIT_COMMANDS, INIT_COMMANDS_SIZE, HAL_MAX_DELAY); /* Clear the whole display */ uint8_t tempBuff[DISPLAY_COLUMNS]; - for(int index = 0; index < DISPLAY_COLUMNS; index++) + for (int index = 0; index < DISPLAY_COLUMNS; index++) { tempBuff[index] = 0; - } + } /* Send the black screen to clear the display */ - for(int page = 0; page < 8; page++) { + for (int page = 0; page < 8; page++) + { uint8_t setPageCmd[] = {0xB0 + page, 0x00, 0x10}; HAL_I2C_Mem_Write(&hi2c1, DISPLAY_ADDRESS_SHIFTED, 0x00, 1, setPageCmd, 3, HAL_MAX_DELAY); HAL_I2C_Mem_Write(&hi2c1, DISPLAY_ADDRESS_SHIFTED, 0x40, 1, tempBuff, DISPLAY_COLUMNS, HAL_MAX_DELAY); @@ -106,9 +108,9 @@ void hwDisplaySetup() __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /* SPI2 GPIO Configuration - PC3 ------> SPI2_MOSI - PB10 ------> SPI2_SCK - PC12 ------> SPI_SS + PC3 ------> SPI2_MOSI + PB10 ------> SPI2_SCK + PC12 ------> SPI_SS */ GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Pin = GPIO_PIN_12; @@ -128,21 +130,17 @@ void hwDisplaySetup() GPIO_InitStruct.Alternate = GPIO_AF5_SPI2; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* SPI2 Configuration - Transmit-only mode - MSB first - Baudrate + Transmit-only mode + MSB first + Baudrate Master configuration Datasize 8 bit */ SPI2->CR2 = (SPI_CR2_DS_0 | SPI_CR2_DS_1 | SPI_CR2_DS_2 | SPI_CR2_NSSP); SPI2->CR1 = - ( - SPI_CR1_SPE - | SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE - | SPI_CR1_BR_1 /* /8 */ - //| SPI_CR1_BR_0 | SPI_CR1_BR_2 /* /64 */ - | SPI_CR1_MSTR - ); + (SPI_CR1_SPE | SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE | SPI_CR1_BR_1 /* /8 */ + //| SPI_CR1_BR_0 | SPI_CR1_BR_2 /* /64 */ + | SPI_CR1_MSTR); /* SPI SS enable */ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_12, GPIO_PIN_SET); #endif @@ -152,7 +150,8 @@ void hwDisplaySend() { #ifdef DISPLAY_I2C /* Transfer the screen to the display */ - for(int page = 0; page < 8; page++) { + for (int page = 0; page < 8; page++) + { uint8_t setPageCmd[] = {0xB0 + page, DISPLAY_STARTCOL, 0x10}; HAL_I2C_Mem_Write(&hi2c1, DISPLAY_ADDRESS_SHIFTED, 0x00, 1, setPageCmd, 3, HAL_MAX_DELAY); HAL_I2C_Mem_Write(&hi2c1, DISPLAY_ADDRESS_SHIFTED, 0x40, 1, buffer + (page * 128), 128, HAL_MAX_DELAY); @@ -161,13 +160,15 @@ void hwDisplaySend() /* Send init cmd */ for (uint8_t initCount = 0; initCount < 2; initCount++) { - while (! (SPI2->SR & SPI_SR_TXE) ); + while (!(SPI2->SR & SPI_SR_TXE)) + ; *((__IO uint8_t *)&SPI2->DR) = 0x01; } /* Send the display buffer */ for (uint16_t transferCount = 0; transferCount < DISPLAY_LENGTH; transferCount++) { - while (! (SPI2->SR & SPI_SR_TXE) ); + while (!(SPI2->SR & SPI_SR_TXE)) + ; *((__IO uint8_t *)&SPI2->DR) = displayBuffer[transferCount]; } #endif @@ -178,25 +179,25 @@ void hwDisplayDraw(uint8_t x, uint8_t y, DisplayColor color) #if defined(DISPLAY_ASCENDING_Y) uint8_t row = (7 - (uint8_t)y / 8); if (color == 0) - buffer[(row*128) + (uint8_t)x] |= 1 << (7 - ((uint8_t)y % 8)); + buffer[(row * 128) + (uint8_t)x] |= 1 << (7 - ((uint8_t)y % 8)); else if (color == 1) - buffer[(row*128) + (uint8_t)x] &= ~ ( 1 << (7 - (uint8_t)y % 8) ); + buffer[(row * 128) + (uint8_t)x] &= ~(1 << (7 - (uint8_t)y % 8)); else if (color == 2) - buffer[(row*128) + (uint8_t)x] ^= ( 1 << (7 - (uint8_t)y % 8) ); -#else + buffer[(row * 128) + (uint8_t)x] ^= (1 << (7 - (uint8_t)y % 8)); +#else uint8_t row = (uint8_t)y / 8; if (color == 0) - buffer[(row*128) + (uint8_t)x] |= 1 << ((uint8_t)y % 8); + buffer[(row * 128) + (uint8_t)x] |= 1 << ((uint8_t)y % 8); else if (color == 1) - buffer[(row*128) + (uint8_t)x] &= ~ ( 1 << ((uint8_t)y % 8) ); + buffer[(row * 128) + (uint8_t)x] &= ~(1 << ((uint8_t)y % 8)); else if (color == 2) - buffer[(row*128) + (uint8_t)x] ^= ( 1 << ((uint8_t)y % 8) ); + buffer[(row * 128) + (uint8_t)x] ^= (1 << ((uint8_t)y % 8)); #endif } void hwDisplayFill(DisplayColor color) { - for(int index = 0; index < DISPLAY_LENGTH; index++) + for (int index = 0; index < DISPLAY_LENGTH; index++) { buffer[index] = color; } diff --git a/src/target/STM32/hal/HwJoystick.cpp b/src/target/STM32/hal/HwJoystick.cpp index e89ef38..1791273 100644 --- a/src/target/STM32/hal/HwJoystick.cpp +++ b/src/target/STM32/hal/HwJoystick.cpp @@ -1,5 +1,5 @@ #include "hardware/HwConfiguration.h" -#if defined (_STM32_HAL_) +#if defined(_STM32_HAL_) #include @@ -15,26 +15,27 @@ volatile uint8_t buttonACount = 0x10; volatile uint8_t buttonBCount = 0x10; #ifdef __cplusplus -extern "C" { -#endif // __cplusplus -void TIM2_IRQHandler(void) +extern "C" { - TIM2->SR &= ~TIM_SR_UIF; - /* Button A debounce */ - ButtonStateType tempButtonState = HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_A_PIN); - buttonACount = (buttonACount << 1) | tempButtonState; - if (buttonACount == 0xFF) - buttonState |= (1U << BUTTON_A); - else if (!buttonACount) - buttonState &= ~ (1U << BUTTON_A); - /* Button B debounce */ - tempButtonState = HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_B_PIN); - buttonBCount = (buttonBCount << 1) | tempButtonState; - if (buttonBCount == 0xFF) - buttonState |= (1U << BUTTON_B); - else if (!buttonBCount) - buttonState &= ~ (1U << BUTTON_B); -} +#endif // __cplusplus + void TIM2_IRQHandler(void) + { + TIM2->SR &= ~TIM_SR_UIF; + /* Button A debounce */ + ButtonStateType tempButtonState = HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_A_PIN); + buttonACount = (buttonACount << 1) | tempButtonState; + if (buttonACount == 0xFF) + buttonState |= (1U << BUTTON_A); + else if (!buttonACount) + buttonState &= ~(1U << BUTTON_A); + /* Button B debounce */ + tempButtonState = HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_B_PIN); + buttonBCount = (buttonBCount << 1) | tempButtonState; + if (buttonBCount == 0xFF) + buttonState |= (1U << BUTTON_B); + else if (!buttonBCount) + buttonState &= ~(1U << BUTTON_B); + } #ifdef __cplusplus } // extern "C" #endif // __cplusplus @@ -53,10 +54,10 @@ void hwJoystickSetup() TIM2->PSC = 8000; TIM2->DIER = TIM_DIER_UIE; /* TIM2 start */ - TIM2->CR1 = TIM_CR1_CEN; + TIM2->CR1 = TIM_CR1_CEN; /* Configure GPIO pins */ GPIO_InitTypeDef GPIO_InitStruct = {0}; - GPIO_InitStruct.Pin = BUTTON_A_PIN|BUTTON_B_PIN; + GPIO_InitStruct.Pin = BUTTON_A_PIN | BUTTON_B_PIN; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct); diff --git a/src/target/STM32/hal/HwNotePlayer.cpp b/src/target/STM32/hal/HwNotePlayer.cpp index 82f47b9..37ba9b3 100644 --- a/src/target/STM32/hal/HwNotePlayer.cpp +++ b/src/target/STM32/hal/HwNotePlayer.cpp @@ -1,5 +1,5 @@ #include "hardware/HwConfiguration.h" -#if defined (_STM32_HAL_) +#if defined(_STM32_HAL_) #include @@ -10,14 +10,14 @@ static void frequencyTimerSetup(); static void durationTimerSetup(); uint16_t noteFrequencies[] = { - 3812, //DO - 3402, //RE - 3016, //MI - 2848, //FA - 2536, //SOL - 2273, //LA - 1987, //SI - 0 //PAUSE + 3812, // DO + 3402, // RE + 3016, // MI + 2848, // FA + 2536, // SOL + 2273, // LA + 1987, // SI + 0 // PAUSE }; uint8_t vol; @@ -28,39 +28,41 @@ uint16_t currentSongStartIndex; bool currentSongLoop; #ifdef __cplusplus -extern "C" { -#endif // __cplusplus -void TIM4_IRQHandler(void) +extern "C" { - FREQUENCY_TIMER->CR1 &= ~TIM_CR1_CEN; - DURATION_TIMER->CR1 &= ~TIM_CR1_CEN; - DURATION_TIMER->SR &= ~TIM_SR_UIF; - /* Configure frequency and duration timer according to the next note */ - if (currentSongIndex < currentSongSize) +#endif // __cplusplus + void TIM4_IRQHandler(void) { - Note tempNote = currentSongNotes[currentSongIndex]; - uint16_t tempFrequency = noteFrequencies[tempNote.frequency]; - if (tempFrequency) + FREQUENCY_TIMER->CR1 &= ~TIM_CR1_CEN; + DURATION_TIMER->CR1 &= ~TIM_CR1_CEN; + DURATION_TIMER->SR &= ~TIM_SR_UIF; + /* Configure frequency and duration timer according to the next note */ + if (currentSongIndex < currentSongSize) { - /* Setup and restart frequency timer */ - FREQUENCY_TIMER->ARR = tempFrequency; - FREQUENCY_TIMER->CCR2 = (tempFrequency / 2) * vol / 100 ; - FREQUENCY_TIMER->CR1 |= TIM_CR1_CEN; + Note tempNote = currentSongNotes[currentSongIndex]; + uint16_t tempFrequency = noteFrequencies[tempNote.frequency]; + if (tempFrequency) + { + /* Setup and restart frequency timer */ + FREQUENCY_TIMER->ARR = tempFrequency; + FREQUENCY_TIMER->CCR2 = (tempFrequency / 2) * vol / 100; + FREQUENCY_TIMER->CR1 |= TIM_CR1_CEN; + } + /* Setup and restart duration timer */ + DURATION_TIMER->CNT = 0; + DURATION_TIMER->ARR = tempNote.duration * 10; /* period = duration ms */ + DURATION_TIMER->CR1 |= TIM_CR1_CEN; + /* Update note index */ + currentSongIndex++; + } + else if (currentSongLoop) + { + currentSongIndex = currentSongStartIndex; + DURATION_TIMER->CNT = 0; + DURATION_TIMER->ARR = 1; + DURATION_TIMER->CR1 |= TIM_CR1_CEN; } - /* Setup and restart duration timer */ - DURATION_TIMER->CNT = 0; - DURATION_TIMER->ARR = tempNote.duration * 10; /* period = duration ms */ - DURATION_TIMER->CR1 |= TIM_CR1_CEN; - /* Update note index */ - currentSongIndex++; - } else if(currentSongLoop) - { - currentSongIndex = currentSongStartIndex; - DURATION_TIMER->CNT = 0; - DURATION_TIMER->ARR = 1; - DURATION_TIMER->CR1 |= TIM_CR1_CEN; } -} #ifdef __cplusplus } // extern "C" #endif // __cplusplus @@ -70,7 +72,7 @@ static void frequencyTimerSetup() /* Enable FREQUENCY_TIMER clock -> FREQUENCY TIMER */ __HAL_RCC_TIM3_CLK_ENABLE(); /* FREQUENCY_TIMER GPIO Configuration - PA7 ------> TIM3_CH2 + PA7 ------> TIM3_CH2 */ __HAL_RCC_GPIOA_CLK_ENABLE(); GPIO_InitTypeDef GPIO_InitStruct = {0}; @@ -81,23 +83,23 @@ static void frequencyTimerSetup() GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* FREQUENCY_TIMER base setup */ - FREQUENCY_TIMER->PSC = 40; /* Prescaler 40 -> timer clock = 2MHz*/ + FREQUENCY_TIMER->PSC = 40; /* Prescaler 40 -> timer clock = 2MHz*/ FREQUENCY_TIMER->ARR = 3974; /* Period = CLK/(ARR*PSC) */ FREQUENCY_TIMER->DIER = TIM_DIER_UIE; /* FREQUENCY_TIMER PWM ch2 setup */ FREQUENCY_TIMER->CCER &= ~TIM_CCER_CC2E; - FREQUENCY_TIMER->CCMR1 &= ~ (TIM_CCMR1_OC2M | TIM_CCMR1_CC2S); - FREQUENCY_TIMER->CCMR1 |= ((TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1) << 8U);//(TIM_OCMODE_PWM1 << 8U); + FREQUENCY_TIMER->CCMR1 &= ~(TIM_CCMR1_OC2M | TIM_CCMR1_CC2S); + FREQUENCY_TIMER->CCMR1 |= ((TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1) << 8U); //(TIM_OCMODE_PWM1 << 8U); FREQUENCY_TIMER->CCER &= ~TIM_CCER_CC2P; - FREQUENCY_TIMER->CCER |= (0x00000000U << 4U);//(TIM_OCPOLARITY_HIGH << 4U); - FREQUENCY_TIMER->CCR2 = 1800; /* Duty Cycle */ + FREQUENCY_TIMER->CCER |= (0x00000000U << 4U); //(TIM_OCPOLARITY_HIGH << 4U); + FREQUENCY_TIMER->CCR2 = 1800; /* Duty Cycle */ /* Set the Preload enable bit for channel2 */ FREQUENCY_TIMER->CCMR1 |= TIM_CCMR1_OC2PE; /* Configure the Output Fast mode */ FREQUENCY_TIMER->CCMR1 &= ~TIM_CCMR1_OC2FE; - FREQUENCY_TIMER->CCMR1 |= 0x00000000U << 8U;//TIM_OCFAST_DISABLE << 8U; + FREQUENCY_TIMER->CCMR1 |= 0x00000000U << 8U; // TIM_OCFAST_DISABLE << 8U; /* Reset the CCxE Bit and activate the channel */ - FREQUENCY_TIMER->CCER &= ~(TIM_CCER_CC1E << (0x00000004U & 0x1FU)); // TIM_CHANNEL_2 == 0x00000004U + FREQUENCY_TIMER->CCER &= ~(TIM_CCER_CC1E << (0x00000004U & 0x1FU)); // TIM_CHANNEL_2 == 0x00000004U FREQUENCY_TIMER->CCER |= (uint32_t)(0x00000001U << (0x00000004U & 0x1FU)); // TIM_CHANNEL_2 == 0x00000004U -- TIM_CCx_ENABLE == 0x00000001U } @@ -113,7 +115,7 @@ static void durationTimerSetup() DURATION_TIMER->PSC = 8000; /* clk@80MHz -> 100 us per tick */ DURATION_TIMER->DIER = TIM_DIER_UIE; /* DURATION_TIMER start */ - //DURATION_TIMER->CR1 = TIM_CR1_CEN; + // DURATION_TIMER->CR1 = TIM_CR1_CEN; } void hwNotePlayerSetup() @@ -140,7 +142,7 @@ void hwNotePlayerVolume(uint8_t volume) vol = volume; } -void hwNotePlayerSong(Note* notes, uint16_t size, uint16_t startIndex, bool loopEn) +void hwNotePlayerSong(Note *notes, uint16_t size, uint16_t startIndex, bool loopEn) { currentSongNotes = notes; currentSongSize = size; diff --git a/src/target/STM32/hal/HwRefresh.cpp b/src/target/STM32/hal/HwRefresh.cpp index 6225692..a030c87 100644 --- a/src/target/STM32/hal/HwRefresh.cpp +++ b/src/target/STM32/hal/HwRefresh.cpp @@ -1,18 +1,19 @@ #include "hardware/HwConfiguration.h" -#if defined (_STM32_HAL_) +#if defined(_STM32_HAL_) #include volatile bool refreshFlag = false; #ifdef __cplusplus -extern "C" { -#endif // __cplusplus -void TIM1_UP_TIM16_IRQHandler() +extern "C" { - TIM1->SR &= ~TIM_DIER_UIE; - refreshFlag = true; -} +#endif // __cplusplus + void TIM1_UP_TIM16_IRQHandler() + { + TIM1->SR &= ~TIM_DIER_UIE; + refreshFlag = true; + } #ifdef __cplusplus } // extern "C" #endif // __cplusplu @@ -20,27 +21,28 @@ void TIM1_UP_TIM16_IRQHandler() void hwRefreshSetup() { /* TIM1 Clock enable */ - __HAL_RCC_TIM1_CLK_ENABLE(); - /* TIM1 interrupt Init */ - HAL_NVIC_SetPriority(TIM1_UP_TIM16_IRQn, 0, 0); - HAL_NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn); + __HAL_RCC_TIM1_CLK_ENABLE(); + /* TIM1 interrupt Init */ + HAL_NVIC_SetPriority(TIM1_UP_TIM16_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn); /* TIM1 setup */ #ifdef REFRESH_60HZ - /* Refresh set to 60 Hz */ + /* Refresh set to 60 Hz */ TIM1->ARR = 1666; #else - /* Refresh set to 30 Hz */ + /* Refresh set to 30 Hz */ TIM1->ARR = 3332; #endif TIM1->PSC = 800; TIM1->DIER = TIM_DIER_UIE; /* TIM1 start */ - TIM1->CR1 = TIM_CR1_CEN; + TIM1->CR1 = TIM_CR1_CEN; } void hwRefreshWaitEndFrame() { - while (!refreshFlag) ; + while (!refreshFlag) + ; refreshFlag = false; }