Skip to content

Commit

Permalink
capture-screen and serial comm added in primitive
Browse files Browse the repository at this point in the history
  • Loading branch information
sengulhamza committed Mar 12, 2024
1 parent e960203 commit dcebd14
Show file tree
Hide file tree
Showing 10 changed files with 365 additions and 0 deletions.
Binary file added capture_screen/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions capture_screen/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# build dir
build
20 changes: 20 additions & 0 deletions capture_screen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.10)

set(CMAKE_OSX_ARCHITECTURES arm64)

message("-- Cmake running on platform: " ${CMAKE_HOST_SYSTEM_NAME})

project(capture_screen)

# add the sources here
set(sources
#src/main.c
src/serial_comm.c
#src/capture_api.c
src/hex_dump.c
)

add_executable(capture_screen ${sources})

target_include_directories(capture_screen PRIVATE inc/)
target_link_libraries(capture_screen PRIVATE "-framework CoreGraphics -pthread")
1 change: 1 addition & 0 deletions capture_screen/code-format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./tools/format.sh --recursive "*.c" "*.h"
27 changes: 27 additions & 0 deletions capture_screen/inc/binary_tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef _BINARY_TREE_H_
#define _BINARY_TREE_H_

#include <stdint.h>

///Node structure of binary tree
struct node_t {
struct node_t *left_child;
struct node_t *right_child;
};

typedef struct node_t node;

#ifdef __cplusplus
extern "C" {
#endif

node *bin_tr_new_node(void);
void bin_tr_print_node(node *root_node);
node *bin_tr_insert_node(node *node);
uint8_t bin_tr_is_anomaly(node *root_node);

#ifdef __cplusplus
}
#endif

#endif
18 changes: 18 additions & 0 deletions capture_screen/inc/hex_dump.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef _HEX_DUMP_H_
#define _HEX_DUMP_H_

#include <stdint.h>

#define VARIABLE_PTR_STRGFY(var) var, #var

#ifdef __cplusplus
extern "C" {
#endif

void hex_dump(uint8_t *buffer, char *var_name, size_t len);

#ifdef __cplusplus
}
#endif

#endif
37 changes: 37 additions & 0 deletions capture_screen/src/hex_dump.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>

void hex_dump(uint8_t *buffer, char *var_name, size_t len)
{
printf("\r\n%s - length:%zu\r\n", var_name, len);
printf("HEX\t\t\t\t\t\t ASCII\n");
printf("---\t\t\t\t\t\t -----\n");
for (int i = 0; i < len; i++) {
printf("%02x ", buffer[i]);
if ((i + 1) % 8 == 0) {
printf(" ");
} \
if ((i + 1) % 16 == 0) {
printf(" ");
for (int j = i - 15; j <= i; j++) {
printf("%c", buffer[j]);
}
printf("\n");
}
}
if (len % 16 != 0) {
int remaining = len % 16;
for (int i = 0; i < 16 - remaining; i++) {
printf(" ");
if ((i + 1 + len - remaining) % 8 == 0) {
printf(" ");
}
}
printf(" ");
for (int i = len - remaining; i < len; i++) {
printf("%c", buffer[i]);
} \
printf("\n");
}
}
60 changes: 60 additions & 0 deletions capture_screen/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <stdio.h>
#include <stdlib.h>
#include <ApplicationServices/ApplicationServices.h>

void CaptureScreen(const char *outputFileName)
{
// Get the main screen dimensions
CGRect screenBounds = CGDisplayBounds(CGMainDisplayID());
int screenWidth = (int)CGRectGetWidth(screenBounds);
int screenHeight = (int)CGRectGetHeight(screenBounds);

// Create a bitmap context for the screenshot
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, screenWidth, screenHeight, 8, screenWidth * 4, colorSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);

// Capture the screen into the bitmap context
CGImageRef screenshot = CGDisplayCreateImage(CGMainDisplayID());
CGContextDrawImage(context, screenBounds, screenshot);
CGImageRelease(screenshot);

// Get the bitmap data
unsigned char *pixels = CGBitmapContextGetData(context);
printf("RES: %d x %d\r\n", screenWidth, screenHeight);

// Iterate through the pixel data and print RGB values to the console
for (int y = 1331; y < screenHeight; ++y) {
for (int x = 0; x < screenWidth; ++x) {
// Calculate the index of the current pixel in the pixel array
int index = y * screenWidth * 4 + x * 4;

// Extract RGB values
unsigned char red = pixels[index + 2];
unsigned char green = pixels[index + 1];
unsigned char blue = pixels[index];

if (1) {
// Print RGB values to the console
printf("Pixel at (%d, %d): R=%u, G=%u, B=%u\n", x, y, red, green, blue);
}
}
}

// Save or process the pixel data as needed
// Example: Save to a file
FILE *outputFile = fopen(outputFileName, "wb");
if (outputFile != NULL) {
fwrite(pixels, 1, screenWidth * screenHeight * 4, outputFile);
fclose(outputFile);
}

// Cleanup
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
}

int main()
{
CaptureScreen("screenshot.bmp");
return 0;
}
185 changes: 185 additions & 0 deletions capture_screen/src/serial_comm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

#define MAX_QUEUE_SIZE 256

typedef struct {
char data[MAX_QUEUE_SIZE];
size_t front;
size_t rear;
pthread_mutex_t mutex;
pthread_cond_t not_empty;
} Queue;

void initializeQueue(Queue *queue)
{
queue->front = 0;
queue->rear = 0;
pthread_mutex_init(&queue->mutex, NULL);
pthread_cond_init(&queue->not_empty, NULL);
}

void enqueue(Queue *queue, char item)
{
pthread_mutex_lock(&queue->mutex);

while ((queue->rear + 1) % MAX_QUEUE_SIZE == queue->front) {
pthread_cond_wait(&queue->not_empty, &queue->mutex);
}

queue->data[queue->rear] = item;
queue->rear = (queue->rear + 1) % MAX_QUEUE_SIZE;

pthread_cond_signal(&queue->not_empty);
pthread_mutex_unlock(&queue->mutex);
}

char dequeue(Queue *queue)
{
pthread_mutex_lock(&queue->mutex);

while (queue->front == queue->rear) {
pthread_cond_wait(&queue->not_empty, &queue->mutex);
}

char item = queue->data[queue->front];
queue->front = (queue->front + 1) % MAX_QUEUE_SIZE;

pthread_cond_signal(&queue->not_empty);
pthread_mutex_unlock(&queue->mutex);

return item;
}

int openSerialPort(const char *portName)
{
int serialPort = open(portName, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (serialPort == -1) {
perror("Error opening serial port");
exit(EXIT_FAILURE);
}
return serialPort;
}

void configureSerialPort(int serialPort, speed_t baudRate)
{
struct termios tty;
if (tcgetattr(serialPort, &tty) != 0) {
perror("Error getting serial port attributes");
close(serialPort);
exit(EXIT_FAILURE);
}

cfsetospeed(&tty, baudRate);
cfsetispeed(&tty, baudRate);

tty.c_cflag &= ~PARENB;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;

tty.c_cflag &= ~CRTSCTS;
tty.c_cflag |= CREAD | CLOCAL;

tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 1;

if (tcsetattr(serialPort, TCSANOW, &tty) != 0) {
perror("Error setting serial port attributes");
close(serialPort);
exit(EXIT_FAILURE);
}
}

void *senderTask(void *arg)
{
printf("%s task started!\r\n", __func__);

Queue *sendQueue = (Queue *)arg;

int serialPort = openSerialPort("/dev/tty.usbmodemmeplight1"); // Replace with your serial port
configureSerialPort(serialPort, B230400);

while (1) {
//char dataToSend = dequeue(sendQueue);
char *test_write_buff = "test_write_ buffer hamza test. 1 2 3 4 5 6 7 8 9 0\r\n";

write(serialPort, test_write_buff, strlen(test_write_buff));
usleep(1000000); // Adjust sleep time to 1 second
printf("aaa\r\n");
}
}

void *receiverTask(void *arg)
{

printf("%s task started!\r\n", __func__);

Queue *receiveQueue = (Queue *)arg;

int serialPort = openSerialPort("/dev/tty.usbmodemmeplight1"); // Replace with your serial port
configureSerialPort(serialPort, B230400);

while (1) {
char receivedData;
ssize_t bytesRead = read(serialPort, &receivedData, 1);
if (bytesRead > 0) {
enqueue(receiveQueue, receivedData);
}
}
}

void *readProcessTask(void *arg)
{

printf("%s task started!\r\n", __func__);

Queue *readQueue = (Queue *)arg;

while (1) {
char processedData = dequeue(readQueue);
// Process the received data (replace this with your processing logic)
printf("%c", processedData);
}
}

int main()
{
Queue sendQueue, receiveQueue, readQueue;
initializeQueue(&sendQueue);
initializeQueue(&receiveQueue);
initializeQueue(&readQueue);

pthread_t senderThread, receiverThread, readProcessThread;

pthread_create(&senderThread, NULL, senderTask, (void *)&sendQueue);
pthread_create(&receiverThread, NULL, receiverTask, (void *)&receiveQueue);
pthread_create(&readProcessThread, NULL, readProcessTask, (void *)&receiveQueue);

pthread_join(senderThread, NULL);
pthread_join(receiverThread, NULL);
pthread_join(readProcessThread, NULL);
printf("aaa");

while (1) {
printf("aaa");
sleep(1);
}

// Cleanup: Destroy mutex and condition variables
pthread_mutex_destroy(&sendQueue.mutex);
pthread_cond_destroy(&sendQueue.not_empty);
pthread_mutex_destroy(&receiveQueue.mutex);
pthread_cond_destroy(&receiveQueue.not_empty);
pthread_mutex_destroy(&readQueue.mutex);
pthread_cond_destroy(&readQueue.not_empty);

return 0;
}
15 changes: 15 additions & 0 deletions capture_screen/tools/format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
astyle \
--style=otbs \
--attach-namespaces \
--attach-classes \
--indent=spaces=4 \
--convert-tabs \
--align-pointer=name \
--align-reference=name \
--keep-one-line-statements \
--pad-header \
--pad-oper \
--pad-comma \
--unpad-paren \
--suffix=none \
"$@"

0 comments on commit dcebd14

Please sign in to comment.