Skip to content

Instantly share code, notes, and snippets.

View chayanforyou's full-sized avatar
🤖
Learning

Chayan Mistry chayanforyou

🤖
Learning
View GitHub Profile
@chayanforyou
chayanforyou / isolate_flutter.dart
Created March 4, 2024 04:53
Using an Isolate in Flutter
import 'dart:async';
import 'dart:isolate';
class CountdownTimer {
final receivePort = ReceivePort();
late Isolate _isolate;
void stop() {
receivePort.close();
_isolate.kill(priority: Isolate.immediate);

The technique I normally use is to prefix the data with a 4-byte rolling sequence number where the largest number represents the lastest / current value. In the case of storing 2 bytes of actual data that would give 6 bytes total and then I form into a circular queue arrangement so for 128 bytes of EEPROM it would contain 21 entries and increase endurance 21 times.

Then when booting the largest sequence number can be used to determine both the next sequence number to be used and the current tail of the queue. The following C pseudo-code demonstrates, this assumes that upon initial programming the EEPROM area has been erased to values of 0xFF so I ignore a sequence number of 0xFFFF:

struct
{
  uint32_t sequence_no;
  uint16_t my_data;
} QUEUE_ENTRY;
@chayanforyou
chayanforyou / Using .c files in Arduino.md
Created July 7, 2023 07:12
Arduino not working with ".c" file

The .ino files of Arduino sketches are compiled as C++ after a little bit of preprocessing. If you want to use C functions in C++, you need to wrap their declarations in:

extern "C" {}

There are a couple ways you can do this:

In the sketch:

@chayanforyou
chayanforyou / pic12f615_adc_interrupt.c
Created April 27, 2023 14:46 — forked from sapher/pic12f615_adc_interrupt.c
PIC12F615 ADC interrupt exemple
#include "xc.h"
#include "config.h"
#define _XTAL_FREQ 4000000
unsigned short pot = 0;
void interrupt isr() {
// Disable GIE (best practice?)
@chayanforyou
chayanforyou / MainActivity.java
Created October 9, 2022 09:52 — forked from blackcj/MainActivity.java
Design support library with CoordinatorLayout, SwipeRefreshLayout and RecyclerView.
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import com.blackcj.designsupportexample.adapters.RecyclerViewAdapter;
@chayanforyou
chayanforyou / AppbarSwipeRefreshLayout
Created October 9, 2022 09:36 — forked from ian-ellis/AppbarSwipeRefreshLayout
Fixing Up SwipeRefreshLayout to handle a collapsing toolbar
package au.com.qantas.qantas.common.presentation;
import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.util.AttributeSet;
import android.view.View;
import android.support.v4.widget.SwipeRefreshLayout;
public class AppbarSwipeRefreshLayout extends SwipeRefreshLayout implements
AppBarLayout.OnOffsetChangedListener {
@chayanforyou
chayanforyou / atmega8_timer0.c
Created October 1, 2022 02:53 — forked from mkleemann/atmega8_timer0.c
atmega8 timer0 - use overflow interrupt
// All values/comments are valid with ATmega8 running at Fosc = 4.000MHz
#include <avr/io.h>
#include <avr/interrupt.h>
// TIMER0 with prescaler clkI/O/1024
#define TIMER0_PRESCALER (1 << CS02) | (1 << CS00)
void main()
@chayanforyou
chayanforyou / DecimalToFraction
Created September 28, 2022 04:00 — forked from jaffes2/DecimalToFraction
Decimal to Fraction Converter
/*
* Converts Decimals to Fractions
*
*/
package simpleai;
/**
*
* @author sarabeth
*/
@chayanforyou
chayanforyou / decimalToFraction.js
Created September 28, 2022 04:00 — forked from redteam-snippets/decimalToFraction.js
JavaScript: Decimal To Fraction
// Adapted from: http://bateru.com/news/2011/11/improved-code-for-javascript-decimal-to-fraction/
function gcd(a, b) {
return (b) ? gcd(b, a % b) : a;
}
var decimalToFraction = function (_decimal) {
var top = _decimal.toString().replace(/\d+[.]/, '');
var bottom = Math.pow(10, top.length);
if (_decimal > 1) {