From c7619a74937747cb79aa39ca36979fcd9954d5b3 Mon Sep 17 00:00:00 2001 From: SlavaGolubnichiy Date: Thu, 20 Jan 2022 19:03:11 +0200 Subject: [PATCH 1/3] Lesson1: Implement rock-paper-scissors game Added file RockPaperScissors.c with implemented functions: - randomize() which returns 32-bit pseudo-random number; - get_rps_id() which returns rps-object's id by it's symbol; - get_rps_symbol() which returns rps-object's symbol by it's id; - get_rps_string() which return rps-object's name by it's id; - rps_does_first_win() which implements rps-games rules; - main() which is program entry-point, contains program logic; Signed-off-by: SlavaGolubnichiy --- 01_git/scissors/RockPaperScissors.c | 118 ++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 01_git/scissors/RockPaperScissors.c diff --git a/01_git/scissors/RockPaperScissors.c b/01_git/scissors/RockPaperScissors.c new file mode 100644 index 0000000..773e75e --- /dev/null +++ b/01_git/scissors/RockPaperScissors.c @@ -0,0 +1,118 @@ +#include +#include +#include +#include + +uint32_t randomize(uint32_t range_min, uint32_t range_max) +{ + uint32_t* ptr1 = (uint32_t*)malloc(4); + uint32_t* ptr2 = (uint32_t*)malloc(4); + uint32_t pos_seed = (uint32_t) ptr1; + uint32_t src_seed = (uint32_t) ptr2; + free(ptr1); + free(ptr2); + uint8_t rshifts_num = pos_seed % (sizeof(src_seed)*8); + uint32_t random_num = (src_seed >> rshifts_num) % (range_max+1 - range_min) + range_min; + return random_num; +} + +uint8_t get_rps_id(char rps_symbol) +{ + switch(rps_symbol) + { + case 'r': + return 0; // rock + case 'p': + return 1; // paper + case 's': + return 2; // scissors + default: + return -1; // error-code + } +} + +char get_rps_symbol(uint8_t rps_id) +{ + switch(rps_id) + { + case 0: + return 'r'; // rock + case 1: + return 'p'; // paper + case 2: + return 's'; // scissors + default: + return 'E'; // error-code + } +} + +char* get_rps_string(uint8_t rps_id) +{ + switch(rps_id) + { + case 0: + return "rock"; // rock + case 1: + return "paper"; // paper + case 2: + return "scissors"; // scissors + default: + return "ERROR"; // error-code + } +} + +bool rps_does_first_win(uint8_t first, uint8_t second) +{ + if (first == 0 && second == 2) + { + return true; + } + else if (first == 2 && second == 0) + { + return false; + } + else + { + if (first >= second) + { + return true; + } + else if ((first == 0) && (second == 2)) // if (first = 'r' && second == 's') + { + return true; + } + else + { + return false; + } + } +} + +int main() +{ + uint8_t random_rps = (uint8_t) randomize(0, 2); + + printf("Welcome to the Rock-Paper-Scissors game.\n"); + printf("Choose:\n r - rock,\n p - paper,\n s - scissors.\n"); + printf("Your option: "); + char symbol = 0; + scanf("%c", &symbol); + uint8_t user_rps = get_rps_id(symbol); + + bool does_user_win = rps_does_first_win(user_rps, random_rps); + printf("My option was: %c.\n", get_rps_symbol(random_rps)); + if (does_user_win) + { + printf("You win: %s beats %s\n", get_rps_string(user_rps), get_rps_string(random_rps)); + } + else + { + printf("I win: %s beats %s\n", get_rps_string(random_rps), get_rps_string(user_rps)); + } + + printf("Try again!"); + + printf("\n\n"); + return 0; +} + From a61b8e17a1f0fcf18e3b62d10bfeba888d821378 Mon Sep 17 00:00:00 2001 From: SlavaGolubnichiy Date: Mon, 24 Jan 2022 14:56:42 +0200 Subject: [PATCH 2/3] Lesson1: Implement makefile Added file named Makefile, used to build an executable with gcc. Scenarios, implemented in Makefile: - program (builds object-files and an executable); - RockPaperScissors.o (builds RockPaperScissors.o object file); - clean (removes object files and an executable); Signed-off-by: SlavaGolubnichiy --- 01_git/scissors/Makefile | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100755 01_git/scissors/Makefile diff --git a/01_git/scissors/Makefile b/01_git/scissors/Makefile new file mode 100755 index 0000000..d602acc --- /dev/null +++ b/01_git/scissors/Makefile @@ -0,0 +1,14 @@ +# Makefile for Rock-Paper-Scissors game +# Author: ViacheslavHolubnychyi + +C=gcc +OBJS=RockPaperScissors.o + +program: ${OBJS} + ${C} -o program ${OBJS} + +RockPaperScissors.o: RockPaperScissors.c + ${C} -c RockPaperScissors.c + +clean: + rm *.o program From c8d01c2168ad966aa71f0edaa592ca607c758bb9 Mon Sep 17 00:00:00 2001 From: SlavaGolubnichiy Date: Tue, 25 Jan 2022 16:52:59 +0200 Subject: [PATCH 3/3] Task02: Implement hardware detector script Script detects following plugged hardware: - mounted devices using /dev/sd . location and type parameters - USB-devices . id, name - block devices . name, size, type, mountpoint - SCSI . name, hctl, type, vendor, model, rev, tran - bus devices . bus, device, class description - PCI . id, type, name, rev - I2C . bus, number of busses, their i2c devices Signed-off-by: SlavaGolubnichiy --- 02_bash/hwdetect/hwdetect.sh | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 02_bash/hwdetect/hwdetect.sh diff --git a/02_bash/hwdetect/hwdetect.sh b/02_bash/hwdetect/hwdetect.sh new file mode 100755 index 0000000..b3b8e2e --- /dev/null +++ b/02_bash/hwdetect/hwdetect.sh @@ -0,0 +1,53 @@ +#!/bin/bash + +#colors +YEL='\033[1;33m' +NC='\033[0m' # No Color + +echo -e "${YEL}This computer name: ${NC}" +hostname +echo "" + +echo -e "${YEL}Current directory: ${NC}" +pwd +echo "" + +echo -e "${YEL}Mounted devices: ${NC}" +mount | grep /dev/sd +echo "" + +echo -e "${YEL}USB devices: ${NC}" +lsusb +echo "" + +echo -e "${YEL}Block devices: ${NC}" +lsblk +echo "" +echo -e "${YEL}SCSI devices: ${NC}" +lsblk -S +echo "" + +echo -e "${YEL}Bus devices:${NC}" +sudo lshw -businfo +echo "" + +echo -e "${YEL}PCI devices: ${NC}" +lspci +echo "" + +echo -e "${YEL}I2C devices: ${NC}" + +#part1 +sudo lshw | grep -w "\-pci\|pci" + +#part2 +i2c_num=$(sudo i2cdetect -l | wc -l) # i2c_num = count number of lines in i2c busses list +((max_index= ${i2c_num} - 1)) # max_index = i2c_num - 1; + +echo " - number of i2c busses: $i2c_num" +for i in $(seq 0 ${max_index}) +do + echo " - bus $i:" + sudo i2cdetect -y ${i} + echo "" +done