Skip to content

Instantly share code, notes, and snippets.

View branw's full-sized avatar
🐠
<º)))><

Brandon W branw

🐠
<º)))><
View GitHub Profile
@branw
branw / samd51_1dac_2adc_dma.cpp
Last active March 21, 2024 18:52
Minimum working example of analog data acquisition with one output and two inputs on the SAMD51 Grand Central M4.
// Produces a sawtooth on A0 and samples A1 and A3 into buffers via DMA
#include <Adafruit_ZeroDMA.h>
#include <wiring_private.h> // for access to pinPeripheral
// Create buffers for DMAing data around -- the .dmabuffers section is supposed to
// optimize the memory location for the DMA controller
__attribute__ ((section(".dmabuffers"), used)) static uint16_t dac_buffer[4096], adc_buffer[2][4096];
void setup() {
@branw
branw / map.hpp
Last active September 3, 2020 16:20
A static, immutable C++17 map designed for mapping continuous enum keys to arbitrary values
#pragma once
#include <utility>
namespace Detail {
template<typename KeyTy, typename ValueTy>
struct Element {
using KeyType = KeyTy;
using ValueType = ValueTy;
using PairType = std::pair<KeyTy, ValueTy>;
// FNV-1a hash with 64-bit output
size_t FNV1a(const uint8_t *data, size_t dataLen) {
uint64_t hash = 14695981039346656037ull;
for (size_t i = 0; i < dataLen; i++) {
hash ^= data[i];
hash *= 1099511628211ull;
}
return hash;
}
import matplotlib.pyplot as plt
from datetime import datetime
import serial
import serial.tools.list_ports
import struct
import time
import math
import os
# COM port of Arduino Due; leave None for cross-platform auto-detection
import matplotlib.pyplot as plt
from datetime import datetime
import numpy as np
import serial
import struct
import time
import math
import os
# Native USB port of Due (use Device Manager to find)
#define VERSION_MAJOR 0
#define VERSION_MINOR 2
// 1.6 MHz is the max frequency of the DAC, so it's easiest
// just to scale everything off of it. 1 ADC channel runs at
// 41.25% (~660 kHz); 2 ADC channels run at 25% (400 kHz each)
const int freq = 1.6e6;
// Duration of a single chirp
const double chirp_duration = 15E-3;
import matplotlib.pyplot as plt
from datetime import datetime
import numpy as np
import serial
import struct
import time
import math
import os
# Native USB port of Due (use Device Manager to find)
def main():
try:
ptu = PTU(PTU_PORT)
except serial.serialutil.SerialException:
print(f'Could not connect to pan-tilt on port {PTU_PORT}')
return
# Open connection to device
ser = serial.Serial(DUE_PORT)
ser.setDTR(False)
bool flash_read_unique_id(uint32_t *pul_data) {
// Initialize the first EEFC bank in 128-bit mode with 4 wait states
if (efc_init(EFC0, 0, 4)) {
return false;
}
// Enter the unique identifier mode, copy it, and exit
return !efc_perform_read_sequence(EFC0, EFC_FCMD_STUI, EFC_FCMD_SPUI, pul_data, 4);
}
@branw
branw / mega.cpp
Last active September 24, 2018 17:37
#include <Arduino.h>
#include <BasicStepperDriver.h>
// Accept a trigger of 's' over Serial1 or Serial2
#define USE_SERIAL_TRIGGER true
// Enable the motors only when they are being moved
#define DISABLE_MOTORS false
const int EAR_LEFT_DIR = 48, EAR_LEFT_STEP = 46, EAR_LEFT_ENABLE = 62; // Z