# guivi

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.

## SimplestColorBalance

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){

channels.reserve(3);

cv::split(inArray, channels);

}

else {

channels.reserve(1);

inArray.copyTo(channels[0]);

}

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

cv::Mat flat;

channels[i].reshape(1,1).copyTo(flat);

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

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

double topVal = flat.at<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 (channel.at<float>(m, n) < lowVal)

channel.at<float>(m, n) = lowVal;

if (channel.at<float>(m, n) > topVal)

channel.at<float>(m, n) = topVal;

}

}

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

//channel.convertTo(channel, CV_32F);

results.push_back(channel);

}

cv::merge(results, outArray);

outArray.convertTo(outArray, CV_8U);

}

## MFC VfW

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

http://www.orgler.it/webcam.htm

## 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()

{

ClearData();

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

*/

{

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;

}

}

}

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

Socket programming for windows and Linux, an on-line in depth book with tutorials:
https://www.winsocketdotnetworkprogramming.com

Lock-in-amplifier
https://www.instructables.com/id/Lock-in-Amplifier/

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

Physiscs

## MCP4725

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 <stdlib.h> // exit function
#include <inttypes.h> // uint8_t, etc
#include <linux/i2c-dev.h> // I2C bus definitions

int fd;
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);
}
close(fd);
return 0;
}

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

http://www.bristolwatch.com/rpi/mcp4725.html

## Adding and making a .PC file for pkg-config

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

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:

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:

prefix=/usr
exec_prefix=${prefix} includedir=${prefix}/include
libdir=${exec_prefix}/lib 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:

$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