Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…l-core into jacdac-v0
  • Loading branch information
jamesadevine committed May 23, 2019
2 parents 5a84c35 + a76fd8d commit 87066bd
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 18 deletions.
5 changes: 4 additions & 1 deletion inc/driver-models/Accelerometer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ DEALINGS IN THE SOFTWARE.
#define ACCELEROMETER_EVT_6G 9
#define ACCELEROMETER_EVT_8G 10
#define ACCELEROMETER_EVT_SHAKE 11
#define ACCELEROMETER_EVT_2G 12

/**
* Gesture recogniser constants
Expand All @@ -64,6 +65,7 @@ DEALINGS IN THE SOFTWARE.
#define ACCELEROMETER_TILT_TOLERANCE 200
#define ACCELEROMETER_FREEFALL_TOLERANCE 400
#define ACCELEROMETER_SHAKE_TOLERANCE 400
#define ACCELEROMETER_2G_TOLERANCE 2048
#define ACCELEROMETER_3G_TOLERANCE 3072
#define ACCELEROMETER_6G_TOLERANCE 6144
#define ACCELEROMETER_8G_TOLERANCE 8192
Expand All @@ -73,6 +75,7 @@ DEALINGS IN THE SOFTWARE.

#define ACCELEROMETER_REST_THRESHOLD (ACCELEROMETER_REST_TOLERANCE * ACCELEROMETER_REST_TOLERANCE)
#define ACCELEROMETER_FREEFALL_THRESHOLD ((uint32_t)ACCELEROMETER_FREEFALL_TOLERANCE * (uint32_t)ACCELEROMETER_FREEFALL_TOLERANCE)
#define ACCELEROMETER_2G_THRESHOLD ((uint32_t)ACCELEROMETER_2G_TOLERANCE * (uint32_t)ACCELEROMETER_2G_TOLERANCE)
#define ACCELEROMETER_3G_THRESHOLD ((uint32_t)ACCELEROMETER_3G_TOLERANCE * (uint32_t)ACCELEROMETER_3G_TOLERANCE)
#define ACCELEROMETER_6G_THRESHOLD ((uint32_t)ACCELEROMETER_6G_TOLERANCE * (uint32_t)ACCELEROMETER_6G_TOLERANCE)
#define ACCELEROMETER_8G_THRESHOLD ((uint32_t)ACCELEROMETER_8G_TOLERANCE * (uint32_t)ACCELEROMETER_8G_TOLERANCE)
Expand All @@ -86,7 +89,7 @@ namespace codal
x:1,
y:1,
z:1,
unused,
impulse_2,
impulse_3,
impulse_6,
impulse_8,
Expand Down
43 changes: 43 additions & 0 deletions inc/driver-models/ScreenIO.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
The MIT License (MIT)
Copyright (c) 2017 Lancaster University.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

#ifndef CODAL_SCREENIO_H
#define CODAL_SCREENIO_H

#include "CodalConfig.h"

namespace codal
{

class ScreenIO
{
public:
virtual void send(const void *txBuffer, uint32_t txSize) = 0;
virtual void startSend(const void *txBuffer, uint32_t txSize, PVoidCallback doneHandler,
void *handlerArg) = 0;
};

} // namespace codal

#endif
2 changes: 1 addition & 1 deletion inc/drivers/ILI9341.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace codal
class ILI9341 : public ST7735
{
public:
ILI9341(SPI &spi, Pin &cs, Pin &dc);
ILI9341(ScreenIO &io, Pin &cs, Pin &dc);
int init();
};

Expand Down
47 changes: 47 additions & 0 deletions inc/drivers/SPIScreenIO.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
The MIT License (MIT)
Copyright (c) 2017 Lancaster University.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

#ifndef DEVICE_SPISCREENIO_H
#define DEVICE_SPISCREENIO_H

#include "Pin.h"
#include "SPI.h"
#include "ScreenIO.h"

namespace codal
{

class SPIScreenIO : public ScreenIO
{
public:
SPI &spi;
SPIScreenIO(SPI &spi);
virtual void send(const void *txBuffer, uint32_t txSize);
virtual void startSend(const void *txBuffer, uint32_t txSize, PVoidCallback doneHandler,
void *handlerArg);
};

} // namespace codal

#endif
17 changes: 10 additions & 7 deletions inc/drivers/ST7735.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ DEALINGS IN THE SOFTWARE.
#include "Pin.h"
#include "SPI.h"
#include "Event.h"
#include "ScreenIO.h"

namespace codal
{
Expand All @@ -45,7 +46,7 @@ struct ST7735WorkBuffer;
class ST7735 : public CodalComponent
{
protected:
SPI &spi;
ScreenIO &io;
Pin &cs;
Pin &dc;
uint8_t cmdBuf[20];
Expand All @@ -68,27 +69,29 @@ class ST7735 : public CodalComponent
static void sendColorsStep(ST7735 *st);

public:
ST7735(SPI &spi, Pin &cs, Pin &dc);
ST7735(ScreenIO &io, Pin &cs, Pin &dc);
virtual int init();

/**
* Configure screen-specific parameters.
*
* @param madctl See MADCTL_* constants above
* @param frmctr1 defaults to 0x083b3b, 0x053a3a, 0x053c3c depending on screen size; 0x000605 was found to work well on 160x128 screen; big-endian
* @param frmctr1 defaults to 0x083b3b, 0x053a3a, 0x053c3c depending on screen size; 0x000605
* was found to work well on 160x128 screen; big-endian
*/
void configure(uint8_t madctl, uint32_t frmctr1);
/**
* Set rectangle where pixels sent by sendIndexedImage() will be stored.
*/
void setAddrWindow(int x, int y, int w, int h);
/**
* Send 4 bit indexed color image, little endian, column-major, using specified palette (use NULL
* if unchanged).
* Send 4 bit indexed color image, little endian, column-major, using specified palette (use
* NULL if unchanged).
*/
int sendIndexedImage(const uint8_t *src, unsigned width, unsigned height, uint32_t *palette);
/**
* Waits for the previous sendIndexedImage() operation to complete (it normally executes in background).
* Waits for the previous sendIndexedImage() operation to complete (it normally executes in
* background).
*/
void waitForSendDone();

