Disabled external gits
This commit is contained in:
5
cs309-psoc/lab_4_1/sw/hps/application/client/Makefile
Normal file
5
cs309-psoc/lab_4_1/sw/hps/application/client/Makefile
Normal file
@@ -0,0 +1,5 @@
|
||||
LDLIBS= -lcaca
|
||||
CFLAGS= -Wall
|
||||
|
||||
main: main.o
|
||||
main.o: main.c
|
216
cs309-psoc/lab_4_1/sw/hps/application/client/main.c
Normal file
216
cs309-psoc/lab_4_1/sw/hps/application/client/main.c
Normal file
@@ -0,0 +1,216 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <caca.h>
|
||||
|
||||
#define ERR -1
|
||||
#define NO_ERR 0
|
||||
|
||||
#define M_REQUIRE(cond_, mess_, ...) \
|
||||
do{ \
|
||||
if(!(cond_)){ \
|
||||
fprintf(stderr, mess_, ##__VA_ARGS__); \
|
||||
return ERR; \
|
||||
} \
|
||||
}while(0);
|
||||
|
||||
#define M_REQUIRE_NO_ERR(v, mess_, ...)\
|
||||
M_REQUIRE(v==NO_ERR, mess_, ##__VA_ARGS__)
|
||||
|
||||
#define M_REQUIRE_NO_NULL(v, mess_, ...)\
|
||||
M_REQUIRE(v!=NULL, mess_, ##__VA_ARGS__)
|
||||
|
||||
#define MAX(a,b) ((a)>(b)?(a):(b))
|
||||
#define MIN(a,b) ((a)<(b)?(a):(b))
|
||||
|
||||
#define DEFAULT_PORT 25700
|
||||
|
||||
#define IM_WIDTH 80
|
||||
#define IM_HEIGHT 60
|
||||
#define IM_BPP 16
|
||||
#define IM_TOTAL IM_WIDTH*IM_HEIGHT
|
||||
|
||||
#define PCKT_SIZE (IM_WIDTH*sizeof(pix_t)+sizeof(uint32_t))
|
||||
|
||||
#define PX_MAX_V 0x3FFF
|
||||
|
||||
#define IM_MR 0xFF00
|
||||
#define IM_MG 0x0000
|
||||
#define IM_MB 0x00FF
|
||||
#define IM_MA 0x0000
|
||||
|
||||
#define IM_5B 0x1F
|
||||
|
||||
#define REF_DIV 1
|
||||
|
||||
|
||||
typedef uint16_t pix_t;
|
||||
typedef struct sockaddr_in saddr_t;
|
||||
|
||||
/*=================================================================================================*/
|
||||
|
||||
|
||||
pix_t IMG_IN[IM_TOTAL] = {0};
|
||||
caca_canvas_t* cv = NULL;
|
||||
caca_display_t* dp = NULL;
|
||||
caca_dither_t* dither;
|
||||
int ww = 0, wh = 0;
|
||||
uint64_t msg_cnt = 0;
|
||||
uint8_t refresh = 0;
|
||||
|
||||
char event = 0;
|
||||
|
||||
/*=================================================================================================*/
|
||||
|
||||
int setup_caca(){
|
||||
cv = caca_create_canvas(IM_WIDTH,IM_HEIGHT);
|
||||
M_REQUIRE_NO_NULL(cv, "Error: Unable to create caca canvas\n");
|
||||
dp = caca_create_display(cv);
|
||||
M_REQUIRE_NO_NULL(dp, "Error: Unable to create caca canvas\n");
|
||||
caca_set_display_title(dp,"CACA HEAT VIEWER");
|
||||
ww = caca_get_canvas_width(cv);
|
||||
wh = caca_get_canvas_height(cv);
|
||||
|
||||
#ifdef COLORING_RB
|
||||
dither = caca_create_dither(IM_BPP,IM_WIDTH,IM_HEIGHT,IM_WIDTH*sizeof(pix_t),IM_MR,IM_MG,IM_MB,IM_MA);
|
||||
#else
|
||||
dither = caca_create_dither(IM_BPP,IM_WIDTH,IM_HEIGHT,IM_WIDTH*sizeof(pix_t),IM_5B<<10,IM_5B<<5,IM_5B,IM_MA);
|
||||
#endif
|
||||
|
||||
caca_refresh_display(dp);
|
||||
return NO_ERR;
|
||||
}
|
||||
|
||||
int clear_caca(){
|
||||
caca_free_display(dp);
|
||||
caca_free_canvas(cv);
|
||||
return NO_ERR;
|
||||
}
|
||||
|
||||
int get_server_addr(const char* ip, const uint16_t port, struct sockaddr_in* const p_server_addr){
|
||||
assert(p_server_addr != NULL);
|
||||
|
||||
struct sockaddr_in server_addr = {0};
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_port = htons(port);
|
||||
if(ip == NULL){
|
||||
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
}else{
|
||||
M_REQUIRE(inet_aton(ip, &server_addr.sin_addr)== 1, "Error: Unable to set server adress (IP)\n");
|
||||
}
|
||||
*p_server_addr = server_addr;
|
||||
return NO_ERR;
|
||||
}
|
||||
|
||||
int bind_server(const int socket, const uint16_t port){
|
||||
struct sockaddr_in server_addr;
|
||||
M_REQUIRE_NO_ERR(get_server_addr(NULL, port, &server_addr), "Error: Unable to get server adress\n");
|
||||
M_REQUIRE_NO_ERR(bind(socket, (const struct sockaddr*) &server_addr, sizeof(server_addr)), " Error: Unable to bind socket\n");
|
||||
return NO_ERR;
|
||||
}
|
||||
|
||||
pix_t bernstein_rgb(float f){
|
||||
uint8_t r = (9*(1-f) *f *f *f)*IM_5B;
|
||||
uint8_t g = (15*(1-f) *(1-f) *f *f)*IM_5B;
|
||||
uint8_t b = (9*(1-f) *(1-f) *(1-f) *f)*IM_5B;
|
||||
return r<<10 | g<<5 | b;
|
||||
}
|
||||
|
||||
pix_t custom_rb(float f){
|
||||
uint8_t r = f*0xFF ;
|
||||
uint8_t b = (1-f)*0xFF;
|
||||
return r<<8 | b;
|
||||
}
|
||||
|
||||
void adjust_row(uint32_t row){
|
||||
for(size_t i = 0; i < IM_WIDTH; ++i){
|
||||
#ifdef COLORING_RB
|
||||
IMG_IN[row*IM_WIDTH+i] = custom_rb((float)IMG_IN[row*IM_WIDTH+i]/PX_MAX_V);
|
||||
#else
|
||||
IMG_IN[row*IM_WIDTH+i] = bernstein_rgb((float)IMG_IN[row*IM_WIDTH+i]/PX_MAX_V);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
int handle_message(ssize_t in_msg_len, uint8_t* in_msg){
|
||||
M_REQUIRE_NO_NULL(in_msg,"Error: input message is null\n");
|
||||
M_REQUIRE(in_msg_len >=0, "Error: server message timeout\n");
|
||||
M_REQUIRE(in_msg_len == PCKT_SIZE, "Error: message with wrong length (%zu)\n",in_msg_len);
|
||||
|
||||
uint32_t row = (uint32_t) in_msg[0];
|
||||
if(row < IM_HEIGHT){
|
||||
memcpy(IMG_IN + row*IM_WIDTH, in_msg + sizeof(uint32_t), IM_WIDTH*sizeof(pix_t));
|
||||
adjust_row(row);
|
||||
}
|
||||
if(row == IM_HEIGHT-1){
|
||||
refresh = 1;
|
||||
}
|
||||
|
||||
return NO_ERR;
|
||||
}
|
||||
|
||||
int main (int argc, char** argv){
|
||||
M_REQUIRE(argc==2,"Invalid usage of client. You should pass the address of the server.\n");
|
||||
|
||||
fprintf(stderr, "Starting Caca\n");
|
||||
M_REQUIRE_NO_ERR(setup_caca(),"Error: Creating Caca\n");
|
||||
memset(IMG_IN,0,IM_TOTAL*sizeof(pix_t));
|
||||
M_REQUIRE_NO_ERR(caca_dither_bitmap(cv,0,0,IM_WIDTH,IM_HEIGHT,dither,IMG_IN), "Error: Unable to draw\n");
|
||||
caca_refresh_display(dp);
|
||||
fprintf(stderr, "Started CACA HEAT VIEWER successfully\n");
|
||||
|
||||
fprintf(stderr, "Starting Network\n");
|
||||
int32_t s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
M_REQUIRE(s > 0, "Error: Unable to open socket\n");
|
||||
M_REQUIRE_NO_ERR(bind_server(s, 0), "Error: Unable to bind socket\n");
|
||||
saddr_t srv_addr;
|
||||
M_REQUIRE_NO_ERR(get_server_addr(argv[1], DEFAULT_PORT, &srv_addr), "Error: Unable to get server adress\n");
|
||||
fprintf(stderr, "Started Network\n");
|
||||
|
||||
fprintf(stderr, "Sending poke to server...\n");
|
||||
char e = 0;
|
||||
M_REQUIRE(sendto(s,&e,0,0,(struct sockaddr *)&srv_addr,sizeof(srv_addr))==0, " Error: Unable to send message to server\n");
|
||||
fprintf(stderr, "Sent poke successfully\n");
|
||||
uint8_t quit = 0;
|
||||
while(!feof(stdin) && !ferror(stdin) && !quit){
|
||||
caca_event_t ev;
|
||||
if(caca_get_event(dp, CACA_EVENT_RESIZE | CACA_EVENT_QUIT | CACA_EVENT_KEY_PRESS, &ev, 0)){
|
||||
if(caca_get_event_type(&ev) == CACA_EVENT_RESIZE){
|
||||
caca_set_canvas_size(cv,IM_WIDTH,IM_HEIGHT);
|
||||
}else if(caca_get_event_type(&ev) & CACA_EVENT_QUIT){
|
||||
quit = 1;
|
||||
}else {
|
||||
switch(caca_get_event_key_ch(&ev)){
|
||||
case 'Q':
|
||||
case 'q':
|
||||
quit=1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
uint8_t in_msg[PCKT_SIZE] = {0};
|
||||
ssize_t in_msg_len = recv(s, in_msg, PCKT_SIZE, 0);
|
||||
M_REQUIRE_NO_ERR(handle_message(in_msg_len, in_msg), "Error: Couldn't handle message\n");
|
||||
++msg_cnt;
|
||||
if (msg_cnt%(IM_HEIGHT/REF_DIV)==0 || refresh){
|
||||
refresh = 0;
|
||||
msg_cnt = 0;
|
||||
M_REQUIRE_NO_ERR(caca_dither_bitmap(cv,0,0,IM_WIDTH,IM_HEIGHT,dither,IMG_IN), "Error: Unable to draw\n");
|
||||
caca_refresh_display(dp);
|
||||
}
|
||||
}
|
||||
M_REQUIRE(sendto(s,&e,0,0,(struct sockaddr *)&srv_addr,sizeof(srv_addr))==0, " Error: Unable to send message to server\n");
|
||||
M_REQUIRE_NO_ERR(clear_caca(),"Error: Clearing Caca\n" );
|
||||
close(s);
|
||||
|
||||
return NO_ERR;
|
||||
}
|
Reference in New Issue
Block a user