9 years, 4 months ago.

How to construct a library with digital out pins.

I want to construct a library (properly for Mbed) that drives a LCD display using 3 DigitalOut pins.

I can add my code in later but not sure of the correct way to set this out and define the pins in the constructor. I'm using the following files:

main program

#include "mbed.h"
#include "9digitLCD.h"

9digitLCD lcd(PTC0,PTC1,PTD6); // WO, CS, DATA

int main()
{
     
    while(1){}
    
}   

9digitLCD.h

#include "mbed.h"



class 9digitLCD : public Stream {
    
public:

    9digitLCD(PinName WO, PinName CS, PinName DATA); // Default constructor
  


private:
    
    DigitalOut  WO;
    DigitalOut  CS;
    DigitalOut  DATA;   
};


9digitLCD.cpp

#include "9digitLCD.h"


9digitLCD::9digitLCD(PinName WO, PinName CS, PinName DATA) : _WO(pin),_CS(pin),_DATA(pin){initialise_display();}





Can anyone help just to get me started with this Generally I chop and change existing libraries, but its about time I do my own.

Many thanks

Paul

1 Answer

9 years, 4 months ago.

Hi, Paul-san,

Following is my trial.

Typing 0 from the terminal turn LED to white, 7 to dim...

#ifndef _RGBLED_H_
#define _RGBLED_H_

class RGBLED {
public:
    RGBLED(PinName RedPin, PinName GreenPin, PinName BluePin) ;
    ~RGBLED() ;
    
    void write(int value) ;
    int read(void) ;
    
    #ifdef MBED_OPERATORS
    /** A shorthand for write()
     */
    RGBLED& operator= (int value) {
        write(value);
        return *this;
    }

    RGBLED& operator= (RGBLED& rhs) {
        write(rhs.read());
        return *this;
    }

    /** A shorthand for read()
     */
    operator int() {
        return read();
    }
#endif

private:
    DigitalOut _R ;
    DigitalOut _G ;
    DigitalOut _B ;
} ;
#endif /* _RGBLED_H_ */

#include "mbed.h"
#include "RGBLED.h"

RGBLED::RGBLED(PinName RedPin, PinName GreenPin, PinName BluePin) : _R(RedPin), _G(GreenPin), _B(BluePin)
{
    // for the initial value, I want dim all three
    _R = 1 ;
    _G = 1 ;
    _B = 1 ;
}

RGBLED::~RGBLED() 
{
    // same reason with the constructor
    _R = 1 ;
    _G = 1 ;
    _B = 1 ;
}

void RGBLED::write(int value) 
{
    _R = (value >> 2) & 0x01 ;
    _G = (value >> 1) & 0x01 ;
    _B = value & 0x01 ;
}

int RGBLED::read(void)
{
    int result ;
    result = 0x4 * _R + 0x2 * _G + _B ;
    return( result ) ;
}

#include "mbed.h"
#include "RGBLED.h"

RGBLED rgbled(PTB18, PTB19, PTD1) ; // for FRDM-KL25Z

int main() {
    int value ;
    while(1) {
        printf("> ") ;
        scanf("%d", &value) ;
        rgbled = value ;
    }
}

moto

Thank you Motoo :) I will use this example to build my code and let you know how I get on.

edit....

That worked fine, all done now, here is the result:

https://developer.mbed.org/users/star297/code/HT1621_16seg_LCD/

I took out the "~ RGBLED() ;"

Not sure why or if this is needed or why it would be needed, something to do with 'Bitwise NOT' but I can't find an example explanation for use in constructing a library.

posted by Paul Staron 14 Jan 2015

If you were asking why I added "RGBLED()", I added it only to turn all LED off. With my FRDM-KL25Z, sometimes LED left on as white, which is too dazzling for me, so I decided to turn it off, in case the destructor is called. moto

posted by Motoo Tanaka 18 Jan 2015