Blog Image


About the blog

In this blog I will keep track of projects I develop though out this year and may be in the future. For now it is juts a testing ground for developing the blog itself but I hope as I put more material it will become a good place for me to hold information.


C/C++ Programming Posted on 14 Aug, 2019 17:11:06

void SimplestColorBalance(cv::Mat inArray, cv::Mat &outArray, int percent){

if (percent<= 0)

percent = 5;

inArray.convertTo(inArray, CV_32F);

int rows = inArray.rows;

int cols = inArray.cols;

int chnls = inArray.channels();

double halfPercent = percent /200.0;

std::vector<cv::Mat> channels;

std::vector<cv::Mat> results;

if (chnls == 3){


cv::split(inArray, channels);


else {




for (int i = 0; i < chnls; i++){

cv::Mat flat;


cv::sort(flat, flat, cv::SORT_ASCENDING);

double lowVal =<float>(0, floor(flat.cols * halfPercent));

double topVal =<float>(0, ceil(flat.cols * (1.0-halfPercent)));

cv::Mat channel = channels[i];

for (int m = 0; m < rows; m++){

for (int n = 0; n < cols; n++){

if (<float>(m, n) < lowVal)<float>(m, n) = lowVal;

if (<float>(m, n) > topVal)<float>(m, n) = topVal;



cv::normalize(channel, channel, 0, 255, cv::NORM_MINMAX);

//channel.convertTo(channel, CV_32F);



cv::merge(results, outArray);

outArray.convertTo(outArray, CV_8U);



C/C++ Programming Posted on 19 Jul, 2019 16:49:05

LED 100W DC Power Supply

things Posted on 01 Jul, 2019 11:00:11

C# Parser

things Posted on 26 Jun, 2019 08:01:40

namespace CSharpParser


class Parser


private string m_strData;

private int m_nMaxLength;

private List<string> m_lData;

/*! \brief Constructor of the parser class.


public Parser()



m_lData = new List<string>();

m_nMaxLength = 256;


public void ClearData()


m_strData = “”;


/*! \brief Add data to the \ref m_strData variable and make sure it is not larger than \ref m_nMaxLength

* \param string to be added to \ref m_strData


public void AddData(string _data)


m_strData += _data;

if (m_strData.Length > m_nMaxLength)


m_strData = m_strData.Remove(m_strData.Length – m_nMaxLength);



/*! \brief Sor the data in \ref m_strData by splitting by ‘\r’ characters


public void SortData()


string[] _data = m_strData.Split(‘\r’);

foreach (string _str in _data)



/*! \brief Returnt the data in the \ref m_lData list for processing by inheritance or parent

* \return List<string> List of possible strings


public List<string> GetData()


return m_lData;




Interesting Links

things Posted on 30 May, 2019 20:42:42

Socket programming for windows and Linux, an on-line in depth book with tutorials:


Links to interesting channels in youtube

things Posted on 20 May, 2019 21:00:14

Tom Heylen Electronics Blog:

Lewis Loflin Electronics Blog:

Arduino Nano Propellant clock:

MFC/Cli/C# All Together Videos from ~Jun 2018:

Constant Current



Linux Posted on 09 Apr, 2019 15:07:55

MCP4725 12-Bit DAC Interface to Raspberry Pi

by Lewis Loflin

YouTube video see MCP4725 12-Bit DAC Interface to Raspberry Pi.

The MCP4725 is a 12-Bit Digital-to-Analog Converter with EEPROM Memory. Here I’ll connect the MCP4725 to Raspberry Pi and illustrate how to program the device. I’ll be Debian based Raspbian Linux.

Fig. 1 MCP4725 breakout board available from a number of vendors. The boards usually have pull up resistors that need to be disconnected or use a level translator which is what I did. The Raspberry Pi I2C buss is 3.3V while I operated mine at 5V.

The features of the MCP4725:

The MCP4725 is a low-power, high accuracy, single channel, 12-bit buffered voltage output Digital-to-Analog Converter (DAC) with non-volatile memory (EEPROM).

Its on-board precision output amplifier allows it to achieve rail-to-rail analog output swing. The DAC input and configuration data can be programmed to the non-volatile memory (EEPROM) by the user using I2C interface command.

The non-volatile memory feature enables the DAC device to hold the DAC input code during power-off time, and the DAC output is available immediately after power-up. This feature is very useful when the DAC device is used as a supporting device for other devices in the network.

The device includes a Power-On-Reset (POR) circuit to ensure reliable power-up and an on-board charge pump for the EEPROM programming voltage. The DAC reference is driven from VDD directly. In power-down mode, the output amplifier can be configured to present a low, medium, or high resistance output load.

Fig 2 MCP4725 internal block diagram.

Looking Closer

The MCP4725 DAC is a 12-bit device with values from 0-4095, with 4095 outputting a voltage near Vcc. First we write a control byte with the following specifications:

(Refer to page 18-19 spec sheet), uint8_t control_byte = 0b01000000; bits 7-5 are 010 write DAC only, 011 write DAC and EEPROM. We can write the 12-bit value to the DAC only or the DAC and the EEPROM. The values stored in the EEPROM sets the power up output voltage. This is byte 0.

If say 2048 is stored in the EEPROM the voltage output when power up is 1/2 Vcc.

The uint8_t is an unsigned 8-bit integer value that’s defined across multiple versions of C.

In the example here I write to the DAC only because there’s a delay writing to the EEPROM, in addition so many write cycles will shorten the life of the EEPROM.

Bits 4-3 are unused while bits 2-1 setup power down (PD) where a selected resistor value is switched from the output to ground. I set this value to open. Bit 0 is also unused.

The next two bytes are also uint8_t variables. The uint16_t val is an unsigned 16-bit integer value where one enters a value from 0-4095. The variable val is shifted four places right and loaded into byte 1 as the 8 MSB of val.

Then val is shifted four places left and loaded into byte 2 where bits 7-4 are the LSB if val while bits 3-0 are unused.

Then all three bytes are sent over I2C to the MCP4725 that outputs a voltage based on the value of val. After the three bytes are written the program exits.

// MCP4725.c
// Program the MCP4725 DAC
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> // read/write usleep
#include <stdlib.h> // exit function
#include <inttypes.h> // uint8_t, etc
#include <linux/i2c-dev.h> // I2C bus definitions

int fd;
int mcp4725_address = 0x62;
int16_t val;
uint8_t writeBuf[3];
float myfloat;

int main() {

// open device on /dev/i2c-1 the default on Raspberry Pi B
if ((fd = open(“/dev/i2c-1”, O_RDWR)) < 0) {
printf(“Error: Couldn’t open device! %d\n”, fd);
exit (1);

// connect to ads1115 as i2c slave
if (ioctl(fd, I2C_SLAVE, mcp4725_address) < 0) {
printf(“Error: Couldn’t find device on address!\n”);
exit (1);

// 12-bit device values from 0-4095
// page 18-19 spec sheet
writeBuf[0] = 0b01000000; // control byte
// bits 7-5; 010 write DAC; 011 write DAC and EEPROM
// bits 4-3 unused
// bits 2-1 PD1, PD0 PWR down P19 00 normal.
// bit 0 unused
writeBuf[1] = 0b00000000; // HIGH data
// bits 7-0 D11-D4
writeBuf[2] = 0b00000000; // LOW data
// bits 7-4 D3-D0
// bits 3-0 unused

// input number from 0-4095
// 2048 50% Vcc
char buffer [15];
printf (“Enter a number 0-4095: “);
scanf(“%s”, buffer); // string to
int val = atoi(buffer);
printf(“You entered %d “, val);

// write number to MCP4725 DAC
writeBuf[1] = val >> 4; // MSB 11-4 shift right 4 places
printf(“WriteBuf[1] = %d “, writeBuf[1]);
writeBuf[2] = val << 4; // LSB 3-0 shift left 4 places
printf(“WriteBuf[2] = %d \n”, writeBuf[2]);
if (write(fd, writeBuf, 3) != 3) {
perror(“Write to register 1”);
exit (1);
return 0;

All the information above has been copied from a webpage which is very informative and I recommend visiting:

Adding and making a .PC file for pkg-config

C/C++ Programming Posted on 27 Mar, 2019 10:04:49

Package OpenCV not found? Let’s Find It.

mainOpenCV has been refined over the years and installing it has become way more user-friendly now. If has been ported to many platforms and you don’t need an IDE to use it. There are many different ways in which you can install it and use it in your existing code. But sometimes, when people try to do different things with it, they run into problems. This post deals with one such problem. When you write code that uses OpenCV, you need to tell your compiler where it is. The reason for this is that this location will contains all the libraries and binaries necessary for the header files to work in your code. If you don’t specify this, you will encounter the following error:

Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc’
to the PKG_CONFIG_PATH environment variable
No package ‘opencv’ found

Where do we go from here?

The reason for the error is that when you try to compile your code from the command line, you need to tell it where to find OpenCV related stuff. People generally use pkg-config to take care of this because it’s easy to handle and it looks clean. Imagine doing the ‘-l’ library-includes every time you want to compile some code. Tedious! Just open your terminal and type the following:

$ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

This is the path to opencv.pc by default. If you have already installed OpenCV, you should already have opencv.pc somewhere on your machine. So in case you don’t have opencv.pc file in that path, just search for it and assign the path to the PKG_CONFIG_PATH variable:

$ export PKG_CONFIG_PATH=/path/to/the/file

And you are done! It’s better to add this line to your bashrc file so that you don’t have to do it every single time you reopen your terminal.

I can’t find my opencv.pc file, what do I do?

If you cannot find the opencv.pc file, don’t worry! Just create a new file called opencv.pc in /usr/local/lib/pkgconfig and add the following content to that file:


Name: opencv
Description: The opencv library
Version: 2.x.x
Cflags: -I${includedir}/opencv -I${includedir}/opencv2
Libs: -L${libdir} -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l

After creating this file, make sure the PKG_CONFIG_PATH variable contains this path and you are good to go.

How do I run my OpenCV code now?

Now that all that installing stuff is out of the way, let’s see how we can run your OpenCV code:

Install opencv on your system.
Unzip the zip file
Open the terminal and go to that folder
Type the following on your terminal to compile:

$ g++ main.cpp MyFile.h MyFile.cpp `pkg-config opencv –cflags –libs` -o main.o

Type the following on your terminal to run (you may need to chmod 777 main.o):

$ ./main.o



« PreviousNext »