Skip to content

Commit

Permalink
feat:Added the base structure for the animations with speed controll.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jhalakupadhyay committed Sep 7, 2024
1 parent bcdb92c commit 85da751
Show file tree
Hide file tree
Showing 16 changed files with 341 additions and 77 deletions.
53 changes: 53 additions & 0 deletions lib/bademagic_module/utils/byte_array_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,56 @@ List<int> hexStringToByteArray(String hexString) {
logger.d(data.length);
return data;
}

List<List<int>> byteArrayToBinaryArray(List<int> byteArray) {
List<List<int>> binaryArray = List.generate(11, (_) => []);

int rowIndex = 0;
for (int byte in byteArray) {
List<int> binaryRepresentation = [];
for (int i = 7; i >= 0; i--) {
binaryRepresentation.add((byte >> i) & 1);
}

binaryArray[rowIndex].addAll(binaryRepresentation);

rowIndex = (rowIndex + 1) % 11;
}

logger.d(
"binaryArray: $binaryArray"); // Use print instead of logger for standalone example
return binaryArray;
}

String hexToBin(String hex) {
// Convert hex to binary string
String binaryString = BigInt.parse(hex, radix: 16).toRadixString(2);

// Pad the binary string with leading zeros if necessary to ensure it's a multiple of 8 bits
int paddingLength = (8 - (binaryString.length % 8)) % 8;
binaryString = binaryString.padLeft(binaryString.length + paddingLength, '0');
logger.d("binaryString: $binaryString");
return binaryString;
}

List<List<int>> binaryStringTo2DList(String binaryString) {
int maxHeight = 11;
List<List<int>> binary2DList = List.generate(maxHeight, (_) => []);

for (int x = 0; x < binaryString.length; x++) {
int a = 0;
for (int y = a; y < 11; y++) {
for (int z = 0; z < 8; z++) {
binary2DList[y].add(int.parse(binaryString[x++]));
if (x >= binaryString.length) {
break;
}
}
if (x >= binaryString.length) {
break;
}
}
}
logger.d("binary2DList: $binary2DList");
return binary2DList;
}
22 changes: 13 additions & 9 deletions lib/bademagic_module/utils/converters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import 'package:badgemagic/bademagic_module/utils/byte_array_utils.dart';
import 'package:badgemagic/bademagic_module/utils/data_to_bytearray_converter.dart';
import 'package:badgemagic/bademagic_module/utils/file_helper.dart';
import 'package:badgemagic/bademagic_module/utils/image_utils.dart';
import 'package:badgemagic/providers/cardsprovider.dart';
import 'package:badgemagic/providers/badgeview_provider.dart';
import 'package:badgemagic/providers/imageprovider.dart';
import 'package:badgemagic/view/draw_badge_screen.dart';
import 'package:get_it/get_it.dart';