Expand All @@ -98,6 +101,6 @@ class ST7735 : public CodalComponent
virtual int setSleep(bool sleepMode);
};

}
} // namespace codal

#endif
10 changes: 8 additions & 2 deletions source/driver-models/Accelerometer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Accelerometer::Accelerometer(CoordinateSpace &cspace, uint16_t id) : sample(), s
this->shake.z = 0;
this->shake.count = 0;
this->shake.timer = 0;
this->shake.impulse_2 = 1;
this->shake.impulse_3 = 1;
this->shake.impulse_6 = 1;
this->shake.impulse_8 = 1;
Expand Down Expand Up @@ -223,8 +224,13 @@ void Accelerometer::updateGesture()
// For these events, we don't perform any low pass filtering.
uint32_t force = instantaneousAccelerationSquared();

if (force > ACCELEROMETER_3G_THRESHOLD)
if (force > ACCELEROMETER_2G_THRESHOLD)
{
if (force > ACCELEROMETER_2G_THRESHOLD && !shake.impulse_2)
{
Event e(DEVICE_ID_GESTURE, ACCELEROMETER_EVT_2G);
shake.impulse_2 = 1;
}
if (force > ACCELEROMETER_3G_THRESHOLD && !shake.impulse_3)
{
Event e(DEVICE_ID_GESTURE, ACCELEROMETER_EVT_3G);
Expand All @@ -248,7 +254,7 @@ void Accelerometer::updateGesture()
if (impulseSigma < ACCELEROMETER_GESTURE_DAMPING)
impulseSigma++;
else
shake.impulse_3 = shake.impulse_6 = shake.impulse_8 = 0;
shake.impulse_2 = shake.impulse_3 = shake.impulse_6 = shake.impulse_8 = 0;


// Determine what it looks like we're doing based on the latest sample...
Expand Down
4 changes: 2 additions & 2 deletions source/drivers/ILI9341.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static const uint8_t initcmd[] = {
ILI9341_PWCTR2 , 1, 0x10, // Power control SAP[2:0];BT[3:0]
ILI9341_VMCTR1 , 2, 0x3e, 0x28, // VCM control
ILI9341_VMCTR2 , 1, 0x86, // VCM control2
ILI9341_MADCTL , 1, 0x48, // Memory Access Control
ILI9341_MADCTL , 1, 0x08, // Memory Access Control
ILI9341_VSCRSADD, 1, 0x00, // Vertical scroll zero
ILI9341_PIXFMT , 1, 0x55,
ILI9341_FRMCTR1 , 2, 0x00, 0x18,
Expand All @@ -148,7 +148,7 @@ static const uint8_t initcmd[] = {
namespace codal
{

ILI9341::ILI9341(SPI &spi, Pin &cs, Pin &dc) : ST7735(spi, cs, dc)
ILI9341::ILI9341(ScreenIO &io, Pin &cs, Pin &dc) : ST7735(io, cs, dc)
{
double16 = true;
}
Expand Down
10 changes: 5 additions & 5 deletions source/drivers/ST7735.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
namespace codal
{

ST7735::ST7735(SPI &spi, Pin &cs, Pin &dc) : spi(spi), cs(cs), dc(dc), work(NULL)
ST7735::ST7735(ScreenIO &io, Pin &cs, Pin &dc) : io(io), cs(cs), dc(dc), work(NULL)
{
double16 = false;
}
Expand Down Expand Up @@ -238,7 +238,7 @@ void ST7735::sendColorsStep(ST7735 *st)
base[i + 32 + 64] = (palette[i] >> 2) & 0x3f;
}
st->startRAMWR(0x2D);
st->spi.transfer(work->dataBuf, 128, NULL, 0);
st->io.send(work->dataBuf, 128);
st->cs.setDigitalValue(1);
}

Expand Down Expand Up @@ -286,7 +286,7 @@ void ST7735::sendColorsStep(ST7735 *st)

void ST7735::startTransfer(unsigned size)
{
spi.startTransfer(work->dataBuf, size, NULL, 0, (PVoidCallback)&ST7735::sendColorsStep, this);
io.startSend(work->dataBuf, size, (PVoidCallback)&ST7735::sendColorsStep, this);
}

void ST7735::startRAMWR(int cmd)
Expand Down Expand Up @@ -386,12 +386,12 @@ void ST7735::sendCmd(uint8_t *buf, int len)
buf = cmdBuf;
dc.setDigitalValue(0);
cs.setDigitalValue(0);
spi.transfer(buf, 1, NULL, 0);
io.send(buf, 1);
dc.setDigitalValue(1);
len--;
buf++;
if (len > 0)
spi.transfer(buf, len, NULL, 0);
io.send(buf, len);
cs.setDigitalValue(1);
}

Expand Down
19 changes: 19 additions & 0 deletions source/drivers/ScreenIO.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "SPIScreenIO.h"

namespace codal
{

SPIScreenIO::SPIScreenIO(SPI &spi) : spi(spi) {}

void SPIScreenIO::send(const void *txBuffer, uint32_t txSize)
{
spi.transfer((const uint8_t *)txBuffer, txSize, NULL, 0);
}

void SPIScreenIO::startSend(const void *txBuffer, uint32_t txSize, PVoidCallback doneHandler,
void *handlerArg)
{
spi.startTransfer((const uint8_t *)txBuffer, txSize, NULL, 0, doneHandler, handlerArg);
}

} // namespace codal

0 comments on commit 87066bd

Please sign in to comment.