DEV Community

jay jordan
jay jordan

Posted on

gaming portable console heater

include

include

include

include

include

include

include

int uinput;

extern "C"
JNIEXPORT void JNICALL
Java_com_monobogdan_inputservicebridge_InputNative_init(JNIEnv *env, jclass clazz) {
uinput = open("/dev/input/event2", O_WRONLY);

__android_log_print(ANDROID_LOG_DEBUG  , "Test", uinput >= 0 ? "Open event OK" : "Failed to open event");
Enter fullscreen mode Exit fullscreen mode

}

void emit(int fd, int type, int code, int val)
{
struct input_event ie;
ie.type = type;
ie.code = code;
ie.value = val;
/* timestamp values below are ignored */
ie.time.tv_sec = 0;
ie.time.tv_usec = 0;
write(fd, &ie, sizeof(ie));
}

extern "C"
JNIEXPORT void JNICALL
Java_com_monobogdan_inputservicebridge_InputNative_sendKeyEvent(JNIEnv *env, jclass clazz,
jint key_code, jboolean pressed) {
__android_log_print(ANDROID_LOG_DEBUG , "Test", "Send");
emit(uinput, EV_KEY, key_code, (bool)pressed ? 1 : 0);
emit(uinput, EV_SYN, SYN_REPORT, 0);
}
package com.monobogdan.inputservicebridge;

public class InputListener extends Service {

private static final int tty = 3;

private InputManager iManager;
private Map<Character, Integer> keyMap;
private Method injectMethod;

private Process runAsRoot(String cmd)
{
    try {
        return Runtime.getRuntime().exec(new String[] { "su", "-c", cmd });
    }
    catch (IOException e)
    {
        e.printStackTrace();

        return null;
    }
}

@Override
public void onCreate() {
    super.onCreate();

    // According to linux key map (input-event-codes.h)
    keyMap = new HashMap<>();
    keyMap.put('U', 103);
    keyMap.put('D', 108);
    keyMap.put('L', 105);
    keyMap.put('R', 106);
    keyMap.put('E', 115);
    keyMap.put('B', 158);
    keyMap.put('A', 232);
    keyMap.put('C', 212);

    InputNative.init();

    try {
        runAsRoot("chmod 777 /dev/input/event2").waitFor();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }

    Executors.newSingleThreadExecutor().execute(new Runnable() {
        @Override
        public void run() {
            Process proc = runAsRoot("cat /dev/ttyMT" + tty);
            BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));

            while(true)
            {
                try {
                    String line = reader.readLine();

                    if(line != null && line.length() > 0) {
                        Log.i("Hi", "run: " + line);

                        boolean pressing = line.charAt(0) == 'D';
                        int keyCode = keyMap.get(line.charAt(2));

                        Log.i("TAG", "run: " + keyCode);
                        InputNative.sendKeyEvent(keyCode, pressing);
                    }
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }

                /*try {
                    Thread.sleep(1000 / 30);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
            }
        }
    });
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}
Enter fullscreen mode Exit fullscreen mode

}#include

include

include

include "pico/stdlib.h"

include "pico/time.h"

include "hardware/uart.h"

struct keyMap
{
int gpio;
char key;
bool pressed;
int lastTick;
};

keyMap keys[] = {
{
15,
'L',
false,
0
},
{
14,
'U',
false,
0
},
{
13,
'D',
false,
0
},
{
12,
'R',
false,
0
},
{
11,
'E',
false,
0
},
{
10,
'B',
false,
0
},

{
    20,
    'A',
    false,
    0
},
{
    21,
    'C',
    false,
    0
}
Enter fullscreen mode Exit fullscreen mode

};

define KEY_NUM 8

int main() {
stdio_init_all();

uart_init(uart0, 921600);
gpio_set_function(PICO_DEFAULT_UART_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(PICO_DEFAULT_UART_RX_PIN, GPIO_FUNC_UART);
sleep_ms(1000); // Allow serial monitor to settle

for(int i = 0; i <  KEY_NUM; i++)
{
    gpio_init(keys[i].gpio);
    gpio_set_dir(keys[i].gpio, false);
    gpio_pull_up(keys[i].gpio);
}

while(true)
{
    int now = time_us_32();

    for(int i = 0; i < KEY_NUM; i++)
    {
        char buf[5];
        buf[1] = ' ';
        buf[3] = '\n';
        buf[4] = 0;

        if(!gpio_get(keys[i].gpio) && !keys[i].pressed && now - keys[i].lastTick > 15500)
        {
            buf[0] = 'D';
            buf[2] = keys[i].key;
            puts(buf);

            keys[i].lastTick = now;
            keys[i].pressed = true;
            continue;
        }

        if(gpio_get(keys[i].gpio) && keys[i].pressed && now - keys[i].lastTick > 15500)
        {
            buf[0] = 'U';
            buf[2] = keys[i].key;
            puts(buf);

            keys[i].pressed = false;
            keys[i].lastTick = now;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}

include "DHT.h"

include "LiquidCrystal.h"

LiquidCrystal lcd(7, 8, 9, 10, 11 ,12);

define DHTPIN 6

define DHTTYPE DHT22

DHT sensor(DHTPIN, DHTTYPE);
int relay_pin = 9;

void setup() {
lcd.begin(16,2);
sensor.begin();
pinMode(relay_pin, OUTPUT);
digitalWrite(relay_pin, HIGH);
}
void loop() {
lcd.clear();
float t = sensor.readTemperature(); //считывание температуры с датчика
// Проверка, посылает ли датчик значения или нет
if (isnan(t)) {
lcd.print("Failed");
delay(1000);
return;
}
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print(" C");
if (t > 35){
digitalWrite(relay_pin, LOW);
lcd.setCursor(0,1);
lcd.print("Fan is ON ");
delay(10);
}
else{
digitalWrite(relay_pin, HIGH);
lcd.setCursor(0,1);
lcd.print("Fan is OFF ");
}
delay(2000);
}

Top comments (0)