class Converters {
Expand All @@ -18,7 +16,6 @@ class Converters {
DrawBadgeProvider badgeList = GetIt.instance.get<DrawBadgeProvider>();

int controllerLength = 0;
bool isEmpty = false;

Future<List<String>> messageTohex(String message) async {
List<String> hexStrings = [];
Expand All @@ -43,16 +40,23 @@ class Converters {
}
}
}
if (message.isEmpty) {
badgeList.resetGrid();
isEmpty = true;
return hexStrings;
}

void badgeAnimation(String message) async {
if (message == "") {
//geerate a 2d list with all values as 0
List<List<int>> image =
List.generate(11, (i) => List.generate(44, (j) => 0));
badgeList.setNewGrid(image);
badgeList.startAnimation();
} else {
isEmpty = false;
List<String> hexStrings = await messageTohex(message);
List<int> byteArray = hexStringToByteArray(hexStrings.join());
// List<List<int>> binaryArray = byteArrayToBinaryArray(byteArray);
List<List<int>> binaryArray = byteArrayToBinaryArray(byteArray);
badgeList.setNewGrid(binaryArray);
badgeList.startAnimation();
}
return hexStrings;
}

//function to convert the bitmap to the LED hex format
Expand Down
1 change: 0 additions & 1 deletion lib/bademagic_module/utils/image_utils.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:ui' as ui;
import 'dart:ui';
import 'package:badgemagic/bademagic_module/utils/byte_array_utils.dart';
import 'package:badgemagic/bademagic_module/utils/converters.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down
15 changes: 15 additions & 0 deletions lib/badge_animation/animation_abstract.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
abstract class BadgeAnimation {
void animation(
List<List<bool>> grid,
List<List<int>> newGrid,
int animationIndex,
bool validMarquee,
bool flashLEDOn,
int currentcountFrame,
int i,
int j,
int newHeight,
int newWidth,
int badgeHeight,
int badgeWidth);
}
12 changes: 12 additions & 0 deletions lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ const String aniRight = 'assets/animations/ic_anim_right.gif';
const String effFlash = 'assets/effects/ic_effect_flash.gif';
const String effInvert = 'assets/effects/ic_effect_invert.gif';
const String effMarque = 'assets/effects/ic_effect_marquee.gif';

//constants for the animation speed
const Duration aniBaseSpeed = Duration(microseconds: 200000); // in uS
const Duration aniMarqueSpeed = Duration(microseconds: 100000); // in uS
const Duration aniFlashSpeed = Duration(microseconds: 500000); // in uS

// Function to calculate animation speed based on speed level
int aniSpeedStrategy(int speedLevel) {
int speedInMicroseconds = aniBaseSpeed.inMicroseconds -
(speedLevel * aniBaseSpeed.inMicroseconds ~/ 8);
return speedInMicroseconds;
}
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:badgemagic/providers/cardsprovider.dart';
import 'package:badgemagic/providers/drawbadge_provider.dart';
import 'package:badgemagic/providers/badgeview_provider.dart';
import 'package:badgemagic/providers/getitlocator.dart';
import 'package:badgemagic/providers/imageprovider.dart';
import 'package:badgemagic/view/draw_badge_screen.dart';
Expand Down
203 changes: 203 additions & 0 deletions lib/providers/badgeview_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
import 'dart:async';

import 'package:badgemagic/bademagic_module/utils/byte_array_utils.dart';
import 'package:badgemagic/badge_animation/animation_abstract.dart';
import 'package:badgemagic/constants.dart';
import 'package:badgemagic/providers/cardsprovider.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';

class DrawBadgeProvider extends ChangeNotifier {
CardProvider cardData = GetIt.instance<CardProvider>();
int countFrame = 0;
int animationIndex = 0;
int lastFrame = 0;
AnimationController? _controller;
int animationSpeed = aniBaseSpeed.inMilliseconds;
int counter = 0;
Timer? timer;
//List that contains the state of each cell of the badge for home view
List<List<bool>> homeViewGrid =
List.generate(11, (i) => List.generate(44, (j) => false));

//List that contains the state of each cell of the badge for draw view
List<List<bool>> drawViewGrid =
List.generate(11, (i) => List.generate(44, (j) => false));

//getter for the drawViewGrid
List<List<bool>> getDrawViewGrid() => drawViewGrid;

//setter for the drawViewGrid
void setDrawViewGrid(List<List<bool>> newGrid) {
drawViewGrid = newGrid;
notifyListeners();
}

BadgeAnimation? currentAnimation;

//function to update the state of the cell
void updateGrid(int row, int col) {
homeViewGrid[row][col] = isDrawing;
notifyListeners();
}

//function to reset the state of the cell
void resetGrid() {
homeViewGrid = List.generate(11, (i) => List.generate(44, (j) => false));
notifyListeners();
}

//function to get the state of the cell
List<List<bool>> getGrid() => homeViewGrid;

//boolean variable to check for isDrawing on Draw badge screen
bool isDrawing = true;

//function to toggle the isDrawing variable
void toggleIsDrawing(bool drawing) {
isDrawing = drawing;
notifyListeners();
}

//function to calculate duration for the animation
void calculateDuration() {
int newSpeed = aniSpeedStrategy(cardData.getOuterValue());
if (newSpeed != animationSpeed) {
animationSpeed = newSpeed;
timer?.cancel();
startTimer();
}
}

//function to get the isDrawing variable
bool getIsDrawing() => isDrawing;

List<List<int>> newGrid =
List.generate(11, (i) => List.generate(44, (j) => 0));

//getter for newGrid
List<List<int>> getNewGrid() => newGrid;

//setter for newGrid
void setNewGrid(List<List<int>> newGrid) {
this.newGrid = newGrid;
notifyListeners();
}

void initializeAnimation(TickerProvider vsync) {
_controller = AnimationController(
vsync: vsync,
duration: const Duration(seconds: 1000),
)..addListener(() {
setAnimationMode();
changeGridValue(newGrid);
calculateDuration();
});
startTimer();
_controller!.repeat();
}

void startTimer() {
logger.i("Timer started");
logger.i("Animation speed: $animationSpeed");
timer =
Timer.periodic(Duration(microseconds: animationSpeed), (Timer timer) {
animationIndex++;
});
}

void startAnimation() {
animationIndex = 0;
_controller!.forward();
}

void setAnimationMode() {
switch (cardData.getAnimationIndex()) {
//add cases from 0 to 8
case 0:
currentAnimation = null;
break;
case 1:
// currentAnimation = RightAnimation();
break;
case 2:
// currentAnimation = UpAnimation();
break;
case 3:
// currentAnimation = DownAnimation();
break;
case 4:
// currentAnimation = FixedAnimation();
break;
case 5:
// currentAnimation = SnowFlakeAnimation();
break;
case 6:
currentAnimation = null;
break;
case 7:
currentAnimation = null;
break;
case 8:
currentAnimation = null;
break;
default:
currentAnimation = null;
break;
}
}

void changeGridValue(List<List<int>> newGrid) {
int badgeWidth = homeViewGrid[0].length;
int badgeHeight = homeViewGrid.length;
int newHeight = newGrid.length;
int newWidth = newGrid[0].length;

// Process grid
for (int i = 0; i < badgeHeight; i++) {
// bool matchFrame = false;

for (int j = 0; j < badgeWidth; j++) {
bool flashLEDOn = true;

if (cardData.getEffectIndex(1) == 1) {
int aIFlash = animationIndex % 2;
flashLEDOn = aIFlash == 0;
}

bool validMarquee = false;

if (cardData.getEffectIndex(2) == 1) {
int aIMarquee = animationIndex ~/ 2;
validMarquee =
(i == 0 || j == 0 || i == badgeHeight - 1 || j == badgeWidth - 1);

if (validMarquee) {
if ((i == 0 || j == badgeWidth - 1) &&
!(i == badgeHeight - 1 && j == badgeWidth - 1)) {
validMarquee = (i + j) % 4 == (aIMarquee % 4);
} else {
validMarquee = (i + j - 1) % 4 == (3 - (aIMarquee % 4));
}
}
}
if (currentAnimation != null) {
currentAnimation!.animation(
homeViewGrid,
newGrid,
animationIndex,
validMarquee,
flashLEDOn,
countFrame,
i,
j,
newHeight,
newWidth,
badgeHeight,
badgeWidth);
}
}
notifyListeners();
}
}
}
34 changes: 0 additions & 34 deletions lib/providers/drawbadge_provider.dart

This file was deleted.

2 changes: 1 addition & 1 deletion lib/providers/getitlocator.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:badgemagic/providers/cardsprovider.dart';
import 'package:badgemagic/providers/drawbadge_provider.dart';
import 'package:badgemagic/providers/badgeview_provider.dart';
import 'package:badgemagic/providers/imageprovider.dart';
import 'package:get_it/get_it.dart';

Expand Down
Loading

0 comments on commit 85da751

Please sign in to comment.