#include "capcherimage.h"
void CapcherImage::run(){
unsigned char img[256*256]={0};
qDebug("Image Thread start!"); //"start!!"
emit state(TREAD_START);
while(1)
{
if (m_pause)
{
emit state(TREAD_PAUSE); //"complete!!"
m_pauseCondition.wait(&mutex);
emit state(TREAD_RESUME);//"start!!"
}
//running code
arduinoSAWTOOTHWAVE(ENABLE);
readOneFrame(img);
arduinoSAWTOOTHWAVE(DISABLE);
emit state(TREAD_RUNNING); //"image make..."
if(makeJPEG(img) == ERROR){
m_pause = TRUE;
qDebug("make image error!!");
continue;
}
emit state(DISPLAY_IMAGE);
//running code end
m_pause = TRUE;
}
emit state(TREAD_END); //"complete!!"
qDebug("Image Thread stop!");
}
void CapcherImage::arduinoSAWTOOTHWAVE(int command){
if(command == ENABLE){
digitalWrite(ARDUINO_ENABLE_PIN, ENABLE);
}else if(command == DISABLE){
digitalWrite(ARDUINO_ENABLE_PIN, DISABLE);
}
}
void CapcherImage::readOneFrame(unsigned char *oneFrame){
int raw = 0, col=0;
long int start_time;
long int time_difference;
struct timespec gettime_now;
int percent = 0;
int lock = ON;
long int in_start_time;
long int in_time_difference;
struct timespec in_gettime_now;
s_p_signal=STOP;
imageMakingProgress(percent);
gpio_8bit_usage(ENABLE);
//while start
while(1){
//measure strat time
clock_gettime(CLOCK_REALTIME, &gettime_now);
start_time = gettime_now.tv_nsec;
if(digitalRead(S_P_SIGNAL_PIN) == STOP){
lock = OFF;
}
if(digitalRead(S_P_SIGNAL_PIN) == START && lock == OFF){
lock = ON;
/*
//measure strat time
clock_gettime(CLOCK_REALTIME, &gettime_now);
start_time = gettime_now.tv_nsec;
*/
//speed control
// 1) 500
// 2) 30
//usleep(30);
//mearsure end time
do{
clock_gettime(CLOCK_REALTIME, &gettime_now);
time_difference = gettime_now.tv_nsec - start_time;
if(time_difference<0)
time_difference += 1000000000;
//qDebug("msec : %lf", time_difference/1000000.0);
}while(time_difference < 100000);
//synchronization test
digitalWrite(SYNCHRONIZATION_PIN, START);
while(1){
//measure strat time
clock_gettime(CLOCK_REALTIME, &in_gettime_now);
in_start_time = in_gettime_now.tv_nsec;
//read
oneFrame[raw*256 + col] = Read_8bit_GPIO();
col++;
//speed control
// 1) 410
// 2) 160
//usleep(160);
if(col == 256){
//synchronization test
digitalWrite(SYNCHRONIZATION_PIN, STOP);
/*
//mearsure end time
clock_gettime(CLOCK_REALTIME, &gettime_now);
time_difference = gettime_now.tv_nsec - start_time;
if(time_difference<0)
time_difference += 1000000000;
qDebug("msec : %lf", time_difference/1000000.0);
*/
col =0;
raw++;
if((raw+4) % 26 == 0){
int percent = (raw+4)/26;
imageMakingProgress(percent); //take 50 usec
}
break;
}
//mearsure end time
do{
clock_gettime(CLOCK_REALTIME, &in_gettime_now);
in_time_difference = in_gettime_now.tv_nsec - in_start_time;
if(in_time_difference<0)
in_time_difference += 1000000000;
//qDebug("msec : %lf", in_time_difference/1000000.0);
}while(in_time_difference < 234000);
}
}
if(raw == 256){
break;
}
}
//while end
gpio_8bit_usage(DISABLE);
}
int CapcherImage::makeJPEG(unsigned char *oneFrame){
const int image_height=256, image_width=256;
const char *filename = "result.jpeg";
//initialize image array
/*
for(int i=0; i<256*256; i++){
oneFrame[i] = 200;
}*/
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE *outfile;
JSAMPROW row_pointer[1];
int row_stride;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
outfile = fopen(filename, "wb");
if(outfile == NULL){
fprintf(stderr, "can't open %s\n", filename);
exit(1);
}
jpeg_stdio_dest(&cinfo, outfile);
cinfo.image_width = image_width;
cinfo.image_height = image_height;
cinfo.input_components = 1;
cinfo.in_color_space = JCS_GRAYSCALE;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, 75, TRUE);
jpeg_start_compress(&cinfo, TRUE);
row_stride = image_width * 1;
while(cinfo.next_scanline < cinfo.image_height){
row_pointer[0] = &oneFrame[cinfo.next_scanline * row_stride];
(void)jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);
fclose(outfile);
qDebug("image file is created");
return 0;
}
void CapcherImage::init_Interrupt(){
pinMode(S_P_SIGNAL_PIN, INPUT);
pullUpDnControl(S_P_SIGNAL_PIN, PUD_UP);
pinMode(SYNCHRONIZATION_PIN, OUTPUT);
/*
if ( wiringPiISR (S_P_SIGNAL_PIN, INT_EDGE_FALLING, &interruptISR) < 0 ) {
fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
exit(-1);
}
*/
}
void interruptISR(void) {
char val = digitalRead(S_P_SIGNAL_PIN);
if(val == LOW){
s_p_signal = START;
//qDebug("falling edge detected");
}
else if(val == HIGH){
s_p_signal = STOP;
//qDebug("rising edge detected");
}
}
|