|
#include <cstdio> |
|
#include <cstdlib> |
|
#include <cstdint> |
|
#include <unistd.h> |
|
#include "ASICamera2.h" |
|
|
|
|
|
uint16_t exp_ms = 50; |
|
uint8_t bandwidth = 40; |
|
uint16_t gain = 250; |
|
|
|
uint32_t width = 8288; |
|
uint32_t height = 5644; |
|
uint16_t bin = 1; |
|
ASI_IMG_TYPE imageType = ASI_IMG_RAW16; |
|
|
|
uint8_t bytesPerPixel = 1 + ((imageType == ASI_IMG_RAW16) ? 1 : 0); |
|
uint32_t imgSize = width * height * bytesPerPixel; |
|
uint8_t *imgBuf = new uint8_t[imgSize]; |
|
|
|
|
|
bool getCameraInfo(uint8_t cameraIndex) { |
|
// Get camera info |
|
ASI_CAMERA_INFO info; |
|
ASIGetCameraProperty(&info, cameraIndex); |
|
printf("Using camera %s\n", info.Name); |
|
printf("Maximum resolution: %ld x %ld\n", info.MaxWidth, info.MaxHeight); |
|
return true; |
|
} |
|
|
|
bool openCamera(uint8_t cameraIndex) { |
|
printf("Opening the camera... "); |
|
ASI_ERROR_CODE openCode; |
|
if(ASI_SUCCESS != (openCode = ASIOpenCamera(cameraIndex))) |
|
{ |
|
printf("ERROR: FAILED TO OPEN CAMERA (%d)\n", openCode); |
|
return false; |
|
} |
|
printf("Done.\n"); |
|
return true; |
|
} |
|
|
|
bool initializeCamera(uint8_t cameraIndex) { |
|
printf("Initializing the camera... "); |
|
ASI_ERROR_CODE initCode; |
|
if(ASI_SUCCESS != (initCode = ASIInitCamera(cameraIndex))) |
|
{ |
|
printf("ERROR: FAILED TO INIT CAMERA (%d)\n", initCode); |
|
return false; |
|
} |
|
printf("Done.\n"); |
|
return true; |
|
} |
|
|
|
bool setCameraControlValues(uint8_t cameraIndex) { |
|
printf("Setting control values... "); |
|
ASI_ERROR_CODE expCode = ASISetControlValue(cameraIndex, ASI_EXPOSURE, exp_ms * 1000, ASI_FALSE); |
|
ASI_ERROR_CODE bwCode = ASISetControlValue(cameraIndex, ASI_BANDWIDTHOVERLOAD, bandwidth, ASI_FALSE); |
|
ASI_ERROR_CODE gainCode = ASISetControlValue(cameraIndex, ASI_GAIN, gain, ASI_FALSE); |
|
if(ASI_SUCCESS != expCode || ASI_SUCCESS != bwCode || ASI_SUCCESS != gainCode) |
|
{ |
|
printf("ERROR: FAILED TO SET CONTROL VALUE: (%d ; %d ; %d)\n", expCode, bwCode, gainCode); |
|
return false; |
|
} |
|
printf("Done.\n"); |
|
return true; |
|
} |
|
|
|
bool setCameraROI(uint8_t cameraIndex) { |
|
printf("Setting Camera ROI... "); |
|
ASI_ERROR_CODE roiCode; |
|
if(ASI_SUCCESS != (roiCode = ASISetROIFormat(cameraIndex, width, height, bin, imageType))) |
|
{ |
|
printf("ERROR: FAILED TO SET ROI: %d\n", roiCode); |
|
return false; |
|
} |
|
printf(" Done.\n"); |
|
return true; |
|
} |
|
|
|
bool takeCameraExposure(uint8_t cameraIndex) { |
|
printf("Starting the exposure... "); |
|
ASI_ERROR_CODE exposureCode = ASIStartExposure(cameraIndex, ASI_FALSE); |
|
if(ASI_SUCCESS != exposureCode) { |
|
printf("ERROR: START EXPOSURE FAILED: %d\n", exposureCode); |
|
return false; |
|
} |
|
printf("Done.\n"); |
|
|
|
printf("Waiting for the exposure to complete..."); |
|
fflush(stdout); |
|
ASI_EXPOSURE_STATUS status = ASI_EXP_WORKING; |
|
while (ASI_EXP_WORKING == status) |
|
{ |
|
usleep(100000); |
|
printf("."); |
|
fflush(stdout); |
|
ASIGetExpStatus(cameraIndex, &status); |
|
} |
|
if(ASI_EXP_SUCCESS != status) { |
|
printf("ERROR: EXPOSURE FAILED: %d\n", status); |
|
return false; |
|
} |
|
printf(" Done.\n"); |
|
return true; |
|
} |
|
|
|
bool retrieveCameraImage(uint8_t cameraIndex) { |
|
printf("Retrieving image from camera into buffer... "); |
|
ASI_ERROR_CODE retrieveCode; |
|
if(ASI_SUCCESS != (retrieveCode = ASIGetDataAfterExp(cameraIndex, imgBuf, imgSize))) { |
|
printf("ERROR: RETRIEVE FAILED: %d\n", retrieveCode); |
|
return false; |
|
} |
|
printf(" Done.\n"); |
|
return true; |
|
} |
|
|
|
void close(uint8_t cameraIndex) { |
|
printf("Stopping exposure and closing camera... "); |
|
ASIStopExposure(cameraIndex); |
|
ASICloseCamera(cameraIndex); |
|
printf("Done.\n"); |
|
} |
|
|
|
int main(int argc, char **argv) { |
|
|
|
// Hard-wired to first (and only) camera |
|
uint8_t cameraIndex = 0; |
|
|
|
uint8_t cameraCount = ASIGetNumOfConnectedCameras(); |
|
printf("Camera count: %d\n", cameraCount); |
|
|
|
if(!getCameraInfo(cameraIndex)) { |
|
return EXIT_FAILURE; |
|
} |
|
|
|
if(!openCamera(cameraIndex)) { |
|
return EXIT_FAILURE; |
|
} |
|
|
|
if(!initializeCamera(cameraIndex)) { |
|
close(cameraIndex); |
|
return EXIT_FAILURE; |
|
} |
|
|
|
if(!setCameraControlValues(cameraIndex)) { |
|
close(cameraIndex); |
|
return EXIT_FAILURE; |
|
} |
|
|
|
if(!setCameraROI(cameraIndex)) { |
|
close(cameraIndex); |
|
return EXIT_FAILURE; |
|
} |
|
|
|
if(!takeCameraExposure(cameraIndex)) { |
|
close(cameraIndex); |
|
return EXIT_FAILURE; |
|
} |
|
|
|
if(!retrieveCameraImage(cameraIndex)) { |
|
close(cameraIndex); |
|
return EXIT_FAILURE; |
|
} |
|
|
|
printf("Buffer size: %d\n", imgSize); |
|
printf("First few bytes:\n"); |
|
for(uint8_t i = 0; i < 4; i++) { |
|
printf("\t"); |
|
for(uint8_t j = 0; j < 8; j++) { |
|
printf("%02x ", imgBuf[i * 8 + j]); |
|
} |
|
printf("\n"); |
|
} |
|
|
|
close(cameraIndex); |
|
|
|
return EXIT_SUCCESS; |
|
} |