diff --git a/.github/.gitignore b/.github/.gitignore deleted file mode 100644 index 8b13789..0000000 --- a/.github/.gitignore +++ /dev/null @@ -1 +0,0 @@ - diff --git a/.github/actions/avr_setup/action.yml b/.github/actions/avr_setup/action.yml new file mode 100644 index 0000000..3b92db0 --- /dev/null +++ b/.github/actions/avr_setup/action.yml @@ -0,0 +1,18 @@ +name: "Get changed files list" +description: "Setup Node with caching for dependencies" +runs: + using: "composite" + steps: + - name: Set up Arduino CLI + uses: arduino/setup-arduino-cli@v2 + + - name: Install platform + run: | + arduino-cli core update-index + arduino-cli core install ${{ env.platform }} + shell: bash + + - name: Install popular libs + run: | + arduino-cli lib install LiquidCrystal + shell: bash \ No newline at end of file diff --git a/.github/actions/get_changed_files/action.yml b/.github/actions/get_changed_files/action.yml new file mode 100644 index 0000000..ecf73ac --- /dev/null +++ b/.github/actions/get_changed_files/action.yml @@ -0,0 +1,13 @@ +name: "Get changed files list" +description: "Setup Node with caching for dependencies" +runs: + using: "composite" + steps: + - name: Get changed files list + id: get_changed_files + run: | + echo "Changed files:" + git diff --name-only ${{ github.event.pull_request.base.sha }} > changed_files.txt + cat changed_files.txt + echo "The list is saved to changed_files.txt" + shell: bash \ No newline at end of file diff --git a/.github/workflows/Lab_01_CI.yml b/.github/workflows/Lab_01_CI.yml deleted file mode 100644 index 985573b..0000000 --- a/.github/workflows/Lab_01_CI.yml +++ /dev/null @@ -1,37 +0,0 @@ -name: Cheking of compile Arduino sketch for AVR/AtMega and ESP8266 - -on: - push: - branches: [ main, master ] - pull_request: - branches: [ main, master ] - -jobs: - test-matrix: - strategy: - matrix: - arduino-platform: - - "arduino:avr" - - "esp8266:esp8266" - include: - - arduino-platform: "arduino:avr" - fqbn: "arduino:avr:mega" - - arduino-platform: "esp8266:esp8266" - fqbn: "esp8266:esp8266:generic" - - runs-on: ubuntu-22.04 - - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Set up Arduino CLI - uses: arduino/setup-arduino-cli@v2 - - - name: Install platform - run: | - arduino-cli core update-index - arduino-cli core install ${{ matrix.arduino-platform }} - - - name: Compile Sketch - run: arduino-cli compile --fqbn ${{ matrix.fqbn }} ./mc_labs/mc_lab_01/*.ino diff --git a/.github/workflows/lab-validation.yml b/.github/workflows/lab-validation.yml new file mode 100644 index 0000000..059343b --- /dev/null +++ b/.github/workflows/lab-validation.yml @@ -0,0 +1,114 @@ +name: Cheking of compile Arduino sketch for AVR/AtMega + +on: + pull_request: + branches: [main, master] + +env: + platform: "arduino:avr" + fqbn_master: "arduino:avr:mega" + COMMIT_COUNT: $(( ${{ github.event_name == 'pull_request' && github.event.pull_request.commits || 0 }} + 1 )) + +jobs: + handle_bad_branch_name: + runs-on: ubuntu-22.04 + if: (contains(github.head_ref, 'mc_lab_1') || contains(github.head_ref, 'mc_lab_2') || contains(github.head_ref, 'mc_lab_3') || contains(github.head_ref, 'mc_lab_4') || contains(github.head_ref, 'mc_lab_5') || contains(github.head_ref, 'mc_lab_6') || contains(github.head_ref, 'mc_lab_7')) == false + steps: + - name: Fail the build + run: | + echo "The branch name is not correct. It should contain 'mc_lab_' prefix" + exit 1 + build_labs_1_to_4: + runs-on: ubuntu-22.04 + if: contains(github.head_ref, 'mc_lab_1') || contains(github.head_ref, 'mc_lab_2') || contains(github.head_ref, 'mc_lab_3') || contains(github.head_ref, 'mc_lab_4') + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: ${{ env.COMMIT_COUNT }} + + - name: Get changed files list + uses: ./.github/actions/get_changed_files + + - name: Set up Arduino CLI + uses: ./.github/actions/avr_setup + + - name: Compile Sketch + run: arduino-cli compile --fqbn ${{ env.fqbn_master }} $(grep -E '\.ino$' changed_files.txt | xargs) + build_lab_5: + runs-on: ubuntu-22.04 + if: contains(github.head_ref, 'mc_lab_5') + env: + fqbn_slave: "arduino:avr:nano" + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: ${{ env.COMMIT_COUNT }} + + - name: Get changed files list + uses: ./.github/actions/get_changed_files + + - name: Get master folder + run: | + cat changed_files.txt | xargs dirname | grep 'master' | grep -m 1 -vE '/(.*master.*|.*slave.*)/' > master_project.txt + echo "Master project:" + cat master_project.txt + + - name: Get slave folders + run: | + cat changed_files.txt | xargs dirname | grep 'slave' | grep -vE '/(.*master.*|.*slave.*)/' > slave_projects.txt + echo "Slave projects:" + cat slave_projects.txt + + - name: Check if there is at least one master and one slave project + run: | + if [ ! -s master_project.txt ] || [ ! -s slave_projects.txt ]; then + echo "There is no master or slave project" + exit 1 + fi + + - name: Set up Arduino CLI + uses: ./.github/actions/avr_setup + + - name: Compile master + run: while read master_folder; do arduino-cli compile --fqbn ${{ env.fqbn_master }} $master_folder/*.ino; done < master_project.txt + + - name: Compile slaves + run: while read slave_folder; do arduino-cli compile --fqbn ${{ env.fqbn_slave }} $slave_folder/*.ino; done < slave_projects.txt + build_lab_6: + runs-on: ubuntu-22.04 + if: contains(github.head_ref, 'mc_lab_6') + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: ${{ env.COMMIT_COUNT }} + + - name: It just passes + run: echo "It just passes. It's too complex" + build_lab_7: + runs-on: ubuntu-22.04 + if: contains(github.head_ref, 'mc_lab_7') + env: + register-bindings: "m2560def.inc" + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: ${{ env.COMMIT_COUNT }} + + - name: Get changed files list + uses: ./.github/actions/get_changed_files + + - name: Setup AVRA Assembler + run: | + git clone https://github.com/Ro5bert/avra.git + cd avra + sudo make install + + - name: Preprocess sketch - append register bindings to the top of the file + run: printf ".include \"${{ env.register-bindings }}\"\n\n" | cat - $(grep -m 1 -E '\.(asm|S)$' changed_files.txt | xargs) > pipeline_main_assembly_source_file.asm + + - name: Compile Sketch + run: avra pipeline_main_assembly_source_file.asm diff --git a/mc_labs/mc_lab_04/lab4-mko/keypad4x4.h b/mc_labs/mc_lab_04/lab4-mko/keypad4x4.h new file mode 100644 index 0000000..46ce22e --- /dev/null +++ b/mc_labs/mc_lab_04/lab4-mko/keypad4x4.h @@ -0,0 +1,85 @@ +#define DDR_KEYPAD DDRK +#define PORT_KEYPAD PORTK +#define PIN_KEYPAD PINK + + + + + + +class SegmentDisplay { +public: + byte* segments; + byte* numbers; + byte numberOfDigits; + byte* value; + SegmentDisplay(byte segments[8], byte numbers[], byte numberOfDigits) + : segments(segments), numbers(numbers), numberOfDigits(numberOfDigits) { + + value = new byte[numberOfDigits]; + } + + virtual ~SegmentDisplay() { + delete value; + } + + void init() { + for(int i = 0; i < 8; i++) + pinMode(segments[i], OUTPUT); + + for(int i = 0; i < numberOfDigits; i++) { + pinMode(numbers[i], OUTPUT); + digitalWrite(numbers[i], HIGH); + } + } + + void setValue(byte value[]) { + for(int i = 0; i < numberOfDigits; i++) { + this->value[i] = value[i]; + } + } + + + void update() { + byte actualCounter = min(counter, 7); + for(int i = 0; i < numberOfDigits; i++) + digitalWrite(numbers[i], i == actualCounter ? LOW : HIGH); + + + for(int i = 0; i < 8; i++) + digitalWrite(segments[i], (value[actualCounter] >> i) & 1 ? HIGH : LOW); + + + counter = (counter + 1) % (numberOfDigits + 2); + } + + private: + byte counter = 0; +}; + +void getSegmentFromTime(byte * segment, long time, bool alarmActive) { + long hours = (time / (60 * 60)) % 24; + long minutes = (time / 60) % 60; + long seconds = time % 60; + long timeBySymbol[] = { hours / 10, hours % 10, minutes / 10, minutes % 10, seconds / 10, seconds % 10 }; + + for(int i = 0; i < 6; i++) { + // .gfedcba + if(timeBySymbol[i] == 0) segment[i] = 0b00111111; + if(timeBySymbol[i] == 1) segment[i] = 0b00000110; + if(timeBySymbol[i] == 2) segment[i] = 0b01011011; + if(timeBySymbol[i] == 3) segment[i] = 0b01001111; + if(timeBySymbol[i] == 4) segment[i] = 0b01100110; + if(timeBySymbol[i] == 5) segment[i] = 0b01101101; + if(timeBySymbol[i] == 6) segment[i] = 0b01111101; + if(timeBySymbol[i] == 7) segment[i] = 0b00000111; + if(timeBySymbol[i] == 8) segment[i] = 0b01111111; + if(timeBySymbol[i] == 9) segment[i] = 0b01101111; + } + + if(alarmActive) segment[5] = 0b01110111; + + segment[1] |= 0b10000000; + segment[3] |= 0b10000000; +} + diff --git a/mc_labs/mc_lab_04/lab4-mko/lab4-mko.ino b/mc_labs/mc_lab_04/lab4-mko/lab4-mko.ino new file mode 100644 index 0000000..e43c1d0 --- /dev/null +++ b/mc_labs/mc_lab_04/lab4-mko/lab4-mko.ino @@ -0,0 +1,153 @@ + + +// include the library code: +#include "keypad4x4.h" +#include + +#define MINUTES 1 +#define HOURS 2 + +enum ClockMode { + Time, + Alarm +}; + + +// initialize the library by associating any needed LCD interface pin +// with the arduino pin number it is connected to +const char segments[] = { 53, 52, 51, 50, 10, 11, 12, 13 }; +const char numbers[] = { 22, 23, 24, 25, 26, 27 }; +const char timeButton = 18; +const char alarmButton = 70; +const char setButton = 71; +const char toggleButton = 72; +const char buzzerPin = 35; + + +long time = 0; +long alarm = 0; +bool alarmActive = false; +bool alarmSetModeActive = false; +int inputData = 0; + +SegmentDisplay seg(segments, numbers, 6); + + + + + +ISR(TIMER2_OVF_vect) { + time = (time + 1) % ((long)60 * 60 * 24); +} + +ISR(TIMER1_COMPA_vect) { + displayTime(alarmSetModeActive ? alarm : time); + seg.update(); +} + + + + + +void handleButtonPress() { + byte port_val = PIND; + + if(!(~(port_val >> 3) & 0b1111)) return; + + if(!((port_val >> 3) & 1)) { + getTimeFromInputs(HOURS, alarmSetModeActive ? alarm : time); + } + if(!((port_val >> 4) & 1)) { + getTimeFromInputs(MINUTES, alarmSetModeActive ? alarm : time); + } + if(!((port_val >> 5) & 1)) + alarmSetModeActive = !alarmSetModeActive; + if(!((port_val >> 6) & 1)) alarmActive = !alarmActive; + + delay(200); +} + + +void handleCurrentMode() { + +} + + + +void displayTime(long seconds) { + byte segment[] = { 0, 0, 0, 0, 0, 0 }; + getSegmentFromTime(segment, seconds, alarmActive); + seg.setValue(segment); +} + + + +void getTimeFromInputs(char buttonMode, long& time) { + static long hours = 0; + static long minutes = 0; + + if(buttonMode == HOURS) + time = time + 3600; + if(buttonMode == MINUTES) + time = time + 60; +} + +void checkAlarm() { + digitalWrite(buzzerPin, (alarmActive && ((time - alarm) % 2 == 0) && ((time - alarm) < 30) && ((time - alarm) > 0)) ? HIGH : LOW); +} + + + +void setup() { + Serial.begin(9600); + + + DDRD &= ~0b01111000; + PORTD |= 0b01111000; + + pinMode(buzzerPin, OUTPUT); + + noInterrupts(); + //Asynchronous Operation of Timer/Counter2 + //The CPU main clock frequency must be more than four times the oscillator frequency + //a. Disable the Timer/Counter2 interrupts by clearing OCIE2x and TOIE2 + TIMSK2 = 0; + + //b. Select clock source by setting AS2 as appropriate + //When AS2 is written to one, Timer/Counter2 is clocked from a crystal oscillator + //connected to the timer oscillator 1 (TOSC1) pin + ASSR |= (1 << AS2); + + //c. Write new values to TCNT2, OCR2x, and TCCR2x + TCCR2A = 0; + TCCR2B = 0; + TCNT2 = 0; + + //d. To switch to asynchronous operation: Wait for TCN2xUB, OCR2xUB, and TCR2xUB + while (ASSR & 0x1F) {} + + //no need to change TCCR2A, normal counting mode + //prescaler set to 128 + TCCR2B |= (1 << CS22) | (1 << CS20); + + //e. Clear the Timer/Counter2 interrupt flags + TIFR2 = 0x07; + + //f. Enable interrupts, if needed + TIMSK2 |= (1 << TOIE2); + + // dynamic indication set up with timer 2 + TCCR1A = 0x00; + TCCR1B = (1 << WGM12) | (1 << CS12) | (1 << CS10); //CTC mode & Prescaler @ 1024 + TIMSK1 = (1 << OCIE1A); // allow compare match mode + OCR1A = 15624 / 512; // compare value = 1/1024 sec (8MHz AVR) + + interrupts(); + + seg.init(); +} + +void loop() { + handleButtonPress(); + checkAlarm(); +} \ No newline at end of file diff --git a/mc_labs/mc_lab_04/lab4-mko/lab4.pdsprj b/mc_labs/mc_lab_04/lab4-mko/lab4.pdsprj new file mode 100644 index 0000000..27fe046 Binary files /dev/null and b/mc_labs/mc_lab_04/lab4-mko/lab4.pdsprj differ