Skip to content

Commit

Permalink
doxyclang
Browse files Browse the repository at this point in the history
  • Loading branch information
ladyada committed Dec 28, 2019
1 parent 44d2755 commit c2768f7
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 9 deletions.
105 changes: 100 additions & 5 deletions Adafruit_Microbit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ Adafruit_Microbit_Matrix::Adafruit_Microbit_Matrix() : Adafruit_GFX(5, 5) {

Adafruit_Microbit_Matrix::~Adafruit_Microbit_Matrix(void) {}

/*!
* @brief Initialized the 5x5 matrix and scanning IRQ
* @returns True
*/
boolean Adafruit_Microbit_Matrix::begin(void) {
handle = this;

Expand All @@ -83,8 +87,10 @@ boolean Adafruit_Microbit_Matrix::begin(void) {
return true;
}

// Matrix object function called by IRQ handler for each row
// This is not optimized at all but its not so bad either!
/*!
* @brief Matrix object function called by IRQ handler for each row
* This is not optimized at all but its not so bad either!
*/
void Adafruit_Microbit_Matrix::rowHandler(void) {
// disable current row
digitalWrite(rowpins[currentRow], LOW);
Expand All @@ -107,7 +113,9 @@ void Adafruit_Microbit_Matrix::rowHandler(void) {
digitalWrite(rowpins[currentRow], HIGH);
}

// This sets up the IRQ for timer 2 to run the matrix refresh.
/*!
* @brief Sets up the IRQ for timer 2 to run the matrix refresh.
*/
void Adafruit_Microbit_Matrix::startTimer(void) {
NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer; // Set the timer in Counter Mode
NRF_TIMER2->TASKS_CLEAR = 1; // clear the task first to be usable for later
Expand All @@ -134,6 +142,12 @@ void IRQ_MATRIX_HANDLER(void) {
}
}

/*!
* @brief Draw a single pixel/LED on the 5x5 matrix
* @param x 0 to 4 column
* @param y 0 to 4 row
* @param color 1 for LEDs on, 0 for off
*/
void Adafruit_Microbit_Matrix::drawPixel(int16_t x, int16_t y, uint16_t color) {
if ((x < 0) || (y < 0) || (x >= _width) || (y >= _height))
return;
Expand Down Expand Up @@ -169,8 +183,15 @@ void Adafruit_Microbit_Matrix::drawPixel(int16_t x, int16_t y, uint16_t color) {
matrix_buffer[row][col] = 0;
}

/*!
* @brief Clear the 5x5 matrix
*/
void Adafruit_Microbit_Matrix::clear(void) { fillScreen(0); }

/*!
* @brief Fill the 5x5 matrix with an LED 'color'
* @param color 1 for LEDs on, 0 for off
*/
void Adafruit_Microbit_Matrix::fillScreen(uint16_t color) {
for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
for (uint8_t c = 0; c < MATRIX_COLS; c++) {
Expand All @@ -179,11 +200,19 @@ void Adafruit_Microbit_Matrix::fillScreen(uint16_t color) {
}
}

/*!
* @brief Display a 5-byte bitmap on the 5x5 LED matrix
* @param bitmap 5 byte bitmap
*/
void Adafruit_Microbit_Matrix::show(const uint8_t bitmap[]) {
clear();
drawBitmap(-3, 0, bitmap, 8, 5, LED_ON);
}

/*!
* @brief Display a string on the 5x5 LED matrix
* @param string Null-terminated ascii string
*/
void Adafruit_Microbit_Matrix::print(char *string) {
setFont(&TomThumb);
setTextWrap(false);
Expand All @@ -198,6 +227,10 @@ void Adafruit_Microbit_Matrix::print(char *string) {
}
}

/*!
* @brief Display a signed number on the 5x5 LED matrix
* @param i The value
*/
void Adafruit_Microbit_Matrix::print(int32_t i) {
char buffer[34];
memset(buffer, 0, 34);
Expand All @@ -206,6 +239,12 @@ void Adafruit_Microbit_Matrix::print(int32_t i) {
print(buffer);
}

/*!
* @brief Display a signed number on the 5x5 LED matrix
* @param i The value
*/
void Adafruit_Microbit_Matrix::print(int i) { print((int32_t)i); }

#define MAX_PRECISION (10)
static const double rounders[MAX_PRECISION + 1] = {
0.5, // 0
Expand All @@ -221,6 +260,11 @@ static const double rounders[MAX_PRECISION + 1] = {
0.00000000005 // 10
};

/*!
* @brief Display a floating point number on the 5x5 LED matrix
* @param f The floating point value
* @param precision Digits after decimal
*/
void Adafruit_Microbit_Matrix::print(double f, int precision) {
char buf[80];

Expand Down Expand Up @@ -311,6 +355,11 @@ void Adafruit_Microbit_Matrix::print(double f, int precision) {
print(buf);
}

/*!
* @brief Scroll display a string on the 5x5 LED matrix
* @param string Null-terminated ascii string
* @param stepdelay Milliseconds per scroll step
*/
void Adafruit_Microbit_Matrix::scrollText(char *string, uint8_t stepdelay) {
setFont(&TomThumb);
setTextWrap(false);
Expand All @@ -324,10 +373,17 @@ void Adafruit_Microbit_Matrix::scrollText(char *string, uint8_t stepdelay) {
}
}

/*************************************************************************************************/
/*****************************************************************/

/*!
* @brief Initializes the LED matrix
*/
void Adafruit_Microbit::begin(void) { matrix.begin(); }

/*!
* @brief Request the temperature from the Soft Device
* @returns Temperature in Celsius
*/
uint8_t Adafruit_Microbit::getDieTemp(void) {
int32_t temp = 0;
uint32_t err_code;
Expand All @@ -343,10 +399,16 @@ uint8_t Adafruit_Microbit::getDieTemp(void) {
return temp / 4;
}

/*************************************************************************************************/
/*********************************************************************/

Adafruit_Microbit_BLESerial *Adafruit_Microbit_BLESerial::_instance = NULL;

/*!
* @brief Create a Nordic UART service interface
* @param req Unused
* @param rdy Unused
* @param rst Unused
*/
Adafruit_Microbit_BLESerial::Adafruit_Microbit_BLESerial(unsigned char req,
unsigned char rdy,
unsigned char rst)
Expand All @@ -367,13 +429,19 @@ Adafruit_Microbit_BLESerial::Adafruit_Microbit_BLESerial(unsigned char req,
addAttribute(this->_txNameDescriptor);
}

/*!
* @brief Initialize Nordic UART service interface
*/
void Adafruit_Microbit_BLESerial::begin(...) {
BLEPeripheral::begin();
#ifdef BLE_SERIAL_DEBUG
Serial.println(F("Adafruit_Microbit_BLESerial::begin()"));
#endif
}

/*!
* @brief Check/flush the UART service pipe
*/
void Adafruit_Microbit_BLESerial::poll() {
if (millis() < this->_flushed + 100) {
BLEPeripheral::poll();
Expand All @@ -382,13 +450,20 @@ void Adafruit_Microbit_BLESerial::poll() {
}
}

/*!
* @brief Send any pending data and close BLE connection
*/
void Adafruit_Microbit_BLESerial::end() {
this->_rxCharacteristic.setEventHandler(BLEWritten, NULL);
this->_rxHead = this->_rxTail = 0;
flush();
BLEPeripheral::disconnect();
}

/*!
* @brief Check how many bytes are available to read over Nordic UART
* @returns Bytes available to read
*/
int Adafruit_Microbit_BLESerial::available(void) {
BLEPeripheral::poll();
int retval = (this->_rxHead - this->_rxTail + sizeof(this->_rxBuffer)) %
Expand All @@ -400,6 +475,10 @@ int Adafruit_Microbit_BLESerial::available(void) {
return retval;
}

/*!
* @brief Peek at next byte of UART buffer, without removing it
* @returns Byte read, -1 if no data to read (use available() first!)
*/
int Adafruit_Microbit_BLESerial::peek(void) {
BLEPeripheral::poll();
if (this->_rxTail == this->_rxHead)
Expand All @@ -414,6 +493,10 @@ int Adafruit_Microbit_BLESerial::peek(void) {
return byte;
}

/*!
* @brief Read one byte out of UART buffer
* @returns Byte read, -1 if no data to read (use available() first!)
*/
int Adafruit_Microbit_BLESerial::read(void) {
BLEPeripheral::poll();
if (this->_rxTail == this->_rxHead)
Expand All @@ -429,6 +512,9 @@ int Adafruit_Microbit_BLESerial::read(void) {
return byte;
}

/*!
* @brief Send any pending data in UART buffer
*/
void Adafruit_Microbit_BLESerial::flush(void) {
if (this->_txCount == 0)
return;
Expand All @@ -441,6 +527,11 @@ void Adafruit_Microbit_BLESerial::flush(void) {
#endif
}

/*!
* @brief Write one byte out to UART service
* @param byte Since data byte to write
* @returns 1 on success, 0 on failure
*/
size_t Adafruit_Microbit_BLESerial::write(uint8_t byte) {
BLEPeripheral::poll();
if (this->_txCharacteristic.subscribed() == false)
Expand All @@ -458,6 +549,10 @@ size_t Adafruit_Microbit_BLESerial::write(uint8_t byte) {
return 1;
}

/*!
* @brief Test if UART service is connected over BLE
* @returns True if connected
*/
Adafruit_Microbit_BLESerial::operator bool() {
bool retval = BLEPeripheral::connected();
#ifdef BLE_SERIAL_DEBUG
Expand Down
14 changes: 10 additions & 4 deletions Adafruit_Microbit.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

void IRQ_MATRIX_HANDLER(void);

/** Class to create Adafruit_GFX interface for 5x5 matrix of micro:bit */
class Adafruit_Microbit_Matrix : public Adafruit_GFX {
public:
Adafruit_Microbit_Matrix();
Expand All @@ -22,17 +23,21 @@ class Adafruit_Microbit_Matrix : public Adafruit_GFX {
void scrollText(char *string, uint8_t stepdelay = 150);
void print(char *string);
void print(int32_t i);
void print(int i) { print((int32_t)i); }
void print(int i);
void print(double f, int precision = 3);

static const uint8_t EMPTYHEART[5], HEART[5], NO[5], YES[5];
static const uint8_t EMPTYHEART[5], ///< an empty heart icon
HEART[5], ///< full heart icon
NO[5], ///< X icon
YES[5]; ///< Check icon

private:
void startTimer();

uint8_t matrix_buffer[3][9];
};

/** Class to use Nordic UART service as a Stream object on micro:bit */
class Adafruit_Microbit_BLESerial : public BLEPeripheral, public Stream {
public:
Adafruit_Microbit_BLESerial(unsigned char req = BLE_DEFAULT_REQ,
Expand Down Expand Up @@ -80,10 +85,11 @@ class Adafruit_Microbit_BLESerial : public BLEPeripheral, public Stream {
BLECharacteristic &rxCharacteristic);
};

/** Class to create hardware interface to BLE/matrix of micro:bit */
class Adafruit_Microbit {
public:
Adafruit_Microbit_Matrix matrix;
Adafruit_Microbit_BLESerial BTLESerial;
Adafruit_Microbit_Matrix matrix; ///< 5x5 graphical matrix
Adafruit_Microbit_BLESerial BTLESerial; ///< Nordic UART service connection

void begin(void);

Expand Down

0 comments on commit c2768f7

Please sign in to comment.