Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.lixko.serialcomm;
- import com.fazecast.jSerialComm.SerialPort;
- public class SerialComm {
- public static final int SERIN = 0;
- public static final int SRCK = 1;
- public static final int RCK = 2;
- public static final int SRCLR = 3;
- public static final boolean HIGH = true;
- public static final boolean LOW = false;
- byte[] gpioa = new byte[1];
- public SerialPort sp;
- public SerialComm(String port) {
- sp = SerialPort.getCommPort(port);
- }
- public void setPin(int pin, int value) {
- if (value == 0)
- gpioa[0] = (byte) (gpioa[0] & ~(1 << pin));
- else
- gpioa[0] = (byte) (gpioa[0] | (1 << pin));
- sp.writeBytes(gpioa, 1);
- }
- public void setPin(int pin, boolean value) {
- if (value)
- gpioa[0] = (byte) (gpioa[0] | (1 << pin));
- else
- gpioa[0] = (byte) (gpioa[0] & ~(1 << pin));
- sp.writeBytes(gpioa, 1);
- try {
- Thread.sleep(0, 1);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void pulsePin(int pin) {
- setPin(pin, LOW);
- setPin(pin, HIGH);
- }
- public void setPort(int value) {
- sp.writeBytes(new byte[] { (byte) value }, 1);
- }
- public void run() throws InterruptedException {
- byte[] valarr = {
- (byte) 0b11001001, 0, 1, 0x0F, (byte) 0b10101010, (byte) 0xFF, 0x55
- };
- while(true) {
- while (!sp.isOpen()) {
- sp.openPort();
- Thread.sleep(100);
- }
- for(byte val : valarr) {
- pulsePin(SRCLR);
- for(int i = 0; i < 8; i++) {
- setPin(SERIN, val & (1 << i));
- pulsePin(SRCK);
- }
- pulsePin(RCK);
- Thread.sleep(500);
- }
- }
- }
- public void demo1() throws InterruptedException {
- int p = 0;
- while (true) {
- setPort(1 << p);
- p++;
- if (p == 5)
- p = 0;
- Thread.sleep(500);
- }
- }
- public void demo2() throws InterruptedException {
- int p = 0;
- boolean direction = true;
- while (true) {
- setPort(1 << p);
- if (p < 5 && direction)
- p++;
- else {
- direction = false;
- p--;
- }
- if (p == 0 && !direction)
- direction = true;
- p++;
- Thread.sleep(500);
- }
- }
- }
- _________________________________-
- void setup() {
- // http://www.stm32duino.com/viewtopic.php?t=1130
- afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY); // release PB3 and PB5
- Serial.begin(); // baudrate doesn't matter with USB
- pinMode(PA0, OUTPUT);
- pinMode(PA1, OUTPUT);
- pinMode(PA2, OUTPUT);
- pinMode(PA3, OUTPUT);
- pinMode(PA4, OUTPUT);
- pinMode(PA5, OUTPUT);
- pinMode(PA6, OUTPUT);
- pinMode(PA7, OUTPUT);
- }
- void loop() {
- if(Serial.available()) {
- byte received = Serial.read();
- GPIOA->regs->BRR = ~received;
- GPIOA->regs->BSRR = received;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment