Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task02: Implement hardware detector script #158

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions 01_git/scissors/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Makefile for Rock-Paper-Scissors game
# Author: ViacheslavHolubnychyi<[email protected]>

C=gcc
OBJS=RockPaperScissors.o

program: ${OBJS}
${C} -o program ${OBJS}

RockPaperScissors.o: RockPaperScissors.c
${C} -c RockPaperScissors.c

clean:
rm *.o program
118 changes: 118 additions & 0 deletions 01_git/scissors/RockPaperScissors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

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;
}

53 changes: 53 additions & 0 deletions 02_bash/hwdetect/hwdetect.sh
Original file line number Diff line number Diff line change
@@ -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